Add more fields, actually send data somewhere

This commit is contained in:
Till Faelligen 2022-03-03 16:19:45 +01:00
parent c60422191e
commit 845363253a
3 changed files with 34 additions and 3 deletions

View file

@ -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

View file

@ -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)

View file

@ -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
}
}