grafana/pkg/metrics/report_usage.go

61 lines
1.3 KiB
Go
Raw Normal View History

2015-03-23 03:14:00 +08:00
package metrics
import (
"bytes"
"encoding/json"
"net/http"
"time"
2015-03-23 03:45:13 +08:00
"github.com/grafana/grafana/pkg/log"
2015-03-23 03:14:00 +08:00
"github.com/grafana/grafana/pkg/setting"
)
func StartUsageReportLoop() chan struct{} {
M_Instance_Start.Inc(1)
2015-03-23 03:45:13 +08:00
ticker := time.NewTicker(24 * time.Hour)
2015-03-23 03:14:00 +08:00
for {
select {
case <-ticker.C:
sendUsageStats()
}
}
}
func sendUsageStats() {
2015-03-23 03:45:13 +08:00
log.Trace("Sending anonymous usage stats to stats.grafana.org")
2015-03-23 03:14:00 +08:00
metrics := map[string]interface{}{}
report := map[string]interface{}{
"version": setting.BuildVersion,
"metrics": metrics,
}
// statsQuery := m.GetSystemStatsQuery{}
// if err := bus.Dispatch(&statsQuery); err != nil {
// log.Error(3, "Failed to get system stats", err)
// return
// }
UsageStats.Each(func(name string, i interface{}) {
switch metric := i.(type) {
case Counter:
if metric.Count() > 0 {
metrics[name+".count"] = metric.Count()
metric.Clear()
}
}
})
// metrics["stats.dashboards.count"] = statsQuery.Result.DashboardCount
// metrics["stats.users.count"] = statsQuery.Result.UserCount
// metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
out, _ := json.Marshal(report)
data := bytes.NewBuffer(out)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
2015-03-23 03:24:35 +08:00
go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data)
2015-03-23 03:14:00 +08:00
}