code.gitea.io/gitea@v1.19.3/modules/metrics/collector.go (about) 1 // Copyright 2018 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package metrics 5 6 import ( 7 "runtime" 8 9 activities_model "code.gitea.io/gitea/models/activities" 10 "code.gitea.io/gitea/modules/setting" 11 12 "github.com/prometheus/client_golang/prometheus" 13 ) 14 15 const namespace = "gitea_" 16 17 // Collector implements the prometheus.Collector interface and 18 // exposes gitea metrics for prometheus 19 type Collector struct { 20 Accesses *prometheus.Desc 21 Actions *prometheus.Desc 22 Attachments *prometheus.Desc 23 BuildInfo *prometheus.Desc 24 Comments *prometheus.Desc 25 Follows *prometheus.Desc 26 HookTasks *prometheus.Desc 27 Issues *prometheus.Desc 28 IssuesOpen *prometheus.Desc 29 IssuesClosed *prometheus.Desc 30 IssuesByLabel *prometheus.Desc 31 IssuesByRepository *prometheus.Desc 32 Labels *prometheus.Desc 33 LoginSources *prometheus.Desc 34 Milestones *prometheus.Desc 35 Mirrors *prometheus.Desc 36 Oauths *prometheus.Desc 37 Organizations *prometheus.Desc 38 Projects *prometheus.Desc 39 ProjectBoards *prometheus.Desc 40 PublicKeys *prometheus.Desc 41 Releases *prometheus.Desc 42 Repositories *prometheus.Desc 43 Stars *prometheus.Desc 44 Teams *prometheus.Desc 45 UpdateTasks *prometheus.Desc 46 Users *prometheus.Desc 47 Watches *prometheus.Desc 48 Webhooks *prometheus.Desc 49 } 50 51 // NewCollector returns a new Collector with all prometheus.Desc initialized 52 func NewCollector() Collector { 53 return Collector{ 54 Accesses: prometheus.NewDesc( 55 namespace+"accesses", 56 "Number of Accesses", 57 nil, nil, 58 ), 59 Actions: prometheus.NewDesc( 60 namespace+"actions", 61 "Number of Actions", 62 nil, nil, 63 ), 64 Attachments: prometheus.NewDesc( 65 namespace+"attachments", 66 "Number of Attachments", 67 nil, nil, 68 ), 69 BuildInfo: prometheus.NewDesc( 70 namespace+"build_info", 71 "Build information", 72 []string{ 73 "goarch", 74 "goos", 75 "goversion", 76 "version", 77 }, nil, 78 ), 79 Comments: prometheus.NewDesc( 80 namespace+"comments", 81 "Number of Comments", 82 nil, nil, 83 ), 84 Follows: prometheus.NewDesc( 85 namespace+"follows", 86 "Number of Follows", 87 nil, nil, 88 ), 89 HookTasks: prometheus.NewDesc( 90 namespace+"hooktasks", 91 "Number of HookTasks", 92 nil, nil, 93 ), 94 Issues: prometheus.NewDesc( 95 namespace+"issues", 96 "Number of Issues", 97 nil, nil, 98 ), 99 IssuesByLabel: prometheus.NewDesc( 100 namespace+"issues_by_label", 101 "Number of Issues", 102 []string{"label"}, nil, 103 ), 104 IssuesByRepository: prometheus.NewDesc( 105 namespace+"issues_by_repository", 106 "Number of Issues", 107 []string{"repository"}, nil, 108 ), 109 IssuesOpen: prometheus.NewDesc( 110 namespace+"issues_open", 111 "Number of open Issues", 112 nil, nil, 113 ), 114 IssuesClosed: prometheus.NewDesc( 115 namespace+"issues_closed", 116 "Number of closed Issues", 117 nil, nil, 118 ), 119 Labels: prometheus.NewDesc( 120 namespace+"labels", 121 "Number of Labels", 122 nil, nil, 123 ), 124 LoginSources: prometheus.NewDesc( 125 namespace+"loginsources", 126 "Number of LoginSources", 127 nil, nil, 128 ), 129 Milestones: prometheus.NewDesc( 130 namespace+"milestones", 131 "Number of Milestones", 132 nil, nil, 133 ), 134 Mirrors: prometheus.NewDesc( 135 namespace+"mirrors", 136 "Number of Mirrors", 137 nil, nil, 138 ), 139 Oauths: prometheus.NewDesc( 140 namespace+"oauths", 141 "Number of Oauths", 142 nil, nil, 143 ), 144 Organizations: prometheus.NewDesc( 145 namespace+"organizations", 146 "Number of Organizations", 147 nil, nil, 148 ), 149 Projects: prometheus.NewDesc( 150 namespace+"projects", 151 "Number of projects", 152 nil, nil, 153 ), 154 ProjectBoards: prometheus.NewDesc( 155 namespace+"projects_boards", 156 "Number of project boards", 157 nil, nil, 158 ), 159 PublicKeys: prometheus.NewDesc( 160 namespace+"publickeys", 161 "Number of PublicKeys", 162 nil, nil, 163 ), 164 Releases: prometheus.NewDesc( 165 namespace+"releases", 166 "Number of Releases", 167 nil, nil, 168 ), 169 Repositories: prometheus.NewDesc( 170 namespace+"repositories", 171 "Number of Repositories", 172 nil, nil, 173 ), 174 Stars: prometheus.NewDesc( 175 namespace+"stars", 176 "Number of Stars", 177 nil, nil, 178 ), 179 Teams: prometheus.NewDesc( 180 namespace+"teams", 181 "Number of Teams", 182 nil, nil, 183 ), 184 UpdateTasks: prometheus.NewDesc( 185 namespace+"updatetasks", 186 "Number of UpdateTasks", 187 nil, nil, 188 ), 189 Users: prometheus.NewDesc( 190 namespace+"users", 191 "Number of Users", 192 nil, nil, 193 ), 194 Watches: prometheus.NewDesc( 195 namespace+"watches", 196 "Number of Watches", 197 nil, nil, 198 ), 199 Webhooks: prometheus.NewDesc( 200 namespace+"webhooks", 201 "Number of Webhooks", 202 nil, nil, 203 ), 204 } 205 } 206 207 // Describe returns all possible prometheus.Desc 208 func (c Collector) Describe(ch chan<- *prometheus.Desc) { 209 ch <- c.Accesses 210 ch <- c.Actions 211 ch <- c.Attachments 212 ch <- c.BuildInfo 213 ch <- c.Comments 214 ch <- c.Follows 215 ch <- c.HookTasks 216 ch <- c.Issues 217 ch <- c.IssuesByLabel 218 ch <- c.IssuesByRepository 219 ch <- c.IssuesOpen 220 ch <- c.IssuesClosed 221 ch <- c.Labels 222 ch <- c.LoginSources 223 ch <- c.Milestones 224 ch <- c.Mirrors 225 ch <- c.Oauths 226 ch <- c.Organizations 227 ch <- c.Projects 228 ch <- c.ProjectBoards 229 ch <- c.PublicKeys 230 ch <- c.Releases 231 ch <- c.Repositories 232 ch <- c.Stars 233 ch <- c.Teams 234 ch <- c.UpdateTasks 235 ch <- c.Users 236 ch <- c.Watches 237 ch <- c.Webhooks 238 } 239 240 // Collect returns the metrics with values 241 func (c Collector) Collect(ch chan<- prometheus.Metric) { 242 stats := activities_model.GetStatistic() 243 244 ch <- prometheus.MustNewConstMetric( 245 c.Accesses, 246 prometheus.GaugeValue, 247 float64(stats.Counter.Access), 248 ) 249 ch <- prometheus.MustNewConstMetric( 250 c.Actions, 251 prometheus.GaugeValue, 252 float64(stats.Counter.Action), 253 ) 254 ch <- prometheus.MustNewConstMetric( 255 c.Attachments, 256 prometheus.GaugeValue, 257 float64(stats.Counter.Attachment), 258 ) 259 ch <- prometheus.MustNewConstMetric( 260 c.BuildInfo, 261 prometheus.GaugeValue, 262 1, 263 runtime.GOARCH, 264 runtime.GOOS, 265 runtime.Version(), 266 setting.AppVer, 267 ) 268 ch <- prometheus.MustNewConstMetric( 269 c.Comments, 270 prometheus.GaugeValue, 271 float64(stats.Counter.Comment), 272 ) 273 ch <- prometheus.MustNewConstMetric( 274 c.Follows, 275 prometheus.GaugeValue, 276 float64(stats.Counter.Follow), 277 ) 278 ch <- prometheus.MustNewConstMetric( 279 c.HookTasks, 280 prometheus.GaugeValue, 281 float64(stats.Counter.HookTask), 282 ) 283 ch <- prometheus.MustNewConstMetric( 284 c.Issues, 285 prometheus.GaugeValue, 286 float64(stats.Counter.Issue), 287 ) 288 for _, il := range stats.Counter.IssueByLabel { 289 ch <- prometheus.MustNewConstMetric( 290 c.IssuesByLabel, 291 prometheus.GaugeValue, 292 float64(il.Count), 293 il.Label, 294 ) 295 } 296 for _, ir := range stats.Counter.IssueByRepository { 297 ch <- prometheus.MustNewConstMetric( 298 c.IssuesByRepository, 299 prometheus.GaugeValue, 300 float64(ir.Count), 301 ir.OwnerName+"/"+ir.Repository, 302 ) 303 } 304 ch <- prometheus.MustNewConstMetric( 305 c.IssuesClosed, 306 prometheus.GaugeValue, 307 float64(stats.Counter.IssueClosed), 308 ) 309 ch <- prometheus.MustNewConstMetric( 310 c.IssuesOpen, 311 prometheus.GaugeValue, 312 float64(stats.Counter.IssueOpen), 313 ) 314 ch <- prometheus.MustNewConstMetric( 315 c.Labels, 316 prometheus.GaugeValue, 317 float64(stats.Counter.Label), 318 ) 319 ch <- prometheus.MustNewConstMetric( 320 c.LoginSources, 321 prometheus.GaugeValue, 322 float64(stats.Counter.AuthSource), 323 ) 324 ch <- prometheus.MustNewConstMetric( 325 c.Milestones, 326 prometheus.GaugeValue, 327 float64(stats.Counter.Milestone), 328 ) 329 ch <- prometheus.MustNewConstMetric( 330 c.Mirrors, 331 prometheus.GaugeValue, 332 float64(stats.Counter.Mirror), 333 ) 334 ch <- prometheus.MustNewConstMetric( 335 c.Oauths, 336 prometheus.GaugeValue, 337 float64(stats.Counter.Oauth), 338 ) 339 ch <- prometheus.MustNewConstMetric( 340 c.Organizations, 341 prometheus.GaugeValue, 342 float64(stats.Counter.Org), 343 ) 344 ch <- prometheus.MustNewConstMetric( 345 c.Projects, 346 prometheus.GaugeValue, 347 float64(stats.Counter.Project), 348 ) 349 ch <- prometheus.MustNewConstMetric( 350 c.ProjectBoards, 351 prometheus.GaugeValue, 352 float64(stats.Counter.ProjectBoard), 353 ) 354 ch <- prometheus.MustNewConstMetric( 355 c.PublicKeys, 356 prometheus.GaugeValue, 357 float64(stats.Counter.PublicKey), 358 ) 359 ch <- prometheus.MustNewConstMetric( 360 c.Releases, 361 prometheus.GaugeValue, 362 float64(stats.Counter.Release), 363 ) 364 ch <- prometheus.MustNewConstMetric( 365 c.Repositories, 366 prometheus.GaugeValue, 367 float64(stats.Counter.Repo), 368 ) 369 ch <- prometheus.MustNewConstMetric( 370 c.Stars, 371 prometheus.GaugeValue, 372 float64(stats.Counter.Star), 373 ) 374 ch <- prometheus.MustNewConstMetric( 375 c.Teams, 376 prometheus.GaugeValue, 377 float64(stats.Counter.Team), 378 ) 379 ch <- prometheus.MustNewConstMetric( 380 c.UpdateTasks, 381 prometheus.GaugeValue, 382 float64(stats.Counter.UpdateTask), 383 ) 384 ch <- prometheus.MustNewConstMetric( 385 c.Users, 386 prometheus.GaugeValue, 387 float64(stats.Counter.User), 388 ) 389 ch <- prometheus.MustNewConstMetric( 390 c.Watches, 391 prometheus.GaugeValue, 392 float64(stats.Counter.Watch), 393 ) 394 ch <- prometheus.MustNewConstMetric( 395 c.Webhooks, 396 prometheus.GaugeValue, 397 float64(stats.Counter.Webhook), 398 ) 399 }