From b5b635f923cd119638af9d612f4562b6b962ed9e Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Tue, 3 May 2022 20:51:57 +0200 Subject: [PATCH] Update config --- setup/config/config_global.go | 31 ++++++++++++++++++++++++------- userapi/userapi.go | 2 +- userapi/util/phonehomestats.go | 4 ++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/setup/config/config_global.go b/setup/config/config_global.go index 3928cc411..9d4c1485e 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -71,11 +71,8 @@ type Global struct { // ServerNotices configuration used for sending server notices ServerNotices ServerNotices `yaml:"server_notices"` - // ReportStats configures anonymous usage stats of the server - ReportStats bool `yaml:"report_stats"` - - // ReportStatsEndpoint configuration - ReportStatsEndpoint string `yaml:"report_stats_endpoint"` + // ReportStats configures opt-in anonymous stats reporting. + ReportStats ReportStats `yaml:"report_stats"` } func (c *Global) Defaults(generate bool) { @@ -86,14 +83,13 @@ func (c *Global) Defaults(generate bool) { c.KeyID = "ed25519:auto" } c.KeyValidityPeriod = time.Hour * 24 * 7 - c.ReportStats = false - c.ReportStatsEndpoint = "https://matrix.org/report-usage-stats/push" c.JetStream.Defaults(generate) c.Metrics.Defaults(generate) c.DNSCache.Defaults() c.Sentry.Defaults() c.ServerNotices.Defaults(generate) + c.ReportStats.Defaults() } func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) { @@ -105,6 +101,7 @@ func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) { c.Sentry.Verify(configErrs, isMonolith) c.DNSCache.Verify(configErrs, isMonolith) c.ServerNotices.Verify(configErrs, isMonolith) + c.ReportStats.Verify(configErrs, isMonolith) } type OldVerifyKeys struct { @@ -171,6 +168,26 @@ func (c *ServerNotices) Defaults(generate bool) { func (c *ServerNotices) Verify(errors *ConfigErrors, isMonolith bool) {} +// ReportStats configures opt-in anonymous stats reporting. +type ReportStats struct { + // Enabled configures anonymous usage stats of the server + Enabled bool `yaml:"enabled"` + + // Endpoint the endpoint to report stats to + Endpoint string `yaml:"endpoint"` +} + +func (c *ReportStats) Defaults() { + c.Enabled = false + c.Endpoint = "https://matrix.org/report-usage-stats/push" +} + +func (c *ReportStats) Verify(configErrs *ConfigErrors, isMonolith bool) { + if c.Enabled { + checkNotEmpty(configErrs, "global.report_stats.endpoint", c.Endpoint) + } +} + // The configuration to use for Sentry error reporting type Sentry struct { Enabled bool `yaml:"enabled"` diff --git a/userapi/userapi.go b/userapi/userapi.go index e546ad1c3..5b11665db 100644 --- a/userapi/userapi.go +++ b/userapi/userapi.go @@ -105,7 +105,7 @@ func NewInternalAPI( } time.AfterFunc(time.Minute, cleanOldNotifs) - if base.Cfg.Global.ReportStats { + if base.Cfg.Global.ReportStats.Enabled { go util.StartPhoneHomeCollector(time.Now(), base.Cfg, db) } diff --git a/userapi/util/phonehomestats.go b/userapi/util/phonehomestats.go index e6b440aa9..c8fd9cac8 100644 --- a/userapi/util/phonehomestats.go +++ b/userapi/util/phonehomestats.go @@ -142,9 +142,9 @@ func (p *phoneHomeStats) collect() { return } - logrus.Infof("Reporting stats to %s: %s", p.cfg.Global.ReportStatsEndpoint, output.String()) + logrus.Infof("Reporting stats to %s: %s", p.cfg.Global.ReportStats.Endpoint, output.String()) - request, err := http.NewRequest("POST", p.cfg.Global.ReportStatsEndpoint, &output) + request, err := http.NewRequest(http.MethodPost, p.cfg.Global.ReportStats.Endpoint, &output) if err != nil { logrus.WithError(err).Error("unable to create phone home stats request") return