metrics.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package metrics
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "runtime"
  7. "strings"
  8. "time"
  9. "github.com/grafana/grafana/pkg/bus"
  10. "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/plugins"
  12. "github.com/grafana/grafana/pkg/setting"
  13. "github.com/prometheus/client_golang/prometheus"
  14. )
  15. const exporterName = "grafana"
  16. var (
  17. M_Instance_Start prometheus.Counter
  18. M_Page_Status *prometheus.CounterVec
  19. M_Api_Status *prometheus.CounterVec
  20. M_Proxy_Status *prometheus.CounterVec
  21. M_Http_Request_Total *prometheus.CounterVec
  22. M_Http_Request_Summary *prometheus.SummaryVec
  23. M_Api_User_SignUpStarted prometheus.Counter
  24. M_Api_User_SignUpCompleted prometheus.Counter
  25. M_Api_User_SignUpInvite prometheus.Counter
  26. M_Api_Dashboard_Save prometheus.Summary
  27. M_Api_Dashboard_Get prometheus.Summary
  28. M_Api_Dashboard_Search prometheus.Summary
  29. M_Api_Admin_User_Create prometheus.Counter
  30. M_Api_Login_Post prometheus.Counter
  31. M_Api_Login_OAuth prometheus.Counter
  32. M_Api_Org_Create prometheus.Counter
  33. M_Api_Dashboard_Snapshot_Create prometheus.Counter
  34. M_Api_Dashboard_Snapshot_External prometheus.Counter
  35. M_Api_Dashboard_Snapshot_Get prometheus.Counter
  36. M_Api_Dashboard_Insert prometheus.Counter
  37. M_Alerting_Result_State *prometheus.CounterVec
  38. M_Alerting_Notification_Sent *prometheus.CounterVec
  39. M_Aws_CloudWatch_GetMetricStatistics prometheus.Counter
  40. M_Aws_CloudWatch_ListMetrics prometheus.Counter
  41. M_Aws_CloudWatch_GetMetricData prometheus.Counter
  42. M_DB_DataSource_QueryById prometheus.Counter
  43. // Timers
  44. M_DataSource_ProxyReq_Timer prometheus.Summary
  45. M_Alerting_Execution_Time prometheus.Summary
  46. // StatTotals
  47. M_Alerting_Active_Alerts prometheus.Gauge
  48. M_StatTotal_Dashboards prometheus.Gauge
  49. M_StatTotal_Users prometheus.Gauge
  50. M_StatActive_Users prometheus.Gauge
  51. M_StatTotal_Orgs prometheus.Gauge
  52. M_StatTotal_Playlists prometheus.Gauge
  53. M_Grafana_Version *prometheus.GaugeVec
  54. )
  55. func newCounterVecStartingAtZero(opts prometheus.CounterOpts, labels []string, labelValues ...string) *prometheus.CounterVec {
  56. counter := prometheus.NewCounterVec(opts, labels)
  57. for _, label := range labelValues {
  58. counter.WithLabelValues(label).Add(0)
  59. }
  60. return counter
  61. }
  62. func newCounterStartingAtZero(opts prometheus.CounterOpts, labelValues ...string) prometheus.Counter {
  63. counter := prometheus.NewCounter(opts)
  64. counter.Add(0)
  65. return counter
  66. }
  67. func init() {
  68. M_Instance_Start = prometheus.NewCounter(prometheus.CounterOpts{
  69. Name: "instance_start_total",
  70. Help: "counter for started instances",
  71. Namespace: exporterName,
  72. })
  73. httpStatusCodes := []string{"200", "404", "500", "unknown"}
  74. M_Page_Status = newCounterVecStartingAtZero(
  75. prometheus.CounterOpts{
  76. Name: "page_response_status_total",
  77. Help: "page http response status",
  78. Namespace: exporterName,
  79. }, []string{"code"}, httpStatusCodes...)
  80. M_Api_Status = newCounterVecStartingAtZero(
  81. prometheus.CounterOpts{
  82. Name: "api_response_status_total",
  83. Help: "api http response status",
  84. Namespace: exporterName,
  85. }, []string{"code"}, httpStatusCodes...)
  86. M_Proxy_Status = newCounterVecStartingAtZero(
  87. prometheus.CounterOpts{
  88. Name: "proxy_response_status_total",
  89. Help: "proxy http response status",
  90. Namespace: exporterName,
  91. }, []string{"code"}, httpStatusCodes...)
  92. M_Http_Request_Total = prometheus.NewCounterVec(
  93. prometheus.CounterOpts{
  94. Name: "http_request_total",
  95. Help: "http request counter",
  96. },
  97. []string{"handler", "statuscode", "method"},
  98. )
  99. M_Http_Request_Summary = prometheus.NewSummaryVec(
  100. prometheus.SummaryOpts{
  101. Name: "http_request_duration_milliseconds",
  102. Help: "http request summary",
  103. },
  104. []string{"handler", "statuscode", "method"},
  105. )
  106. M_Api_User_SignUpStarted = newCounterStartingAtZero(prometheus.CounterOpts{
  107. Name: "api_user_signup_started_total",
  108. Help: "amount of users who started the signup flow",
  109. Namespace: exporterName,
  110. })
  111. M_Api_User_SignUpCompleted = newCounterStartingAtZero(prometheus.CounterOpts{
  112. Name: "api_user_signup_completed_total",
  113. Help: "amount of users who completed the signup flow",
  114. Namespace: exporterName,
  115. })
  116. M_Api_User_SignUpInvite = newCounterStartingAtZero(prometheus.CounterOpts{
  117. Name: "api_user_signup_invite_total",
  118. Help: "amount of users who have been invited",
  119. Namespace: exporterName,
  120. })
  121. M_Api_Dashboard_Save = prometheus.NewSummary(prometheus.SummaryOpts{
  122. Name: "api_dashboard_save_milliseconds",
  123. Help: "summary for dashboard save duration",
  124. Namespace: exporterName,
  125. })
  126. M_Api_Dashboard_Get = prometheus.NewSummary(prometheus.SummaryOpts{
  127. Name: "api_dashboard_get_milliseconds",
  128. Help: "summary for dashboard get duration",
  129. Namespace: exporterName,
  130. })
  131. M_Api_Dashboard_Search = prometheus.NewSummary(prometheus.SummaryOpts{
  132. Name: "api_dashboard_search_milliseconds",
  133. Help: "summary for dashboard search duration",
  134. Namespace: exporterName,
  135. })
  136. M_Api_Admin_User_Create = newCounterStartingAtZero(prometheus.CounterOpts{
  137. Name: "api_admin_user_created_total",
  138. Help: "api admin user created counter",
  139. Namespace: exporterName,
  140. })
  141. M_Api_Login_Post = newCounterStartingAtZero(prometheus.CounterOpts{
  142. Name: "api_login_post_total",
  143. Help: "api login post counter",
  144. Namespace: exporterName,
  145. })
  146. M_Api_Login_OAuth = newCounterStartingAtZero(prometheus.CounterOpts{
  147. Name: "api_login_oauth_total",
  148. Help: "api login oauth counter",
  149. Namespace: exporterName,
  150. })
  151. M_Api_Org_Create = newCounterStartingAtZero(prometheus.CounterOpts{
  152. Name: "api_org_create_total",
  153. Help: "api org created counter",
  154. Namespace: exporterName,
  155. })
  156. M_Api_Dashboard_Snapshot_Create = newCounterStartingAtZero(prometheus.CounterOpts{
  157. Name: "api_dashboard_snapshot_create_total",
  158. Help: "dashboard snapshots created",
  159. Namespace: exporterName,
  160. })
  161. M_Api_Dashboard_Snapshot_External = newCounterStartingAtZero(prometheus.CounterOpts{
  162. Name: "api_dashboard_snapshot_external_total",
  163. Help: "external dashboard snapshots created",
  164. Namespace: exporterName,
  165. })
  166. M_Api_Dashboard_Snapshot_Get = newCounterStartingAtZero(prometheus.CounterOpts{
  167. Name: "api_dashboard_snapshot_get_total",
  168. Help: "loaded dashboards",
  169. Namespace: exporterName,
  170. })
  171. M_Api_Dashboard_Insert = newCounterStartingAtZero(prometheus.CounterOpts{
  172. Name: "api_models_dashboard_insert_total",
  173. Help: "dashboards inserted ",
  174. Namespace: exporterName,
  175. })
  176. M_Alerting_Result_State = prometheus.NewCounterVec(prometheus.CounterOpts{
  177. Name: "alerting_result_total",
  178. Help: "alert execution result counter",
  179. Namespace: exporterName,
  180. }, []string{"state"})
  181. M_Alerting_Notification_Sent = prometheus.NewCounterVec(prometheus.CounterOpts{
  182. Name: "alerting_notification_sent_total",
  183. Help: "counter for how many alert notifications been sent",
  184. Namespace: exporterName,
  185. }, []string{"type"})
  186. M_Aws_CloudWatch_GetMetricStatistics = newCounterStartingAtZero(prometheus.CounterOpts{
  187. Name: "aws_cloudwatch_get_metric_statistics_total",
  188. Help: "counter for getting metric statistics from aws",
  189. Namespace: exporterName,
  190. })
  191. M_Aws_CloudWatch_ListMetrics = newCounterStartingAtZero(prometheus.CounterOpts{
  192. Name: "aws_cloudwatch_list_metrics_total",
  193. Help: "counter for getting list of metrics from aws",
  194. Namespace: exporterName,
  195. })
  196. M_Aws_CloudWatch_GetMetricData = newCounterStartingAtZero(prometheus.CounterOpts{
  197. Name: "aws_cloudwatch_get_metric_data_total",
  198. Help: "counter for getting metric data time series from aws",
  199. Namespace: exporterName,
  200. })
  201. M_DB_DataSource_QueryById = newCounterStartingAtZero(prometheus.CounterOpts{
  202. Name: "db_datasource_query_by_id_total",
  203. Help: "counter for getting datasource by id",
  204. Namespace: exporterName,
  205. })
  206. M_DataSource_ProxyReq_Timer = prometheus.NewSummary(prometheus.SummaryOpts{
  207. Name: "api_dataproxy_request_all_milliseconds",
  208. Help: "summary for dataproxy request duration",
  209. Namespace: exporterName,
  210. })
  211. M_Alerting_Execution_Time = prometheus.NewSummary(prometheus.SummaryOpts{
  212. Name: "alerting_execution_time_milliseconds",
  213. Help: "summary of alert exeuction duration",
  214. Namespace: exporterName,
  215. })
  216. M_Alerting_Active_Alerts = prometheus.NewGauge(prometheus.GaugeOpts{
  217. Name: "alerting_active_alerts",
  218. Help: "amount of active alerts",
  219. Namespace: exporterName,
  220. })
  221. M_StatTotal_Dashboards = prometheus.NewGauge(prometheus.GaugeOpts{
  222. Name: "stat_totals_dashboard",
  223. Help: "total amount of dashboards",
  224. Namespace: exporterName,
  225. })
  226. M_StatTotal_Users = prometheus.NewGauge(prometheus.GaugeOpts{
  227. Name: "stat_total_users",
  228. Help: "total amount of users",
  229. Namespace: exporterName,
  230. })
  231. M_StatActive_Users = prometheus.NewGauge(prometheus.GaugeOpts{
  232. Name: "stat_active_users",
  233. Help: "number of active users",
  234. Namespace: exporterName,
  235. })
  236. M_StatTotal_Orgs = prometheus.NewGauge(prometheus.GaugeOpts{
  237. Name: "stat_total_orgs",
  238. Help: "total amount of orgs",
  239. Namespace: exporterName,
  240. })
  241. M_StatTotal_Playlists = prometheus.NewGauge(prometheus.GaugeOpts{
  242. Name: "stat_total_playlists",
  243. Help: "total amount of playlists",
  244. Namespace: exporterName,
  245. })
  246. M_Grafana_Version = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  247. Name: "info",
  248. Help: "Information about the Grafana",
  249. Namespace: exporterName,
  250. }, []string{"version"})
  251. }
  252. func initMetricVars() {
  253. prometheus.MustRegister(
  254. M_Instance_Start,
  255. M_Page_Status,
  256. M_Api_Status,
  257. M_Proxy_Status,
  258. M_Http_Request_Total,
  259. M_Http_Request_Summary,
  260. M_Api_User_SignUpStarted,
  261. M_Api_User_SignUpCompleted,
  262. M_Api_User_SignUpInvite,
  263. M_Api_Dashboard_Save,
  264. M_Api_Dashboard_Get,
  265. M_Api_Dashboard_Search,
  266. M_DataSource_ProxyReq_Timer,
  267. M_Alerting_Execution_Time,
  268. M_Api_Admin_User_Create,
  269. M_Api_Login_Post,
  270. M_Api_Login_OAuth,
  271. M_Api_Org_Create,
  272. M_Api_Dashboard_Snapshot_Create,
  273. M_Api_Dashboard_Snapshot_External,
  274. M_Api_Dashboard_Snapshot_Get,
  275. M_Api_Dashboard_Insert,
  276. M_Alerting_Result_State,
  277. M_Alerting_Notification_Sent,
  278. M_Aws_CloudWatch_GetMetricStatistics,
  279. M_Aws_CloudWatch_ListMetrics,
  280. M_Aws_CloudWatch_GetMetricData,
  281. M_DB_DataSource_QueryById,
  282. M_Alerting_Active_Alerts,
  283. M_StatTotal_Dashboards,
  284. M_StatTotal_Users,
  285. M_StatActive_Users,
  286. M_StatTotal_Orgs,
  287. M_StatTotal_Playlists,
  288. M_Grafana_Version)
  289. }
  290. func updateTotalStats() {
  291. statsQuery := models.GetSystemStatsQuery{}
  292. if err := bus.Dispatch(&statsQuery); err != nil {
  293. metricsLogger.Error("Failed to get system stats", "error", err)
  294. return
  295. }
  296. M_StatTotal_Dashboards.Set(float64(statsQuery.Result.Dashboards))
  297. M_StatTotal_Users.Set(float64(statsQuery.Result.Users))
  298. M_StatActive_Users.Set(float64(statsQuery.Result.ActiveUsers))
  299. M_StatTotal_Playlists.Set(float64(statsQuery.Result.Playlists))
  300. M_StatTotal_Orgs.Set(float64(statsQuery.Result.Orgs))
  301. }
  302. var usageStatsURL = "https://stats.grafana.org/grafana-usage-report"
  303. func getEdition() string {
  304. if setting.IsEnterprise {
  305. return "enterprise"
  306. } else {
  307. return "oss"
  308. }
  309. }
  310. func sendUsageStats(oauthProviders map[string]bool) {
  311. if !setting.ReportingEnabled {
  312. return
  313. }
  314. metricsLogger.Debug("Sending anonymous usage stats to stats.grafana.org")
  315. version := strings.Replace(setting.BuildVersion, ".", "_", -1)
  316. metrics := map[string]interface{}{}
  317. report := map[string]interface{}{
  318. "version": version,
  319. "metrics": metrics,
  320. "os": runtime.GOOS,
  321. "arch": runtime.GOARCH,
  322. "edition": getEdition(),
  323. }
  324. statsQuery := models.GetSystemStatsQuery{}
  325. if err := bus.Dispatch(&statsQuery); err != nil {
  326. metricsLogger.Error("Failed to get system stats", "error", err)
  327. return
  328. }
  329. metrics["stats.dashboards.count"] = statsQuery.Result.Dashboards
  330. metrics["stats.users.count"] = statsQuery.Result.Users
  331. metrics["stats.orgs.count"] = statsQuery.Result.Orgs
  332. metrics["stats.playlist.count"] = statsQuery.Result.Playlists
  333. metrics["stats.plugins.apps.count"] = len(plugins.Apps)
  334. metrics["stats.plugins.panels.count"] = len(plugins.Panels)
  335. metrics["stats.plugins.datasources.count"] = len(plugins.DataSources)
  336. metrics["stats.alerts.count"] = statsQuery.Result.Alerts
  337. metrics["stats.active_users.count"] = statsQuery.Result.ActiveUsers
  338. metrics["stats.datasources.count"] = statsQuery.Result.Datasources
  339. metrics["stats.stars.count"] = statsQuery.Result.Stars
  340. metrics["stats.folders.count"] = statsQuery.Result.Folders
  341. metrics["stats.dashboard_permissions.count"] = statsQuery.Result.DashboardPermissions
  342. metrics["stats.folder_permissions.count"] = statsQuery.Result.FolderPermissions
  343. metrics["stats.provisioned_dashboards.count"] = statsQuery.Result.ProvisionedDashboards
  344. metrics["stats.snapshots.count"] = statsQuery.Result.Snapshots
  345. metrics["stats.teams.count"] = statsQuery.Result.Teams
  346. dsStats := models.GetDataSourceStatsQuery{}
  347. if err := bus.Dispatch(&dsStats); err != nil {
  348. metricsLogger.Error("Failed to get datasource stats", "error", err)
  349. return
  350. }
  351. // send counters for each data source
  352. // but ignore any custom data sources
  353. // as sending that name could be sensitive information
  354. dsOtherCount := 0
  355. for _, dsStat := range dsStats.Result {
  356. if models.IsKnownDataSourcePlugin(dsStat.Type) {
  357. metrics["stats.ds."+dsStat.Type+".count"] = dsStat.Count
  358. } else {
  359. dsOtherCount += dsStat.Count
  360. }
  361. }
  362. metrics["stats.ds.other.count"] = dsOtherCount
  363. dsAccessStats := models.GetDataSourceAccessStatsQuery{}
  364. if err := bus.Dispatch(&dsAccessStats); err != nil {
  365. metricsLogger.Error("Failed to get datasource access stats", "error", err)
  366. return
  367. }
  368. // send access counters for each data source
  369. // but ignore any custom data sources
  370. // as sending that name could be sensitive information
  371. dsAccessOtherCount := make(map[string]int64)
  372. for _, dsAccessStat := range dsAccessStats.Result {
  373. if dsAccessStat.Access == "" {
  374. continue
  375. }
  376. access := strings.ToLower(dsAccessStat.Access)
  377. if models.IsKnownDataSourcePlugin(dsAccessStat.Type) {
  378. metrics["stats.ds_access."+dsAccessStat.Type+"."+access+".count"] = dsAccessStat.Count
  379. } else {
  380. old := dsAccessOtherCount[access]
  381. dsAccessOtherCount[access] = old + dsAccessStat.Count
  382. }
  383. }
  384. for access, count := range dsAccessOtherCount {
  385. metrics["stats.ds_access.other."+access+".count"] = count
  386. }
  387. anStats := models.GetAlertNotifierUsageStatsQuery{}
  388. if err := bus.Dispatch(&anStats); err != nil {
  389. metricsLogger.Error("Failed to get alert notification stats", "error", err)
  390. return
  391. }
  392. for _, stats := range anStats.Result {
  393. metrics["stats.alert_notifiers."+stats.Type+".count"] = stats.Count
  394. }
  395. authTypes := map[string]bool{}
  396. authTypes["anonymous"] = setting.AnonymousEnabled
  397. authTypes["basic_auth"] = setting.BasicAuthEnabled
  398. authTypes["ldap"] = setting.LdapEnabled
  399. authTypes["auth_proxy"] = setting.AuthProxyEnabled
  400. for provider, enabled := range oauthProviders {
  401. authTypes["oauth_"+provider] = enabled
  402. }
  403. for authType, enabled := range authTypes {
  404. enabledValue := 0
  405. if enabled {
  406. enabledValue = 1
  407. }
  408. metrics["stats.auth_enabled."+authType+".count"] = enabledValue
  409. }
  410. out, _ := json.MarshalIndent(report, "", " ")
  411. data := bytes.NewBuffer(out)
  412. client := http.Client{Timeout: 5 * time.Second}
  413. go client.Post(usageStatsURL, "application/json", data)
  414. }