From 719d4457d7feef6d90a628e2ae4aa5fc16277be5 Mon Sep 17 00:00:00 2001 From: Romain Date: Sat, 25 Sep 2021 12:04:49 +0200 Subject: [PATCH 1/8] Add metrics to get issues by label --- models/statistic.go | 16 ++++++++++++++++ modules/metrics/collector.go | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/models/statistic.go b/models/statistic.go index 43b1afbc48173..f8716cc51809a 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -19,9 +19,15 @@ type Statistic struct { Mirror, Release, LoginSource, Webhook, Milestone, Label, HookTask, Team, UpdateTask, Attachment int64 + IssueByLabel []IssueByLabelCount } } +type IssueByLabelCount struct { + Count int64 + Label string +} + // GetStatistic returns the database statistics func GetStatistic() (stats Statistic) { stats.Counter.User = CountUsers() @@ -37,7 +43,17 @@ func GetStatistic() (stats Statistic) { Count int64 IsClosed bool } + issueCounts := []IssueCount{} + stats.Counter.IssueByLabel = []IssueByLabelCount{} + + _ = db.GetEngine(db.DefaultContext). + Select("COUNT(*) AS count, l.name as label"). + Join("LEFT", "issue_label il", "i.id=il.issue_id"). + Join("LEFT", "label l", "l.id=il.label_id"). + Table("issue i"). + GroupBy("l.name"). + Find(&stats.Counter.IssueByLabel) _ = db.GetEngine(db.DefaultContext).Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts) for _, c := range issueCounts { diff --git a/modules/metrics/collector.go b/modules/metrics/collector.go index f906b58f7da82..27485eb5976c8 100755 --- a/modules/metrics/collector.go +++ b/modules/metrics/collector.go @@ -6,7 +6,6 @@ package metrics import ( "code.gitea.io/gitea/models" - "github.com/prometheus/client_golang/prometheus" ) @@ -24,6 +23,7 @@ type Collector struct { Issues *prometheus.Desc IssuesOpen *prometheus.Desc IssuesClosed *prometheus.Desc + IssuesByLabel *prometheus.Desc Labels *prometheus.Desc LoginSources *prometheus.Desc Milestones *prometheus.Desc @@ -43,6 +43,7 @@ type Collector struct { // NewCollector returns a new Collector with all prometheus.Desc initialized func NewCollector() Collector { + return Collector{ Accesses: prometheus.NewDesc( namespace+"accesses", @@ -79,6 +80,11 @@ func NewCollector() Collector { "Number of Issues", nil, nil, ), + IssuesByLabel: prometheus.NewDesc( + namespace+"issues_by_label", + "Number of Issues", + []string{"label"}, nil, + ), IssuesOpen: prometheus.NewDesc( namespace+"issues_open", "Number of open Issues", @@ -165,7 +171,6 @@ func NewCollector() Collector { nil, nil, ), } - } // Describe returns all possible prometheus.Desc @@ -177,6 +182,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) { ch <- c.Follows ch <- c.HookTasks ch <- c.Issues + ch <- c.IssuesByLabel ch <- c.IssuesOpen ch <- c.IssuesClosed ch <- c.Labels @@ -235,6 +241,14 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) { prometheus.GaugeValue, float64(stats.Counter.Issue), ) + for _, il := range stats.Counter.IssueByLabel { + ch <- prometheus.MustNewConstMetric( + c.IssuesByLabel, + prometheus.GaugeValue, + float64(il.Count), + il.Label, + ) + } ch <- prometheus.MustNewConstMetric( c.IssuesClosed, prometheus.GaugeValue, From af922a2bdb4ebea0c05d4e4bbd040e278c524a10 Mon Sep 17 00:00:00 2001 From: Romain Date: Fri, 1 Oct 2021 09:27:29 +0200 Subject: [PATCH 2/8] Add comment on IssueByLabelCount --- models/statistic.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/statistic.go b/models/statistic.go index f8716cc51809a..92ad4d74b8741 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -23,6 +23,7 @@ type Statistic struct { } } +// IssueByLabelCount contains the number of issue group by label type IssueByLabelCount struct { Count int64 Label string From b34d695d1d2f40830ee2aa369dea45fa735eb95e Mon Sep 17 00:00:00 2001 From: Romain Date: Sun, 3 Oct 2021 11:07:39 +0200 Subject: [PATCH 3/8] Code review - Unify "AS" in SQL (#17201) --- models/statistic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/statistic.go b/models/statistic.go index 9e51b40cba0e9..8556eec6ed1b9 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -51,7 +51,7 @@ func GetStatistic() (stats Statistic) { stats.Counter.IssueByLabel = []IssueByLabelCount{} _ = db.GetEngine(db.DefaultContext). - Select("COUNT(*) AS count, l.name as label"). + Select("COUNT(*) AS count, l.name AS label"). Join("LEFT", "issue_label il", "i.id=il.issue_id"). Join("LEFT", "label l", "l.id=il.label_id"). Table("issue i"). From 70b45b2f8af631fe7e81f790815ae91193dfdfac Mon Sep 17 00:00:00 2001 From: Romain Date: Sun, 3 Oct 2021 11:15:47 +0200 Subject: [PATCH 4/8] Code review - Remove useless join (#17201) --- models/statistic.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/statistic.go b/models/statistic.go index 8556eec6ed1b9..dab45090d7fcd 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -52,9 +52,8 @@ func GetStatistic() (stats Statistic) { _ = db.GetEngine(db.DefaultContext). Select("COUNT(*) AS count, l.name AS label"). - Join("LEFT", "issue_label il", "i.id=il.issue_id"). Join("LEFT", "label l", "l.id=il.label_id"). - Table("issue i"). + Table("issue_label il"). GroupBy("l.name"). Find(&stats.Counter.IssueByLabel) From d9f42101a879a66ef07e49094077fee3d00330f3 Mon Sep 17 00:00:00 2001 From: Romain Date: Sun, 3 Oct 2021 11:33:00 +0200 Subject: [PATCH 5/8] Code review - Disable issue_by_label by default in settings (#17201) --- custom/conf/app.example.ini | 2 ++ models/statistic.go | 20 ++++++++++++-------- modules/setting/setting.go | 10 ++++++---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index f8286a2e87549..d1ffc1ac917fd 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2041,6 +2041,8 @@ PATH = ;ENABLED = false ;; If you want to add authorization, specify a token here ;TOKEN = +;; Enable issue by label metrics; default is false +;ENABLED_ISSUE_BY_LABEL = false ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/models/statistic.go b/models/statistic.go index dab45090d7fcd..f65f5825a0c4e 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -7,6 +7,7 @@ package models import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" + "code.gitea.io/gitea/modules/setting" ) // Statistic contains the database statistics @@ -47,15 +48,18 @@ func GetStatistic() (stats Statistic) { IsClosed bool } - issueCounts := []IssueCount{} - stats.Counter.IssueByLabel = []IssueByLabelCount{} + if setting.Metrics.EnabledIssueByLabel { + stats.Counter.IssueByLabel = []IssueByLabelCount{} + + _ = db.GetEngine(db.DefaultContext). + Select("COUNT(*) AS count, l.name AS label"). + Join("LEFT", "label l", "l.id=il.label_id"). + Table("issue_label il"). + GroupBy("l.name"). + Find(&stats.Counter.IssueByLabel) + } - _ = db.GetEngine(db.DefaultContext). - Select("COUNT(*) AS count, l.name AS label"). - Join("LEFT", "label l", "l.id=il.label_id"). - Table("issue_label il"). - GroupBy("l.name"). - Find(&stats.Counter.IssueByLabel) + issueCounts := []IssueCount{} _ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts) for _, c := range issueCounts { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index afd1e49aed07f..bdb3b1fd7b93d 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -390,11 +390,13 @@ var ( // Metrics settings Metrics = struct { - Enabled bool - Token string + Enabled bool + Token string + EnabledIssueByLabel bool }{ - Enabled: false, - Token: "", + Enabled: false, + Token: "", + EnabledIssueByLabel: false, } // I18n settings From 0cd46761df66518f32ec41209ce71f7b38bbf7b0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 3 Oct 2021 13:13:58 +0200 Subject: [PATCH 6/8] use e --- models/statistic.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/statistic.go b/models/statistic.go index f65f5825a0c4e..c80cebba99feb 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -51,8 +51,7 @@ func GetStatistic() (stats Statistic) { if setting.Metrics.EnabledIssueByLabel { stats.Counter.IssueByLabel = []IssueByLabelCount{} - _ = db.GetEngine(db.DefaultContext). - Select("COUNT(*) AS count, l.name AS label"). + _ = e.Select("COUNT(*) AS count, l.name AS label"). Join("LEFT", "label l", "l.id=il.label_id"). Table("issue_label il"). GroupBy("l.name"). From 42cb0845a4ff501590c17eaafe394b4099cb56a6 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Sun, 3 Oct 2021 17:33:40 -0400 Subject: [PATCH 7/8] restore empty line --- modules/metrics/collector.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/metrics/collector.go b/modules/metrics/collector.go index 9034dbd7567ab..dcc147631b702 100755 --- a/modules/metrics/collector.go +++ b/modules/metrics/collector.go @@ -6,6 +6,7 @@ package metrics import ( "code.gitea.io/gitea/models" + "github.com/prometheus/client_golang/prometheus" ) From c52a82d207f207e39465ca95e60456c195c8f64f Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Sun, 3 Oct 2021 17:35:10 -0400 Subject: [PATCH 8/8] update docs --- docs/content/doc/advanced/config-cheat-sheet.en-us.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index d80f96b3de04b..5726473d240f0 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -853,6 +853,7 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef ## Metrics (`metrics`) - `ENABLED`: **false**: Enables /metrics endpoint for prometheus. +- `ENABLED_ISSUE_BY_LABEL`: **false**: Enable issue by label metrics - `TOKEN`: **\**: 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`. ## API (`api`)