report_usage.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package metrics
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "time"
  7. "github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. func StartUsageReportLoop() chan struct{} {
  11. M_Instance_Start.Inc(1)
  12. ticker := time.NewTicker(24 * time.Hour)
  13. for {
  14. select {
  15. case <-ticker.C:
  16. sendUsageStats()
  17. }
  18. }
  19. }
  20. func sendUsageStats() {
  21. log.Trace("Sending anonymous usage stats to stats.grafana.org")
  22. metrics := map[string]interface{}{}
  23. report := map[string]interface{}{
  24. "version": setting.BuildVersion,
  25. "metrics": metrics,
  26. }
  27. // statsQuery := m.GetSystemStatsQuery{}
  28. // if err := bus.Dispatch(&statsQuery); err != nil {
  29. // log.Error(3, "Failed to get system stats", err)
  30. // return
  31. // }
  32. UsageStats.Each(func(name string, i interface{}) {
  33. switch metric := i.(type) {
  34. case Counter:
  35. if metric.Count() > 0 {
  36. metrics[name+".count"] = metric.Count()
  37. metric.Clear()
  38. }
  39. }
  40. })
  41. // metrics["stats.dashboards.count"] = statsQuery.Result.DashboardCount
  42. // metrics["stats.users.count"] = statsQuery.Result.UserCount
  43. // metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
  44. out, _ := json.Marshal(report)
  45. data := bytes.NewBuffer(out)
  46. client := http.Client{Timeout: time.Duration(5 * time.Second)}
  47. go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data)
  48. }