You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
6.5 KiB

  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package metrics
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "github.com/prometheus/client_golang/prometheus"
  8. )
  9. const namespace = "gitea_"
  10. // Collector implements the prometheus.Collector interface and
  11. // exposes gitea metrics for prometheus
  12. type Collector struct {
  13. Accesses *prometheus.Desc
  14. Actions *prometheus.Desc
  15. Attachments *prometheus.Desc
  16. Comments *prometheus.Desc
  17. Follows *prometheus.Desc
  18. HookTasks *prometheus.Desc
  19. Issues *prometheus.Desc
  20. Labels *prometheus.Desc
  21. LoginSources *prometheus.Desc
  22. Milestones *prometheus.Desc
  23. Mirrors *prometheus.Desc
  24. Oauths *prometheus.Desc
  25. Organizations *prometheus.Desc
  26. PublicKeys *prometheus.Desc
  27. Releases *prometheus.Desc
  28. Repositories *prometheus.Desc
  29. Stars *prometheus.Desc
  30. Teams *prometheus.Desc
  31. UpdateTasks *prometheus.Desc
  32. Users *prometheus.Desc
  33. Watches *prometheus.Desc
  34. Webhooks *prometheus.Desc
  35. }
  36. // NewCollector returns a new Collector with all prometheus.Desc initialized
  37. func NewCollector() Collector {
  38. return Collector{
  39. Accesses: prometheus.NewDesc(
  40. namespace+"accesses",
  41. "Number of Accesses",
  42. nil, nil,
  43. ),
  44. Actions: prometheus.NewDesc(
  45. namespace+"actions",
  46. "Number of Actions",
  47. nil, nil,
  48. ),
  49. Attachments: prometheus.NewDesc(
  50. namespace+"attachments",
  51. "Number of Attachments",
  52. nil, nil,
  53. ),
  54. Comments: prometheus.NewDesc(
  55. namespace+"comments",
  56. "Number of Comments",
  57. nil, nil,
  58. ),
  59. Follows: prometheus.NewDesc(
  60. namespace+"follows",
  61. "Number of Follows",
  62. nil, nil,
  63. ),
  64. HookTasks: prometheus.NewDesc(
  65. namespace+"hooktasks",
  66. "Number of HookTasks",
  67. nil, nil,
  68. ),
  69. Issues: prometheus.NewDesc(
  70. namespace+"issues",
  71. "Number of Issues",
  72. nil, nil,
  73. ),
  74. Labels: prometheus.NewDesc(
  75. namespace+"labels",
  76. "Number of Labels",
  77. nil, nil,
  78. ),
  79. LoginSources: prometheus.NewDesc(
  80. namespace+"loginsources",
  81. "Number of LoginSources",
  82. nil, nil,
  83. ),
  84. Milestones: prometheus.NewDesc(
  85. namespace+"milestones",
  86. "Number of Milestones",
  87. nil, nil,
  88. ),
  89. Mirrors: prometheus.NewDesc(
  90. namespace+"mirrors",
  91. "Number of Mirrors",
  92. nil, nil,
  93. ),
  94. Oauths: prometheus.NewDesc(
  95. namespace+"oauths",
  96. "Number of Oauths",
  97. nil, nil,
  98. ),
  99. Organizations: prometheus.NewDesc(
  100. namespace+"organizations",
  101. "Number of Organizations",
  102. nil, nil,
  103. ),
  104. PublicKeys: prometheus.NewDesc(
  105. namespace+"publickeys",
  106. "Number of PublicKeys",
  107. nil, nil,
  108. ),
  109. Releases: prometheus.NewDesc(
  110. namespace+"releases",
  111. "Number of Releases",
  112. nil, nil,
  113. ),
  114. Repositories: prometheus.NewDesc(
  115. namespace+"repositories",
  116. "Number of Repositories",
  117. nil, nil,
  118. ),
  119. Stars: prometheus.NewDesc(
  120. namespace+"stars",
  121. "Number of Stars",
  122. nil, nil,
  123. ),
  124. Teams: prometheus.NewDesc(
  125. namespace+"teams",
  126. "Number of Teams",
  127. nil, nil,
  128. ),
  129. UpdateTasks: prometheus.NewDesc(
  130. namespace+"updatetasks",
  131. "Number of UpdateTasks",
  132. nil, nil,
  133. ),
  134. Users: prometheus.NewDesc(
  135. namespace+"users",
  136. "Number of Users",
  137. nil, nil,
  138. ),
  139. Watches: prometheus.NewDesc(
  140. namespace+"watches",
  141. "Number of Watches",
  142. nil, nil,
  143. ),
  144. Webhooks: prometheus.NewDesc(
  145. namespace+"webhooks",
  146. "Number of Webhooks",
  147. nil, nil,
  148. ),
  149. }
  150. }
  151. // Describe returns all possible prometheus.Desc
  152. func (c Collector) Describe(ch chan<- *prometheus.Desc) {
  153. ch <- c.Accesses
  154. ch <- c.Actions
  155. ch <- c.Attachments
  156. ch <- c.Comments
  157. ch <- c.Follows
  158. ch <- c.HookTasks
  159. ch <- c.Issues
  160. ch <- c.Labels
  161. ch <- c.LoginSources
  162. ch <- c.Milestones
  163. ch <- c.Mirrors
  164. ch <- c.Oauths
  165. ch <- c.Organizations
  166. ch <- c.PublicKeys
  167. ch <- c.Releases
  168. ch <- c.Repositories
  169. ch <- c.Stars
  170. ch <- c.Teams
  171. ch <- c.UpdateTasks
  172. ch <- c.Users
  173. ch <- c.Watches
  174. ch <- c.Webhooks
  175. }
  176. // Collect returns the metrics with values
  177. func (c Collector) Collect(ch chan<- prometheus.Metric) {
  178. stats := models.GetStatistic()
  179. ch <- prometheus.MustNewConstMetric(
  180. c.Accesses,
  181. prometheus.GaugeValue,
  182. float64(stats.Counter.Access),
  183. )
  184. ch <- prometheus.MustNewConstMetric(
  185. c.Actions,
  186. prometheus.GaugeValue,
  187. float64(stats.Counter.Action),
  188. )
  189. ch <- prometheus.MustNewConstMetric(
  190. c.Attachments,
  191. prometheus.GaugeValue,
  192. float64(stats.Counter.Attachment),
  193. )
  194. ch <- prometheus.MustNewConstMetric(
  195. c.Comments,
  196. prometheus.GaugeValue,
  197. float64(stats.Counter.Comment),
  198. )
  199. ch <- prometheus.MustNewConstMetric(
  200. c.Follows,
  201. prometheus.GaugeValue,
  202. float64(stats.Counter.Follow),
  203. )
  204. ch <- prometheus.MustNewConstMetric(
  205. c.HookTasks,
  206. prometheus.GaugeValue,
  207. float64(stats.Counter.HookTask),
  208. )
  209. ch <- prometheus.MustNewConstMetric(
  210. c.Issues,
  211. prometheus.GaugeValue,
  212. float64(stats.Counter.Issue),
  213. )
  214. ch <- prometheus.MustNewConstMetric(
  215. c.Labels,
  216. prometheus.GaugeValue,
  217. float64(stats.Counter.Label),
  218. )
  219. ch <- prometheus.MustNewConstMetric(
  220. c.LoginSources,
  221. prometheus.GaugeValue,
  222. float64(stats.Counter.LoginSource),
  223. )
  224. ch <- prometheus.MustNewConstMetric(
  225. c.Milestones,
  226. prometheus.GaugeValue,
  227. float64(stats.Counter.Milestone),
  228. )
  229. ch <- prometheus.MustNewConstMetric(
  230. c.Mirrors,
  231. prometheus.GaugeValue,
  232. float64(stats.Counter.Mirror),
  233. )
  234. ch <- prometheus.MustNewConstMetric(
  235. c.Oauths,
  236. prometheus.GaugeValue,
  237. float64(stats.Counter.Oauth),
  238. )
  239. ch <- prometheus.MustNewConstMetric(
  240. c.Organizations,
  241. prometheus.GaugeValue,
  242. float64(stats.Counter.Org),
  243. )
  244. ch <- prometheus.MustNewConstMetric(
  245. c.PublicKeys,
  246. prometheus.GaugeValue,
  247. float64(stats.Counter.PublicKey),
  248. )
  249. ch <- prometheus.MustNewConstMetric(
  250. c.Releases,
  251. prometheus.GaugeValue,
  252. float64(stats.Counter.Release),
  253. )
  254. ch <- prometheus.MustNewConstMetric(
  255. c.Repositories,
  256. prometheus.GaugeValue,
  257. float64(stats.Counter.Repo),
  258. )
  259. ch <- prometheus.MustNewConstMetric(
  260. c.Stars,
  261. prometheus.GaugeValue,
  262. float64(stats.Counter.Star),
  263. )
  264. ch <- prometheus.MustNewConstMetric(
  265. c.Teams,
  266. prometheus.GaugeValue,
  267. float64(stats.Counter.Team),
  268. )
  269. ch <- prometheus.MustNewConstMetric(
  270. c.UpdateTasks,
  271. prometheus.GaugeValue,
  272. float64(stats.Counter.UpdateTask),
  273. )
  274. ch <- prometheus.MustNewConstMetric(
  275. c.Users,
  276. prometheus.GaugeValue,
  277. float64(stats.Counter.User),
  278. )
  279. ch <- prometheus.MustNewConstMetric(
  280. c.Watches,
  281. prometheus.GaugeValue,
  282. float64(stats.Counter.Watch),
  283. )
  284. ch <- prometheus.MustNewConstMetric(
  285. c.Webhooks,
  286. prometheus.GaugeValue,
  287. float64(stats.Counter.Webhook),
  288. )
  289. }