publish.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package metrics
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/grafana/grafana/pkg/bus"
  9. "github.com/grafana/grafana/pkg/log"
  10. m "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/plugins"
  12. "github.com/grafana/grafana/pkg/setting"
  13. )
  14. func Init() {
  15. settings := readSettings()
  16. initMetricVars(settings)
  17. go instrumentationLoop(settings)
  18. }
  19. func instrumentationLoop(settings *MetricSettings) chan struct{} {
  20. M_Instance_Start.Inc(1)
  21. onceEveryDayTick := time.NewTicker(time.Hour * 24)
  22. secondTicker := time.NewTicker(time.Second * time.Duration(settings.IntervalSeconds))
  23. for {
  24. select {
  25. case <-onceEveryDayTick.C:
  26. sendUsageStats()
  27. case <-secondTicker.C:
  28. if settings.Enabled {
  29. sendMetrics(settings)
  30. }
  31. }
  32. }
  33. }
  34. func sendMetrics(settings *MetricSettings) {
  35. if len(settings.Publishers) == 0 {
  36. return
  37. }
  38. metrics := MetricStats.GetSnapshots()
  39. for _, publisher := range settings.Publishers {
  40. publisher.Publish(metrics)
  41. }
  42. }
  43. func sendUsageStats() {
  44. if !setting.ReportingEnabled {
  45. return
  46. }
  47. log.Trace("Sending anonymous usage stats to stats.grafana.org")
  48. version := strings.Replace(setting.BuildVersion, ".", "_", -1)
  49. metrics := map[string]interface{}{}
  50. report := map[string]interface{}{
  51. "version": version,
  52. "metrics": metrics,
  53. }
  54. statsQuery := m.GetSystemStatsQuery{}
  55. if err := bus.Dispatch(&statsQuery); err != nil {
  56. log.Error(3, "Failed to get system stats", err)
  57. return
  58. }
  59. metrics["stats.dashboards.count"] = statsQuery.Result.DashboardCount
  60. metrics["stats.users.count"] = statsQuery.Result.UserCount
  61. metrics["stats.orgs.count"] = statsQuery.Result.OrgCount
  62. metrics["stats.playlist.count"] = statsQuery.Result.PlaylistCount
  63. metrics["stats.plugins.apps.count"] = len(plugins.Apps)
  64. metrics["stats.plugins.panels.count"] = len(plugins.Panels)
  65. metrics["stats.plugins.datasources.count"] = len(plugins.DataSources)
  66. dsStats := m.GetDataSourceStatsQuery{}
  67. if err := bus.Dispatch(&dsStats); err != nil {
  68. log.Error(3, "Failed to get datasource stats", err)
  69. return
  70. }
  71. // send counters for each data source
  72. // but ignore any custom data sources
  73. // as sending that name could be sensitive information
  74. dsOtherCount := 0
  75. for _, dsStat := range dsStats.Result {
  76. if m.IsKnownDataSourcePlugin(dsStat.Type) {
  77. metrics["stats.ds."+dsStat.Type+".count"] = dsStat.Count
  78. } else {
  79. dsOtherCount += dsStat.Count
  80. }
  81. }
  82. metrics["stats.ds.other.count"] = dsOtherCount
  83. out, _ := json.MarshalIndent(report, "", " ")
  84. data := bytes.NewBuffer(out)
  85. client := http.Client{Timeout: time.Duration(5 * time.Second)}
  86. go client.Post("https://stats.grafana.org/grafana-usage-report", "application/json", data)
  87. }