metrics.go 15 KB

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