Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics to get issues by label #17201

Merged
merged 9 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
6543 marked this conversation as resolved.
Show resolved Hide resolved

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
20 changes: 20 additions & 0 deletions models/statistic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,9 +21,16 @@ type Statistic struct {
Milestone, Label, HookTask,
Team, UpdateTask, Project,
ProjectBoard, Attachment int64
IssueByLabel []IssueByLabelCount
}
}

// IssueByLabelCount contains the number of issue group by label
type IssueByLabelCount struct {
Count int64
Label string
}

// GetStatistic returns the database statistics
func GetStatistic() (stats Statistic) {
e := db.GetEngine(db.DefaultContext)
Expand All @@ -39,6 +47,18 @@ func GetStatistic() (stats Statistic) {
Count int64
IsClosed bool
}

if setting.Metrics.EnabledIssueByLabel {
stats.Counter.IssueByLabel = []IssueByLabelCount{}

_ = db.GetEngine(db.DefaultContext).
Select("COUNT(*) AS count, l.name AS label").
6543 marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
18 changes: 16 additions & 2 deletions modules/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package metrics

import (
"code.gitea.io/gitea/models"

"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -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
Expand All @@ -45,6 +45,7 @@ type Collector struct {

// NewCollector returns a new Collector with all prometheus.Desc initialized
func NewCollector() Collector {

return Collector{
Accesses: prometheus.NewDesc(
namespace+"accesses",
Expand Down Expand Up @@ -81,6 +82,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",
Expand Down Expand Up @@ -177,7 +183,6 @@ func NewCollector() Collector {
nil, nil,
),
}

}

// Describe returns all possible prometheus.Desc
Expand All @@ -189,6 +194,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
Expand Down Expand Up @@ -249,6 +255,14 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
prometheus.GaugeValue,
float64(stats.Counter.Issue),
)
for _, il := range stats.Counter.IssueByLabel {
6543 marked this conversation as resolved.
Show resolved Hide resolved
ch <- prometheus.MustNewConstMetric(
c.IssuesByLabel,
prometheus.GaugeValue,
float64(il.Count),
il.Label,
)
}
ch <- prometheus.MustNewConstMetric(
c.IssuesClosed,
prometheus.GaugeValue,
Expand Down
10 changes: 6 additions & 4 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down