metrics.go 11 KB

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