publish.go 2.7 KB

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