From dee44bfe608dd4a1457b97d2daf432645d826ba8 Mon Sep 17 00:00:00 2001 From: Marc Tuduri Date: Wed, 25 Oct 2023 12:35:18 +0200 Subject: [PATCH] Add user label to PR metric --- exporter/metrics.go | 11 +++++++---- test/github_exporter_test.go | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/exporter/metrics.go b/exporter/metrics.go index 3bd3c161..679b2a0b 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -1,7 +1,10 @@ package exporter -import "github.com/prometheus/client_golang/prometheus" -import "strconv" +import ( + "strconv" + + "github.com/prometheus/client_golang/prometheus" +) // AddMetrics - Add's all of the metrics to a map of strings, returns the map. func AddMetrics() map[string]*prometheus.Desc { @@ -21,7 +24,7 @@ func AddMetrics() map[string]*prometheus.Desc { APIMetrics["PullRequestCount"] = prometheus.NewDesc( prometheus.BuildFQName("github", "repo", "pull_request_count"), "Total number of pull requests for given repository", - []string{"repo"}, nil, + []string{"repo", "user"}, nil, ) APIMetrics["Watchers"] = prometheus.NewDesc( prometheus.BuildFQName("github", "repo", "watchers"), @@ -85,7 +88,7 @@ func (e *Exporter) processMetrics(data []*Datum, rates *RateLimits, ch chan<- pr ch <- prometheus.MustNewConstMetric(e.APIMetrics["OpenIssues"], prometheus.GaugeValue, (x.OpenIssues - float64(prCount)), x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language) // prCount - ch <- prometheus.MustNewConstMetric(e.APIMetrics["PullRequestCount"], prometheus.GaugeValue, float64(prCount), x.Name) + ch <- prometheus.MustNewConstMetric(e.APIMetrics["PullRequestCount"], prometheus.GaugeValue, float64(prCount), x.Name, x.Owner.Login) } // Set Rate limit stats diff --git a/test/github_exporter_test.go b/test/github_exporter_test.go index 57064c37..ae454e9c 100644 --- a/test/github_exporter_test.go +++ b/test/github_exporter_test.go @@ -42,7 +42,7 @@ func TestGithubExporter(t *testing.T) { Assert(bodyContains(`github_rate_remaining 60`)). Assert(bodyContains(`github_rate_reset 1.566853865e+09`)). Assert(bodyContains(`github_repo_forks{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 10`)). - Assert(bodyContains(`github_repo_pull_request_count{repo="myRepo"} 3`)). + Assert(bodyContains(`github_repo_pull_request_count{repo="myRepo",user="myOrg"} 3`)). Assert(bodyContains(`github_repo_open_issues{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 2`)). Assert(bodyContains(`github_repo_size_kb{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 946`)). Assert(bodyContains(`github_repo_stars{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 120`)).