metrics.go 12 KB

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