metrics.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package metrics
  2. import (
  3. "runtime"
  4. "github.com/prometheus/client_golang/prometheus"
  5. "github.com/grafana/grafana/pkg/setting"
  6. )
  7. const exporterName = "grafana"
  8. var (
  9. // MInstanceStart is a metric counter for started instances
  10. MInstanceStart prometheus.Counter
  11. // MPageStatus is a metric page http response status
  12. MPageStatus *prometheus.CounterVec
  13. // MApiStatus is a metric api http response status
  14. MApiStatus *prometheus.CounterVec
  15. // MProxyStatus is a metric proxy http response status
  16. MProxyStatus *prometheus.CounterVec
  17. // MHttpRequestTotal is a metric http request counter
  18. MHttpRequestTotal *prometheus.CounterVec
  19. // MHttpRequestSummary is a metric http request summary
  20. MHttpRequestSummary *prometheus.SummaryVec
  21. // MApiUserSignUpStarted is a metric amount of users who started the signup flow
  22. MApiUserSignUpStarted prometheus.Counter
  23. // MApiUserSignUpCompleted is a metric amount of users who completed the signup flow
  24. MApiUserSignUpCompleted prometheus.Counter
  25. // MApiUserSignUpInvite is a metric amount of users who have been invited
  26. MApiUserSignUpInvite prometheus.Counter
  27. // MApiDashboardSave is a metric summary for dashboard save duration
  28. MApiDashboardSave prometheus.Summary
  29. // MApiDashboardGet is a metric summary for dashboard get duration
  30. MApiDashboardGet prometheus.Summary
  31. // MApiDashboardSearch is a metric summary for dashboard search duration
  32. MApiDashboardSearch prometheus.Summary
  33. // MApiAdminUserCreate is a metric api admin user created counter
  34. MApiAdminUserCreate prometheus.Counter
  35. // MApiLoginPost is a metric api login post counter
  36. MApiLoginPost prometheus.Counter
  37. // MApiLoginOAuth is a metric api login oauth counter
  38. MApiLoginOAuth prometheus.Counter
  39. // MApiLoginSAML is a metric api login SAML counter
  40. MApiLoginSAML prometheus.Counter
  41. // MApiOrgCreate is a metric api org created counter
  42. MApiOrgCreate prometheus.Counter
  43. // MApiDashboardSnapshotCreate is a metric dashboard snapshots created
  44. MApiDashboardSnapshotCreate prometheus.Counter
  45. // MApiDashboardSnapshotExternal is a metric external dashboard snapshots created
  46. MApiDashboardSnapshotExternal prometheus.Counter
  47. // MApiDashboardSnapshotGet is a metric loaded dashboards
  48. MApiDashboardSnapshotGet prometheus.Counter
  49. // MApiDashboardInsert is a metric dashboards inserted
  50. MApiDashboardInsert prometheus.Counter
  51. // MAlertingResultState is a metric alert execution result counter
  52. MAlertingResultState *prometheus.CounterVec
  53. // MAlertingNotificationSent is a metric counter for how many alert notifications been sent
  54. MAlertingNotificationSent *prometheus.CounterVec
  55. // MAlertingNotificationSent is a metric counter for how many alert notifications that failed
  56. MAlertingNotificationFailed *prometheus.CounterVec
  57. // MAwsCloudWatchGetMetricStatistics is a metric counter for getting metric statistics from aws
  58. MAwsCloudWatchGetMetricStatistics prometheus.Counter
  59. // MAwsCloudWatchListMetrics is a metric counter for getting list of metrics from aws
  60. MAwsCloudWatchListMetrics prometheus.Counter
  61. // MAwsCloudWatchGetMetricData is a metric counter for getting metric data time series from aws
  62. MAwsCloudWatchGetMetricData prometheus.Counter
  63. // MDBDataSourceQueryByID is a metric counter for getting datasource by id
  64. MDBDataSourceQueryByID prometheus.Counter
  65. // LDAPUsersSyncExecutionTime is a metric summary for LDAP users sync execution duration
  66. LDAPUsersSyncExecutionTime prometheus.Summary
  67. )
  68. // Timers
  69. var (
  70. // MDataSourceProxyReqTimer is a metric summary for dataproxy request duration
  71. MDataSourceProxyReqTimer prometheus.Summary
  72. // MAlertingExecutionTime is a metric summary of alert exeuction duration
  73. MAlertingExecutionTime prometheus.Summary
  74. )
  75. // StatTotals
  76. var (
  77. // MAlertingActiveAlerts is a metric amount of active alerts
  78. MAlertingActiveAlerts prometheus.Gauge
  79. // MStatTotalDashboards is a metric total amount of dashboards
  80. MStatTotalDashboards prometheus.Gauge
  81. // MStatTotalUsers is a metric total amount of users
  82. MStatTotalUsers prometheus.Gauge
  83. // MStatActiveUsers is a metric number of active users
  84. MStatActiveUsers prometheus.Gauge
  85. // MStatTotalOrgs is a metric total amount of orgs
  86. MStatTotalOrgs prometheus.Gauge
  87. // MStatTotalPlaylists is a metric total amount of playlists
  88. MStatTotalPlaylists prometheus.Gauge
  89. // StatsTotalViewers is a metric total amount of viewers
  90. StatsTotalViewers prometheus.Gauge
  91. // StatsTotalEditors is a metric total amount of editors
  92. StatsTotalEditors prometheus.Gauge
  93. // StatsTotalAdmins is a metric total amount of admins
  94. StatsTotalAdmins prometheus.Gauge
  95. // StatsTotalActiveViewers is a metric total amount of viewers
  96. StatsTotalActiveViewers prometheus.Gauge
  97. // StatsTotalActiveEditors is a metric total amount of active editors
  98. StatsTotalActiveEditors prometheus.Gauge
  99. // StatsTotalActiveAdmins is a metric total amount of active admins
  100. StatsTotalActiveAdmins prometheus.Gauge
  101. // grafanaBuildVersion is a metric with a constant '1' value labeled by version, revision, branch, and goversion from which Grafana was built
  102. grafanaBuildVersion *prometheus.GaugeVec
  103. )
  104. func init() {
  105. httpStatusCodes := []string{"200", "404", "500", "unknown"}
  106. MInstanceStart = prometheus.NewCounter(prometheus.CounterOpts{
  107. Name: "instance_start_total",
  108. Help: "counter for started instances",
  109. Namespace: exporterName,
  110. })
  111. MPageStatus = newCounterVecStartingAtZero(
  112. prometheus.CounterOpts{
  113. Name: "page_response_status_total",
  114. Help: "page http response status",
  115. Namespace: exporterName,
  116. }, []string{"code"}, httpStatusCodes...)
  117. MApiStatus = newCounterVecStartingAtZero(
  118. prometheus.CounterOpts{
  119. Name: "api_response_status_total",
  120. Help: "api http response status",
  121. Namespace: exporterName,
  122. }, []string{"code"}, httpStatusCodes...)
  123. MProxyStatus = newCounterVecStartingAtZero(
  124. prometheus.CounterOpts{
  125. Name: "proxy_response_status_total",
  126. Help: "proxy http response status",
  127. Namespace: exporterName,
  128. }, []string{"code"}, httpStatusCodes...)
  129. MHttpRequestTotal = prometheus.NewCounterVec(
  130. prometheus.CounterOpts{
  131. Name: "http_request_total",
  132. Help: "http request counter",
  133. },
  134. []string{"handler", "statuscode", "method"},
  135. )
  136. MHttpRequestSummary = prometheus.NewSummaryVec(
  137. prometheus.SummaryOpts{
  138. Name: "http_request_duration_milliseconds",
  139. Help: "http request summary",
  140. },
  141. []string{"handler", "statuscode", "method"},
  142. )
  143. MApiUserSignUpStarted = newCounterStartingAtZero(prometheus.CounterOpts{
  144. Name: "api_user_signup_started_total",
  145. Help: "amount of users who started the signup flow",
  146. Namespace: exporterName,
  147. })
  148. MApiUserSignUpCompleted = newCounterStartingAtZero(prometheus.CounterOpts{
  149. Name: "api_user_signup_completed_total",
  150. Help: "amount of users who completed the signup flow",
  151. Namespace: exporterName,
  152. })
  153. MApiUserSignUpInvite = newCounterStartingAtZero(prometheus.CounterOpts{
  154. Name: "api_user_signup_invite_total",
  155. Help: "amount of users who have been invited",
  156. Namespace: exporterName,
  157. })
  158. MApiDashboardSave = prometheus.NewSummary(prometheus.SummaryOpts{
  159. Name: "api_dashboard_save_milliseconds",
  160. Help: "summary for dashboard save duration",
  161. Namespace: exporterName,
  162. })
  163. MApiDashboardGet = prometheus.NewSummary(prometheus.SummaryOpts{
  164. Name: "api_dashboard_get_milliseconds",
  165. Help: "summary for dashboard get duration",
  166. Namespace: exporterName,
  167. })
  168. MApiDashboardSearch = prometheus.NewSummary(prometheus.SummaryOpts{
  169. Name: "api_dashboard_search_milliseconds",
  170. Help: "summary for dashboard search duration",
  171. Namespace: exporterName,
  172. })
  173. MApiAdminUserCreate = newCounterStartingAtZero(prometheus.CounterOpts{
  174. Name: "api_admin_user_created_total",
  175. Help: "api admin user created counter",
  176. Namespace: exporterName,
  177. })
  178. MApiLoginPost = newCounterStartingAtZero(prometheus.CounterOpts{
  179. Name: "api_login_post_total",
  180. Help: "api login post counter",
  181. Namespace: exporterName,
  182. })
  183. MApiLoginOAuth = newCounterStartingAtZero(prometheus.CounterOpts{
  184. Name: "api_login_oauth_total",
  185. Help: "api login oauth counter",
  186. Namespace: exporterName,
  187. })
  188. MApiLoginSAML = newCounterStartingAtZero(prometheus.CounterOpts{
  189. Name: "api_login_saml_total",
  190. Help: "api login saml counter",
  191. Namespace: exporterName,
  192. })
  193. MApiOrgCreate = newCounterStartingAtZero(prometheus.CounterOpts{
  194. Name: "api_org_create_total",
  195. Help: "api org created counter",
  196. Namespace: exporterName,
  197. })
  198. MApiDashboardSnapshotCreate = newCounterStartingAtZero(prometheus.CounterOpts{
  199. Name: "api_dashboard_snapshot_create_total",
  200. Help: "dashboard snapshots created",
  201. Namespace: exporterName,
  202. })
  203. MApiDashboardSnapshotExternal = newCounterStartingAtZero(prometheus.CounterOpts{
  204. Name: "api_dashboard_snapshot_external_total",
  205. Help: "external dashboard snapshots created",
  206. Namespace: exporterName,
  207. })
  208. MApiDashboardSnapshotGet = newCounterStartingAtZero(prometheus.CounterOpts{
  209. Name: "api_dashboard_snapshot_get_total",
  210. Help: "loaded dashboards",
  211. Namespace: exporterName,
  212. })
  213. MApiDashboardInsert = newCounterStartingAtZero(prometheus.CounterOpts{
  214. Name: "api_models_dashboard_insert_total",
  215. Help: "dashboards inserted ",
  216. Namespace: exporterName,
  217. })
  218. MAlertingResultState = prometheus.NewCounterVec(prometheus.CounterOpts{
  219. Name: "alerting_result_total",
  220. Help: "alert execution result counter",
  221. Namespace: exporterName,
  222. }, []string{"state"})
  223. MAlertingNotificationSent = prometheus.NewCounterVec(prometheus.CounterOpts{
  224. Name: "alerting_notification_sent_total",
  225. Help: "counter for how many alert notifications have been sent",
  226. Namespace: exporterName,
  227. }, []string{"type"})
  228. MAlertingNotificationFailed = prometheus.NewCounterVec(prometheus.CounterOpts{
  229. Name: "alerting_notification_failed_total",
  230. Help: "counter for how many alert notifications have failed",
  231. Namespace: exporterName,
  232. }, []string{"type"})
  233. MAwsCloudWatchGetMetricStatistics = newCounterStartingAtZero(prometheus.CounterOpts{
  234. Name: "aws_cloudwatch_get_metric_statistics_total",
  235. Help: "counter for getting metric statistics from aws",
  236. Namespace: exporterName,
  237. })
  238. MAwsCloudWatchListMetrics = newCounterStartingAtZero(prometheus.CounterOpts{
  239. Name: "aws_cloudwatch_list_metrics_total",
  240. Help: "counter for getting list of metrics from aws",
  241. Namespace: exporterName,
  242. })
  243. MAwsCloudWatchGetMetricData = newCounterStartingAtZero(prometheus.CounterOpts{
  244. Name: "aws_cloudwatch_get_metric_data_total",
  245. Help: "counter for getting metric data time series from aws",
  246. Namespace: exporterName,
  247. })
  248. MDBDataSourceQueryByID = newCounterStartingAtZero(prometheus.CounterOpts{
  249. Name: "db_datasource_query_by_id_total",
  250. Help: "counter for getting datasource by id",
  251. Namespace: exporterName,
  252. })
  253. LDAPUsersSyncExecutionTime = prometheus.NewSummary(prometheus.SummaryOpts{
  254. Name: "ldap_users_sync_execution_time",
  255. Help: "summary for LDAP users sync execution duration",
  256. Namespace: exporterName,
  257. })
  258. MDataSourceProxyReqTimer = prometheus.NewSummary(prometheus.SummaryOpts{
  259. Name: "api_dataproxy_request_all_milliseconds",
  260. Help: "summary for dataproxy request duration",
  261. Namespace: exporterName,
  262. })
  263. MAlertingExecutionTime = prometheus.NewSummary(prometheus.SummaryOpts{
  264. Name: "alerting_execution_time_milliseconds",
  265. Help: "summary of alert exeuction duration",
  266. Namespace: exporterName,
  267. })
  268. MAlertingActiveAlerts = prometheus.NewGauge(prometheus.GaugeOpts{
  269. Name: "alerting_active_alerts",
  270. Help: "amount of active alerts",
  271. Namespace: exporterName,
  272. })
  273. MStatTotalDashboards = prometheus.NewGauge(prometheus.GaugeOpts{
  274. Name: "stat_totals_dashboard",
  275. Help: "total amount of dashboards",
  276. Namespace: exporterName,
  277. })
  278. MStatTotalUsers = prometheus.NewGauge(prometheus.GaugeOpts{
  279. Name: "stat_total_users",
  280. Help: "total amount of users",
  281. Namespace: exporterName,
  282. })
  283. MStatActiveUsers = prometheus.NewGauge(prometheus.GaugeOpts{
  284. Name: "stat_active_users",
  285. Help: "number of active users",
  286. Namespace: exporterName,
  287. })
  288. MStatTotalOrgs = prometheus.NewGauge(prometheus.GaugeOpts{
  289. Name: "stat_total_orgs",
  290. Help: "total amount of orgs",
  291. Namespace: exporterName,
  292. })
  293. MStatTotalPlaylists = prometheus.NewGauge(prometheus.GaugeOpts{
  294. Name: "stat_total_playlists",
  295. Help: "total amount of playlists",
  296. Namespace: exporterName,
  297. })
  298. StatsTotalViewers = prometheus.NewGauge(prometheus.GaugeOpts{
  299. Name: "stat_totals_viewers",
  300. Help: "total amount of viewers",
  301. Namespace: exporterName,
  302. })
  303. StatsTotalEditors = prometheus.NewGauge(prometheus.GaugeOpts{
  304. Name: "stat_totals_editors",
  305. Help: "total amount of editors",
  306. Namespace: exporterName,
  307. })
  308. StatsTotalAdmins = prometheus.NewGauge(prometheus.GaugeOpts{
  309. Name: "stat_totals_admins",
  310. Help: "total amount of admins",
  311. Namespace: exporterName,
  312. })
  313. StatsTotalActiveViewers = prometheus.NewGauge(prometheus.GaugeOpts{
  314. Name: "stat_totals_active_viewers",
  315. Help: "total amount of viewers",
  316. Namespace: exporterName,
  317. })
  318. StatsTotalActiveEditors = prometheus.NewGauge(prometheus.GaugeOpts{
  319. Name: "stat_totals_active_editors",
  320. Help: "total amount of active editors",
  321. Namespace: exporterName,
  322. })
  323. StatsTotalActiveAdmins = prometheus.NewGauge(prometheus.GaugeOpts{
  324. Name: "stat_totals_active_admins",
  325. Help: "total amount of active admins",
  326. Namespace: exporterName,
  327. })
  328. grafanaBuildVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  329. Name: "build_info",
  330. Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which Grafana was built",
  331. Namespace: exporterName,
  332. }, []string{"version", "revision", "branch", "goversion", "edition"})
  333. }
  334. // SetBuildInformation sets the build information for this binary
  335. func SetBuildInformation(version, revision, branch string) {
  336. edition := "oss"
  337. if setting.IsEnterprise {
  338. edition = "enterprise"
  339. }
  340. grafanaBuildVersion.WithLabelValues(version, revision, branch, runtime.Version(), edition).Set(1)
  341. }
  342. func initMetricVars() {
  343. prometheus.MustRegister(
  344. MInstanceStart,
  345. MPageStatus,
  346. MApiStatus,
  347. MProxyStatus,
  348. MHttpRequestTotal,
  349. MHttpRequestSummary,
  350. MApiUserSignUpStarted,
  351. MApiUserSignUpCompleted,
  352. MApiUserSignUpInvite,
  353. MApiDashboardSave,
  354. MApiDashboardGet,
  355. MApiDashboardSearch,
  356. MDataSourceProxyReqTimer,
  357. MAlertingExecutionTime,
  358. MApiAdminUserCreate,
  359. MApiLoginPost,
  360. MApiLoginOAuth,
  361. MApiLoginSAML,
  362. MApiOrgCreate,
  363. MApiDashboardSnapshotCreate,
  364. MApiDashboardSnapshotExternal,
  365. MApiDashboardSnapshotGet,
  366. MApiDashboardInsert,
  367. MAlertingResultState,
  368. MAlertingNotificationSent,
  369. MAlertingNotificationFailed,
  370. MAwsCloudWatchGetMetricStatistics,
  371. MAwsCloudWatchListMetrics,
  372. MAwsCloudWatchGetMetricData,
  373. MDBDataSourceQueryByID,
  374. LDAPUsersSyncExecutionTime,
  375. MAlertingActiveAlerts,
  376. MStatTotalDashboards,
  377. MStatTotalUsers,
  378. MStatActiveUsers,
  379. MStatTotalOrgs,
  380. MStatTotalPlaylists,
  381. StatsTotalViewers,
  382. StatsTotalEditors,
  383. StatsTotalAdmins,
  384. StatsTotalActiveViewers,
  385. StatsTotalActiveEditors,
  386. StatsTotalActiveAdmins,
  387. grafanaBuildVersion,
  388. )
  389. }
  390. func newCounterVecStartingAtZero(opts prometheus.CounterOpts, labels []string, labelValues ...string) *prometheus.CounterVec {
  391. counter := prometheus.NewCounterVec(opts, labels)
  392. for _, label := range labelValues {
  393. counter.WithLabelValues(label).Add(0)
  394. }
  395. return counter
  396. }
  397. func newCounterStartingAtZero(opts prometheus.CounterOpts, labelValues ...string) prometheus.Counter {
  398. counter := prometheus.NewCounter(opts)
  399. counter.Add(0)
  400. return counter
  401. }