Skip to content

Commit fc5ee1e

Browse files
romdum6543techknowlogick
authored
Add metrics to get issues by label (#17201)
* Add metrics to get issues by label * Add comment on IssueByLabelCount * Code review - Unify "AS" in SQL (#17201) * Code review - Remove useless join (#17201) * Code review - Disable issue_by_label by default in settings (#17201) * use e * restore empty line * update docs Co-authored-by: 6543 <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 89ddbe9 commit fc5ee1e

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,8 @@ PATH =
20412041
;ENABLED = false
20422042
;; If you want to add authorization, specify a token here
20432043
;TOKEN =
2044+
;; Enable issue by label metrics; default is false
2045+
;ENABLED_ISSUE_BY_LABEL = false
20442046

20452047
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20462048
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
853853
## Metrics (`metrics`)
854854

855855
- `ENABLED`: **false**: Enables /metrics endpoint for prometheus.
856+
- `ENABLED_ISSUE_BY_LABEL`: **false**: Enable issue by label metrics
856857
- `TOKEN`: **\<empty\>**: You need to specify the token, if you want to include in the authorization the metrics . The same token need to be used in prometheus parameters `bearer_token` or `bearer_token_file`.
857858

858859
## API (`api`)

models/statistic.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"code.gitea.io/gitea/models/db"
99
"code.gitea.io/gitea/models/login"
10+
"code.gitea.io/gitea/modules/setting"
1011
)
1112

1213
// Statistic contains the database statistics
@@ -20,9 +21,16 @@ type Statistic struct {
2021
Milestone, Label, HookTask,
2122
Team, UpdateTask, Project,
2223
ProjectBoard, Attachment int64
24+
IssueByLabel []IssueByLabelCount
2325
}
2426
}
2527

28+
// IssueByLabelCount contains the number of issue group by label
29+
type IssueByLabelCount struct {
30+
Count int64
31+
Label string
32+
}
33+
2634
// GetStatistic returns the database statistics
2735
func GetStatistic() (stats Statistic) {
2836
e := db.GetEngine(db.DefaultContext)
@@ -39,6 +47,17 @@ func GetStatistic() (stats Statistic) {
3947
Count int64
4048
IsClosed bool
4149
}
50+
51+
if setting.Metrics.EnabledIssueByLabel {
52+
stats.Counter.IssueByLabel = []IssueByLabelCount{}
53+
54+
_ = e.Select("COUNT(*) AS count, l.name AS label").
55+
Join("LEFT", "label l", "l.id=il.label_id").
56+
Table("issue_label il").
57+
GroupBy("l.name").
58+
Find(&stats.Counter.IssueByLabel)
59+
}
60+
4261
issueCounts := []IssueCount{}
4362

4463
_ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)

modules/metrics/collector.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Collector struct {
2424
Issues *prometheus.Desc
2525
IssuesOpen *prometheus.Desc
2626
IssuesClosed *prometheus.Desc
27+
IssuesByLabel *prometheus.Desc
2728
Labels *prometheus.Desc
2829
LoginSources *prometheus.Desc
2930
Milestones *prometheus.Desc
@@ -45,6 +46,7 @@ type Collector struct {
4546

4647
// NewCollector returns a new Collector with all prometheus.Desc initialized
4748
func NewCollector() Collector {
49+
4850
return Collector{
4951
Accesses: prometheus.NewDesc(
5052
namespace+"accesses",
@@ -81,6 +83,11 @@ func NewCollector() Collector {
8183
"Number of Issues",
8284
nil, nil,
8385
),
86+
IssuesByLabel: prometheus.NewDesc(
87+
namespace+"issues_by_label",
88+
"Number of Issues",
89+
[]string{"label"}, nil,
90+
),
8491
IssuesOpen: prometheus.NewDesc(
8592
namespace+"issues_open",
8693
"Number of open Issues",
@@ -177,7 +184,6 @@ func NewCollector() Collector {
177184
nil, nil,
178185
),
179186
}
180-
181187
}
182188

183189
// Describe returns all possible prometheus.Desc
@@ -189,6 +195,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
189195
ch <- c.Follows
190196
ch <- c.HookTasks
191197
ch <- c.Issues
198+
ch <- c.IssuesByLabel
192199
ch <- c.IssuesOpen
193200
ch <- c.IssuesClosed
194201
ch <- c.Labels
@@ -249,6 +256,14 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
249256
prometheus.GaugeValue,
250257
float64(stats.Counter.Issue),
251258
)
259+
for _, il := range stats.Counter.IssueByLabel {
260+
ch <- prometheus.MustNewConstMetric(
261+
c.IssuesByLabel,
262+
prometheus.GaugeValue,
263+
float64(il.Count),
264+
il.Label,
265+
)
266+
}
252267
ch <- prometheus.MustNewConstMetric(
253268
c.IssuesClosed,
254269
prometheus.GaugeValue,

modules/setting/setting.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,13 @@ var (
390390

391391
// Metrics settings
392392
Metrics = struct {
393-
Enabled bool
394-
Token string
393+
Enabled bool
394+
Token string
395+
EnabledIssueByLabel bool
395396
}{
396-
Enabled: false,
397-
Token: "",
397+
Enabled: false,
398+
Token: "",
399+
EnabledIssueByLabel: false,
398400
}
399401

400402
// I18n settings

0 commit comments

Comments
 (0)