report_usage.go 1.4 KB

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