From 987152ba408b03fe0b15dd9c72dfd1614b4159fd Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 5 Oct 2021 20:39:37 +0200 Subject: [PATCH] Add metrics to get issues by repository (#17225) --- custom/conf/app.example.ini | 2 + .../doc/advanced/config-cheat-sheet.en-us.md | 3 +- models/statistic.go | 20 +++++- modules/metrics/collector.go | 69 +++++++++++-------- modules/setting/setting.go | 14 ++-- 5 files changed, 73 insertions(+), 35 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index d8b73aa7e..0a25e7e5c 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2043,6 +2043,8 @@ PATH = ;TOKEN = ;; Enable issue by label metrics; default is false ;ENABLED_ISSUE_BY_LABEL = false +;; Enable issue by repository metrics; default is false +;ENABLED_ISSUE_BY_REPOSITORY = false ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 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 5726473d2..d224533e9 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -853,7 +853,8 @@ 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 +- `ENABLED_ISSUE_BY_LABEL`: **false**: Enable issue by label metrics with format `gitea_issues_by_label{label="bug"} 2`. +- `ENABLED_ISSUE_BY_REPOSITORY`: **false**: Enable issue by repository metrics with format `gitea_issues_by_repository{repository="org/repo"} 5`. - `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`) diff --git a/models/statistic.go b/models/statistic.go index c80cebba9..5e72dc713 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -21,7 +21,8 @@ type Statistic struct { Milestone, Label, HookTask, Team, UpdateTask, Project, ProjectBoard, Attachment int64 - IssueByLabel []IssueByLabelCount + IssueByLabel []IssueByLabelCount + IssueByRepository []IssueByRepositoryCount } } @@ -31,6 +32,13 @@ type IssueByLabelCount struct { Label string } +// IssueByRepositoryCount contains the number of issue group by repository +type IssueByRepositoryCount struct { + Count int64 + OwnerName string + Repository string +} + // GetStatistic returns the database statistics func GetStatistic() (stats Statistic) { e := db.GetEngine(db.DefaultContext) @@ -58,6 +66,16 @@ func GetStatistic() (stats Statistic) { Find(&stats.Counter.IssueByLabel) } + if setting.Metrics.EnabledIssueByRepository { + stats.Counter.IssueByRepository = []IssueByRepositoryCount{} + + _ = e.Select("COUNT(*) AS count, r.owner_name, r.name AS repository"). + Join("LEFT", "repository r", "r.id=i.repo_id"). + Table("issue i"). + GroupBy("r.owner_name, r.name"). + Find(&stats.Counter.IssueByRepository) + } + issueCounts := []IssueCount{} _ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts) diff --git a/modules/metrics/collector.go b/modules/metrics/collector.go index dcc147631..527202e0a 100755 --- a/modules/metrics/collector.go +++ b/modules/metrics/collector.go @@ -15,33 +15,34 @@ const namespace = "gitea_" // Collector implements the prometheus.Collector interface and // exposes gitea metrics for prometheus type Collector struct { - Accesses *prometheus.Desc - Actions *prometheus.Desc - Attachments *prometheus.Desc - Comments *prometheus.Desc - Follows *prometheus.Desc - HookTasks *prometheus.Desc - Issues *prometheus.Desc - IssuesOpen *prometheus.Desc - IssuesClosed *prometheus.Desc - IssuesByLabel *prometheus.Desc - Labels *prometheus.Desc - LoginSources *prometheus.Desc - Milestones *prometheus.Desc - Mirrors *prometheus.Desc - Oauths *prometheus.Desc - Organizations *prometheus.Desc - Projects *prometheus.Desc - ProjectBoards *prometheus.Desc - PublicKeys *prometheus.Desc - Releases *prometheus.Desc - Repositories *prometheus.Desc - Stars *prometheus.Desc - Teams *prometheus.Desc - UpdateTasks *prometheus.Desc - Users *prometheus.Desc - Watches *prometheus.Desc - Webhooks *prometheus.Desc + Accesses *prometheus.Desc + Actions *prometheus.Desc + Attachments *prometheus.Desc + Comments *prometheus.Desc + Follows *prometheus.Desc + HookTasks *prometheus.Desc + Issues *prometheus.Desc + IssuesOpen *prometheus.Desc + IssuesClosed *prometheus.Desc + IssuesByLabel *prometheus.Desc + IssuesByRepository *prometheus.Desc + Labels *prometheus.Desc + LoginSources *prometheus.Desc + Milestones *prometheus.Desc + Mirrors *prometheus.Desc + Oauths *prometheus.Desc + Organizations *prometheus.Desc + Projects *prometheus.Desc + ProjectBoards *prometheus.Desc + PublicKeys *prometheus.Desc + Releases *prometheus.Desc + Repositories *prometheus.Desc + Stars *prometheus.Desc + Teams *prometheus.Desc + UpdateTasks *prometheus.Desc + Users *prometheus.Desc + Watches *prometheus.Desc + Webhooks *prometheus.Desc } // NewCollector returns a new Collector with all prometheus.Desc initialized @@ -88,6 +89,11 @@ func NewCollector() Collector { "Number of Issues", []string{"label"}, nil, ), + IssuesByRepository: prometheus.NewDesc( + namespace+"issues_by_repository", + "Number of Issues", + []string{"repository"}, nil, + ), IssuesOpen: prometheus.NewDesc( namespace+"issues_open", "Number of open Issues", @@ -196,6 +202,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) { ch <- c.HookTasks ch <- c.Issues ch <- c.IssuesByLabel + ch <- c.IssuesByRepository ch <- c.IssuesOpen ch <- c.IssuesClosed ch <- c.Labels @@ -264,6 +271,14 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) { il.Label, ) } + for _, ir := range stats.Counter.IssueByRepository { + ch <- prometheus.MustNewConstMetric( + c.IssuesByRepository, + prometheus.GaugeValue, + float64(ir.Count), + ir.OwnerName+"/"+ir.Repository, + ) + } ch <- prometheus.MustNewConstMetric( c.IssuesClosed, prometheus.GaugeValue, diff --git a/modules/setting/setting.go b/modules/setting/setting.go index bdb3b1fd7..88302be1d 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -390,13 +390,15 @@ var ( // Metrics settings Metrics = struct { - Enabled bool - Token string - EnabledIssueByLabel bool + Enabled bool + Token string + EnabledIssueByLabel bool + EnabledIssueByRepository bool }{ - Enabled: false, - Token: "", - EnabledIssueByLabel: false, + Enabled: false, + Token: "", + EnabledIssueByLabel: false, + EnabledIssueByRepository: false, } // I18n settings