From 845363253a53cf959fe36df1a662576adf495247 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Thu, 3 Mar 2022 16:19:45 +0100 Subject: [PATCH] Add more fields, actually send data somewhere --- dendrite-config.yaml | 5 +++++ setup/config/config_global.go | 4 ++++ syncapi/syncapi.go | 28 +++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/dendrite-config.yaml b/dendrite-config.yaml index e799a1696..3270096bb 100644 --- a/dendrite-config.yaml +++ b/dendrite-config.yaml @@ -71,6 +71,11 @@ global: # Whether this instance sends anonymous usage stats report_stats: false + # The endpoint to report the anonymized homeserver usage statistics to. + # Defaults to https://matrix.org/report-usage-stats/push + # + #report_stats_endpoint: https://example.com/report-usage-stats/push + # Server notices allows server admins to send messages to all users. server_notices: enabled: false diff --git a/setup/config/config_global.go b/setup/config/config_global.go index 4cde29312..126807010 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -63,6 +63,9 @@ type Global struct { // ReportStats configures anonymous usage stats of the server ReportStats bool `yaml:"report_stats"` + + // ReportStatsEndpoint configuration + ReportStatsEndpoint string `yaml:"report_stats_endpoint"` } func (c *Global) Defaults(generate bool) { @@ -74,6 +77,7 @@ func (c *Global) Defaults(generate bool) { } 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) diff --git a/syncapi/syncapi.go b/syncapi/syncapi.go index bbfdbb944..f2213a830 100644 --- a/syncapi/syncapi.go +++ b/syncapi/syncapi.go @@ -19,6 +19,7 @@ import ( "context" "encoding/json" "math" + "net/http" "runtime" "syscall" "time" @@ -136,6 +137,7 @@ type phoneHomeStats struct { cfg *config.Dendrite db storage.Database isMonolith bool + client *http.Client } type timestampToRUUsage struct { @@ -152,10 +154,13 @@ func startPhoneHomeCollector(startTime time.Time, cfg *config.Dendrite, syncDB s db: syncDB, userAPI: userAPI, isMonolith: isMonolith, + client: &http.Client{ + Timeout: time.Second * 30, + }, } // start initial run after 5min - time.AfterFunc(time.Second*10, func() { + time.AfterFunc(time.Second*1, func() { p.collect() }) @@ -173,11 +178,15 @@ func startPhoneHomeCollector(startTime time.Time, cfg *config.Dendrite, syncDB s func (p *phoneHomeStats) collect() { p.stats = make(map[string]interface{}) // general information - p.stats["servername"] = p.serverName + p.stats["homeserver"] = p.serverName p.stats["monolith"] = p.isMonolith p.stats["version"] = internal.VersionString() p.stats["timestamp"] = time.Now().Unix() p.stats["go_version"] = runtime.Version() + p.stats["go_arch"] = runtime.GOARCH + p.stats["go_os"] = runtime.GOOS + p.stats["num_cpu"] = runtime.NumCPU() + p.stats["num_go_routine"] = runtime.NumGoroutine() p.stats["uptime_seconds"] = math.Floor(time.Now().Sub(p.startTime).Seconds()) ctx, cancel := context.WithTimeout(context.TODO(), time.Minute*1) @@ -321,5 +330,18 @@ func (p *phoneHomeStats) collect() { return } - logrus.Infof("Collected data: %s", output.String()) + logrus.Infof("Reporting stats to %s: %s", p.cfg.Global.ReportStatsEndpoint, output.String()) + + request, err := http.NewRequest("POST", p.cfg.Global.ReportStatsEndpoint, &output) + if err != nil { + logrus.WithError(err).Error("unable to create phone home stats request") + return + } + request.Header.Set("User-Agent", "Dendrite/"+internal.VersionString()) + + _, err = p.client.Do(request) + if err != nil { + logrus.WithError(err).Error("unable to send phone home stats") + return + } } \ No newline at end of file