Skip to content

Commit 987152b

Browse files
authored
Add metrics to get issues by repository (#17225)
1 parent 760d61b commit 987152b

File tree

5 files changed

+73
-35
lines changed

5 files changed

+73
-35
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,8 @@ PATH =
20432043
;TOKEN =
20442044
;; Enable issue by label metrics; default is false
20452045
;ENABLED_ISSUE_BY_LABEL = false
2046+
;; Enable issue by repository metrics; default is false
2047+
;ENABLED_ISSUE_BY_REPOSITORY = false
20462048

20472049
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20482050
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,8 @@ 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
856+
- `ENABLED_ISSUE_BY_LABEL`: **false**: Enable issue by label metrics with format `gitea_issues_by_label{label="bug"} 2`.
857+
- `ENABLED_ISSUE_BY_REPOSITORY`: **false**: Enable issue by repository metrics with format `gitea_issues_by_repository{repository="org/repo"} 5`.
857858
- `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`.
858859

859860
## API (`api`)

models/statistic.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type Statistic struct {
2121
Milestone, Label, HookTask,
2222
Team, UpdateTask, Project,
2323
ProjectBoard, Attachment int64
24-
IssueByLabel []IssueByLabelCount
24+
IssueByLabel []IssueByLabelCount
25+
IssueByRepository []IssueByRepositoryCount
2526
}
2627
}
2728

@@ -31,6 +32,13 @@ type IssueByLabelCount struct {
3132
Label string
3233
}
3334

35+
// IssueByRepositoryCount contains the number of issue group by repository
36+
type IssueByRepositoryCount struct {
37+
Count int64
38+
OwnerName string
39+
Repository string
40+
}
41+
3442
// GetStatistic returns the database statistics
3543
func GetStatistic() (stats Statistic) {
3644
e := db.GetEngine(db.DefaultContext)
@@ -58,6 +66,16 @@ func GetStatistic() (stats Statistic) {
5866
Find(&stats.Counter.IssueByLabel)
5967
}
6068

69+
if setting.Metrics.EnabledIssueByRepository {
70+
stats.Counter.IssueByRepository = []IssueByRepositoryCount{}
71+
72+
_ = e.Select("COUNT(*) AS count, r.owner_name, r.name AS repository").
73+
Join("LEFT", "repository r", "r.id=i.repo_id").
74+
Table("issue i").
75+
GroupBy("r.owner_name, r.name").
76+
Find(&stats.Counter.IssueByRepository)
77+
}
78+
6179
issueCounts := []IssueCount{}
6280

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

modules/metrics/collector.go

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,34 @@ const namespace = "gitea_"
1515
// Collector implements the prometheus.Collector interface and
1616
// exposes gitea metrics for prometheus
1717
type Collector struct {
18-
Accesses *prometheus.Desc
19-
Actions *prometheus.Desc
20-
Attachments *prometheus.Desc
21-
Comments *prometheus.Desc
22-
Follows *prometheus.Desc
23-
HookTasks *prometheus.Desc
24-
Issues *prometheus.Desc
25-
IssuesOpen *prometheus.Desc
26-
IssuesClosed *prometheus.Desc
27-
IssuesByLabel *prometheus.Desc
28-
Labels *prometheus.Desc
29-
LoginSources *prometheus.Desc
30-
Milestones *prometheus.Desc
31-
Mirrors *prometheus.Desc
32-
Oauths *prometheus.Desc
33-
Organizations *prometheus.Desc
34-
Projects *prometheus.Desc
35-
ProjectBoards *prometheus.Desc
36-
PublicKeys *prometheus.Desc
37-
Releases *prometheus.Desc
38-
Repositories *prometheus.Desc
39-
Stars *prometheus.Desc
40-
Teams *prometheus.Desc
41-
UpdateTasks *prometheus.Desc
42-
Users *prometheus.Desc
43-
Watches *prometheus.Desc
44-
Webhooks *prometheus.Desc
18+
Accesses *prometheus.Desc
19+
Actions *prometheus.Desc
20+
Attachments *prometheus.Desc
21+
Comments *prometheus.Desc
22+
Follows *prometheus.Desc
23+
HookTasks *prometheus.Desc
24+
Issues *prometheus.Desc
25+
IssuesOpen *prometheus.Desc
26+
IssuesClosed *prometheus.Desc
27+
IssuesByLabel *prometheus.Desc
28+
IssuesByRepository *prometheus.Desc
29+
Labels *prometheus.Desc
30+
LoginSources *prometheus.Desc
31+
Milestones *prometheus.Desc
32+
Mirrors *prometheus.Desc
33+
Oauths *prometheus.Desc
34+
Organizations *prometheus.Desc
35+
Projects *prometheus.Desc
36+
ProjectBoards *prometheus.Desc
37+
PublicKeys *prometheus.Desc
38+
Releases *prometheus.Desc
39+
Repositories *prometheus.Desc
40+
Stars *prometheus.Desc
41+
Teams *prometheus.Desc
42+
UpdateTasks *prometheus.Desc
43+
Users *prometheus.Desc
44+
Watches *prometheus.Desc
45+
Webhooks *prometheus.Desc
4546
}
4647

4748
// NewCollector returns a new Collector with all prometheus.Desc initialized
@@ -88,6 +89,11 @@ func NewCollector() Collector {
8889
"Number of Issues",
8990
[]string{"label"}, nil,
9091
),
92+
IssuesByRepository: prometheus.NewDesc(
93+
namespace+"issues_by_repository",
94+
"Number of Issues",
95+
[]string{"repository"}, nil,
96+
),
9197
IssuesOpen: prometheus.NewDesc(
9298
namespace+"issues_open",
9399
"Number of open Issues",
@@ -196,6 +202,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
196202
ch <- c.HookTasks
197203
ch <- c.Issues
198204
ch <- c.IssuesByLabel
205+
ch <- c.IssuesByRepository
199206
ch <- c.IssuesOpen
200207
ch <- c.IssuesClosed
201208
ch <- c.Labels
@@ -264,6 +271,14 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
264271
il.Label,
265272
)
266273
}
274+
for _, ir := range stats.Counter.IssueByRepository {
275+
ch <- prometheus.MustNewConstMetric(
276+
c.IssuesByRepository,
277+
prometheus.GaugeValue,
278+
float64(ir.Count),
279+
ir.OwnerName+"/"+ir.Repository,
280+
)
281+
}
267282
ch <- prometheus.MustNewConstMetric(
268283
c.IssuesClosed,
269284
prometheus.GaugeValue,

modules/setting/setting.go

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

391391
// Metrics settings
392392
Metrics = struct {
393-
Enabled bool
394-
Token string
395-
EnabledIssueByLabel bool
393+
Enabled bool
394+
Token string
395+
EnabledIssueByLabel bool
396+
EnabledIssueByRepository bool
396397
}{
397-
Enabled: false,
398-
Token: "",
399-
EnabledIssueByLabel: false,
398+
Enabled: false,
399+
Token: "",
400+
EnabledIssueByLabel: false,
401+
EnabledIssueByRepository: false,
400402
}
401403

402404
// I18n settings

0 commit comments

Comments
 (0)