metrics.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package metrics
  2. import (
  3. "runtime"
  4. "github.com/grafana/grafana/pkg/setting"
  5. "github.com/prometheus/client_golang/prometheus"
  6. )
  7. const exporterName = "grafana"
  8. var (
  9. M_Instance_Start prometheus.Counter
  10. M_Page_Status *prometheus.CounterVec
  11. M_Api_Status *prometheus.CounterVec
  12. M_Proxy_Status *prometheus.CounterVec
  13. M_Http_Request_Total *prometheus.CounterVec
  14. M_Http_Request_Summary *prometheus.SummaryVec
  15. M_Api_User_SignUpStarted prometheus.Counter
  16. M_Api_User_SignUpCompleted prometheus.Counter
  17. M_Api_User_SignUpInvite prometheus.Counter
  18. M_Api_Dashboard_Save prometheus.Summary
  19. M_Api_Dashboard_Get prometheus.Summary
  20. M_Api_Dashboard_Search prometheus.Summary
  21. M_Api_Admin_User_Create prometheus.Counter
  22. M_Api_Login_Post prometheus.Counter
  23. M_Api_Login_OAuth prometheus.Counter
  24. M_Api_Org_Create prometheus.Counter
  25. M_Api_Dashboard_Snapshot_Create prometheus.Counter
  26. M_Api_Dashboard_Snapshot_External prometheus.Counter
  27. M_Api_Dashboard_Snapshot_Get prometheus.Counter
  28. M_Api_Dashboard_Insert prometheus.Counter
  29. M_Alerting_Result_State *prometheus.CounterVec
  30. M_Alerting_Notification_Sent *prometheus.CounterVec
  31. M_Aws_CloudWatch_GetMetricStatistics prometheus.Counter
  32. M_Aws_CloudWatch_ListMetrics prometheus.Counter
  33. M_Aws_CloudWatch_GetMetricData prometheus.Counter
  34. M_DB_DataSource_QueryById prometheus.Counter
  35. // Timers
  36. M_DataSource_ProxyReq_Timer prometheus.Summary
  37. M_Alerting_Execution_Time prometheus.Summary
  38. )
  39. // StatTotals
  40. var (
  41. M_Alerting_Active_Alerts prometheus.Gauge
  42. M_StatTotal_Dashboards prometheus.Gauge
  43. M_StatTotal_Users prometheus.Gauge
  44. M_StatActive_Users prometheus.Gauge
  45. M_StatTotal_Orgs prometheus.Gauge
  46. M_StatTotal_Playlists prometheus.Gauge
  47. StatsTotalViewers prometheus.Gauge
  48. StatsTotalEditors prometheus.Gauge
  49. StatsTotalAdmins prometheus.Gauge
  50. StatsTotalActiveViewers prometheus.Gauge
  51. StatsTotalActiveEditors prometheus.Gauge
  52. StatsTotalActiveAdmins prometheus.Gauge
  53. // M_Grafana_Version is a gauge that contains build info about this binary
  54. //
  55. // Deprecated: use M_Grafana_Build_Version instead.
  56. M_Grafana_Version *prometheus.GaugeVec
  57. // grafanaBuildVersion is a gauge that contains build info about this binary
  58. grafanaBuildVersion *prometheus.GaugeVec
  59. )
  60. func init() {
  61. M_Instance_Start = prometheus.NewCounter(prometheus.CounterOpts{
  62. Name: "instance_start_total",
  63. Help: "counter for started instances",
  64. Namespace: exporterName,
  65. })
  66. httpStatusCodes := []string{"200", "404", "500", "unknown"}
  67. M_Page_Status = newCounterVecStartingAtZero(
  68. prometheus.CounterOpts{
  69. Name: "page_response_status_total",
  70. Help: "page http response status",
  71. Namespace: exporterName,
  72. }, []string{"code"}, httpStatusCodes...)
  73. M_Api_Status = newCounterVecStartingAtZero(
  74. prometheus.CounterOpts{
  75. Name: "api_response_status_total",
  76. Help: "api http response status",
  77. Namespace: exporterName,
  78. }, []string{"code"}, httpStatusCodes...)
  79. M_Proxy_Status = newCounterVecStartingAtZero(
  80. prometheus.CounterOpts{
  81. Name: "proxy_response_status_total",
  82. Help: "proxy http response status",
  83. Namespace: exporterName,
  84. }, []string{"code"}, httpStatusCodes...)
  85. M_Http_Request_Total = prometheus.NewCounterVec(
  86. prometheus.CounterOpts{
  87. Name: "http_request_total",
  88. Help: "http request counter",
  89. },
  90. []string{"handler", "statuscode", "method"},
  91. )
  92. M_Http_Request_Summary = prometheus.NewSummaryVec(
  93. prometheus.SummaryOpts{
  94. Name: "http_request_duration_milliseconds",
  95. Help: "http request summary",
  96. },
  97. []string{"handler", "statuscode", "method"},
  98. )
  99. M_Api_User_SignUpStarted = newCounterStartingAtZero(prometheus.CounterOpts{
  100. Name: "api_user_signup_started_total",
  101. Help: "amount of users who started the signup flow",
  102. Namespace: exporterName,
  103. })
  104. M_Api_User_SignUpCompleted = newCounterStartingAtZero(prometheus.CounterOpts{
  105. Name: "api_user_signup_completed_total",
  106. Help: "amount of users who completed the signup flow",
  107. Namespace: exporterName,
  108. })
  109. M_Api_User_SignUpInvite = newCounterStartingAtZero(prometheus.CounterOpts{
  110. Name: "api_user_signup_invite_total",
  111. Help: "amount of users who have been invited",
  112. Namespace: exporterName,
  113. })
  114. M_Api_Dashboard_Save = prometheus.NewSummary(prometheus.SummaryOpts{
  115. Name: "api_dashboard_save_milliseconds",
  116. Help: "summary for dashboard save duration",
  117. Namespace: exporterName,
  118. })
  119. M_Api_Dashboard_Get = prometheus.NewSummary(prometheus.SummaryOpts{
  120. Name: "api_dashboard_get_milliseconds",
  121. Help: "summary for dashboard get duration",
  122. Namespace: exporterName,
  123. })
  124. M_Api_Dashboard_Search = prometheus.NewSummary(prometheus.SummaryOpts{
  125. Name: "api_dashboard_search_milliseconds",
  126. Help: "summary for dashboard search duration",
  127. Namespace: exporterName,
  128. })
  129. M_Api_Admin_User_Create = newCounterStartingAtZero(prometheus.CounterOpts{
  130. Name: "api_admin_user_created_total",
  131. Help: "api admin user created counter",
  132. Namespace: exporterName,
  133. })
  134. M_Api_Login_Post = newCounterStartingAtZero(prometheus.CounterOpts{
  135. Name: "api_login_post_total",
  136. Help: "api login post counter",
  137. Namespace: exporterName,
  138. })
  139. M_Api_Login_OAuth = newCounterStartingAtZero(prometheus.CounterOpts{
  140. Name: "api_login_oauth_total",
  141. Help: "api login oauth counter",
  142. Namespace: exporterName,
  143. })
  144. M_Api_Org_Create = newCounterStartingAtZero(prometheus.CounterOpts{
  145. Name: "api_org_create_total",
  146. Help: "api org created counter",
  147. Namespace: exporterName,
  148. })
  149. M_Api_Dashboard_Snapshot_Create = newCounterStartingAtZero(prometheus.CounterOpts{
  150. Name: "api_dashboard_snapshot_create_total",
  151. Help: "dashboard snapshots created",
  152. Namespace: exporterName,
  153. })
  154. M_Api_Dashboard_Snapshot_External = newCounterStartingAtZero(prometheus.CounterOpts{
  155. Name: "api_dashboard_snapshot_external_total",
  156. Help: "external dashboard snapshots created",
  157. Namespace: exporterName,
  158. })
  159. M_Api_Dashboard_Snapshot_Get = newCounterStartingAtZero(prometheus.CounterOpts{
  160. Name: "api_dashboard_snapshot_get_total",
  161. Help: "loaded dashboards",
  162. Namespace: exporterName,
  163. })
  164. M_Api_Dashboard_Insert = newCounterStartingAtZero(prometheus.CounterOpts{
  165. Name: "api_models_dashboard_insert_total",
  166. Help: "dashboards inserted ",
  167. Namespace: exporterName,
  168. })
  169. M_Alerting_Result_State = prometheus.NewCounterVec(prometheus.CounterOpts{
  170. Name: "alerting_result_total",
  171. Help: "alert execution result counter",
  172. Namespace: exporterName,
  173. }, []string{"state"})
  174. M_Alerting_Notification_Sent = prometheus.NewCounterVec(prometheus.CounterOpts{
  175. Name: "alerting_notification_sent_total",
  176. Help: "counter for how many alert notifications been sent",
  177. Namespace: exporterName,
  178. }, []string{"type"})
  179. M_Aws_CloudWatch_GetMetricStatistics = newCounterStartingAtZero(prometheus.CounterOpts{
  180. Name: "aws_cloudwatch_get_metric_statistics_total",
  181. Help: "counter for getting metric statistics from aws",
  182. Namespace: exporterName,
  183. })
  184. M_Aws_CloudWatch_ListMetrics = newCounterStartingAtZero(prometheus.CounterOpts{
  185. Name: "aws_cloudwatch_list_metrics_total",
  186. Help: "counter for getting list of metrics from aws",
  187. Namespace: exporterName,
  188. })
  189. M_Aws_CloudWatch_GetMetricData = newCounterStartingAtZero(prometheus.CounterOpts{
  190. Name: "aws_cloudwatch_get_metric_data_total",
  191. Help: "counter for getting metric data time series from aws",
  192. Namespace: exporterName,
  193. })
  194. M_DB_DataSource_QueryById = newCounterStartingAtZero(prometheus.CounterOpts{
  195. Name: "db_datasource_query_by_id_total",
  196. Help: "counter for getting datasource by id",
  197. Namespace: exporterName,
  198. })
  199. M_DataSource_ProxyReq_Timer = prometheus.NewSummary(prometheus.SummaryOpts{
  200. Name: "api_dataproxy_request_all_milliseconds",
  201. Help: "summary for dataproxy request duration",
  202. Namespace: exporterName,
  203. })
  204. M_Alerting_Execution_Time = prometheus.NewSummary(prometheus.SummaryOpts{
  205. Name: "alerting_execution_time_milliseconds",
  206. Help: "summary of alert exeuction duration",
  207. Namespace: exporterName,
  208. })
  209. M_Alerting_Active_Alerts = prometheus.NewGauge(prometheus.GaugeOpts{
  210. Name: "alerting_active_alerts",
  211. Help: "amount of active alerts",
  212. Namespace: exporterName,
  213. })
  214. M_StatTotal_Dashboards = prometheus.NewGauge(prometheus.GaugeOpts{
  215. Name: "stat_totals_dashboard",
  216. Help: "total amount of dashboards",
  217. Namespace: exporterName,
  218. })
  219. M_StatTotal_Users = prometheus.NewGauge(prometheus.GaugeOpts{
  220. Name: "stat_total_users",
  221. Help: "total amount of users",
  222. Namespace: exporterName,
  223. })
  224. M_StatActive_Users = prometheus.NewGauge(prometheus.GaugeOpts{
  225. Name: "stat_active_users",
  226. Help: "number of active users",
  227. Namespace: exporterName,
  228. })
  229. M_StatTotal_Orgs = prometheus.NewGauge(prometheus.GaugeOpts{
  230. Name: "stat_total_orgs",
  231. Help: "total amount of orgs",
  232. Namespace: exporterName,
  233. })
  234. M_StatTotal_Playlists = prometheus.NewGauge(prometheus.GaugeOpts{
  235. Name: "stat_total_playlists",
  236. Help: "total amount of playlists",
  237. Namespace: exporterName,
  238. })
  239. StatsTotalViewers = prometheus.NewGauge(prometheus.GaugeOpts{
  240. Name: "stat_totals_viewers",
  241. Help: "total amount of viewers",
  242. Namespace: exporterName,
  243. })
  244. StatsTotalEditors = prometheus.NewGauge(prometheus.GaugeOpts{
  245. Name: "stat_totals_editors",
  246. Help: "total amount of editors",
  247. Namespace: exporterName,
  248. })
  249. StatsTotalAdmins = prometheus.NewGauge(prometheus.GaugeOpts{
  250. Name: "stat_totals_admins",
  251. Help: "total amount of admins",
  252. Namespace: exporterName,
  253. })
  254. StatsTotalActiveViewers = prometheus.NewGauge(prometheus.GaugeOpts{
  255. Name: "stat_totals_active_viewers",
  256. Help: "total amount of viewers",
  257. Namespace: exporterName,
  258. })
  259. StatsTotalActiveEditors = prometheus.NewGauge(prometheus.GaugeOpts{
  260. Name: "stat_totals_active_editors",
  261. Help: "total amount of active editors",
  262. Namespace: exporterName,
  263. })
  264. StatsTotalActiveAdmins = prometheus.NewGauge(prometheus.GaugeOpts{
  265. Name: "stat_totals_active_admins",
  266. Help: "total amount of active admins",
  267. Namespace: exporterName,
  268. })
  269. M_Grafana_Version = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  270. Name: "info",
  271. Help: "Information about the Grafana. This metric is deprecated. please use `grafana_build_info`",
  272. Namespace: exporterName,
  273. }, []string{"version"})
  274. grafanaBuildVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  275. Name: "build_info",
  276. Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which Grafana was built.",
  277. Namespace: exporterName,
  278. }, []string{"version", "revision", "branch", "goversion", "edition"})
  279. }
  280. // SetBuildInformation sets the build information for this binary
  281. func SetBuildInformation(version, revision, branch string) {
  282. // We export this info twice for backwards compatibility.
  283. // Once this have been released for some time we should be able to remote `M_Grafana_Version`
  284. // The reason we added a new one is that its common practice in the prometheus community
  285. // to name this metric `*_build_info` so its easy to do aggregation on all programs.
  286. edition := "oss"
  287. if setting.IsEnterprise {
  288. edition = "enterprise"
  289. }
  290. M_Grafana_Version.WithLabelValues(version).Set(1)
  291. grafanaBuildVersion.WithLabelValues(version, revision, branch, runtime.Version(), edition).Set(1)
  292. }
  293. func initMetricVars() {
  294. prometheus.MustRegister(
  295. M_Instance_Start,
  296. M_Page_Status,
  297. M_Api_Status,
  298. M_Proxy_Status,
  299. M_Http_Request_Total,
  300. M_Http_Request_Summary,
  301. M_Api_User_SignUpStarted,
  302. M_Api_User_SignUpCompleted,
  303. M_Api_User_SignUpInvite,
  304. M_Api_Dashboard_Save,
  305. M_Api_Dashboard_Get,
  306. M_Api_Dashboard_Search,
  307. M_DataSource_ProxyReq_Timer,
  308. M_Alerting_Execution_Time,
  309. M_Api_Admin_User_Create,
  310. M_Api_Login_Post,
  311. M_Api_Login_OAuth,
  312. M_Api_Org_Create,
  313. M_Api_Dashboard_Snapshot_Create,
  314. M_Api_Dashboard_Snapshot_External,
  315. M_Api_Dashboard_Snapshot_Get,
  316. M_Api_Dashboard_Insert,
  317. M_Alerting_Result_State,
  318. M_Alerting_Notification_Sent,
  319. M_Aws_CloudWatch_GetMetricStatistics,
  320. M_Aws_CloudWatch_ListMetrics,
  321. M_Aws_CloudWatch_GetMetricData,
  322. M_DB_DataSource_QueryById,
  323. M_Alerting_Active_Alerts,
  324. M_StatTotal_Dashboards,
  325. M_StatTotal_Users,
  326. M_StatActive_Users,
  327. M_StatTotal_Orgs,
  328. M_StatTotal_Playlists,
  329. M_Grafana_Version,
  330. StatsTotalViewers,
  331. StatsTotalEditors,
  332. StatsTotalAdmins,
  333. StatsTotalActiveViewers,
  334. StatsTotalActiveEditors,
  335. StatsTotalActiveAdmins,
  336. grafanaBuildVersion)
  337. }
  338. func newCounterVecStartingAtZero(opts prometheus.CounterOpts, labels []string, labelValues ...string) *prometheus.CounterVec {
  339. counter := prometheus.NewCounterVec(opts, labels)
  340. for _, label := range labelValues {
  341. counter.WithLabelValues(label).Add(0)
  342. }
  343. return counter
  344. }
  345. func newCounterStartingAtZero(opts prometheus.CounterOpts, labelValues ...string) prometheus.Counter {
  346. counter := prometheus.NewCounter(opts)
  347. counter.Add(0)
  348. return counter
  349. }