grafana/pkg/metrics/report_usage.go

65 lines
1.4 KiB
Go
Raw Normal View History

2015-03-22 14:14:00 -05:00
package metrics
import (
"bytes"
"encoding/json"
"net/http"
2015-03-22 15:13:16 -05:00
"strings"
2015-03-22 14:14:00 -05:00
"time"
"github.com/grafana/grafana/pkg/bus"
2015-03-22 14:45:13 -05:00
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
2015-03-22 14:14:00 -05:00
"github.com/grafana/grafana/pkg/setting"
)
func StartUsageReportLoop() chan struct{} {
M_Instance_Start.Inc(1)
ticker := time.NewTicker(time.Hour * 24)
2015-03-22 14:14:00 -05:00
for {
select {
case <-ticker.C:
sendUsageStats()
}
}
}
func sendUsageStats() {
2015-03-22 14:45:13 -05:00
log.Trace("Sending anonymous usage stats to stats.grafana.org")
2015-03-22 15:13:16 -05:00
version := strings.Replace(setting.BuildVersion, ".", "_", -1)
2015-03-22 14:14:00 -05:00
metrics := map[string]interface{}{}
report := map[string]interface{}{
2015-03-22 15:13:16 -05:00
"version": version,
2015-03-22 14:14:00 -05:00
"metrics": metrics,
}
statsQuery := m.GetSystemStatsQuery{}
if err := bus.Dispatch(&statsQuery); err != nil {
log.Error(3, "Failed to get system stats", err)
return
}
2015-03-22 14:14:00 -05:00
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
2015-03-22 14:14:00 -05:00
out, _ := json.Marshal(report)
data := bytes.NewBuffer(out)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
2015-03-22 14:24:35 -05:00
go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data)
2015-03-22 14:14:00 -05:00
}