From f1b6d8e26f533ce95501659f3023eca3359391d9 Mon Sep 17 00:00:00 2001 From: hainenber Date: Wed, 28 Feb 2024 23:08:40 +0700 Subject: [PATCH] feat(exporter): include Git tag as attribute for `github_repo_release_downloads` metrics Signed-off-by: hainenber --- exporter/metrics.go | 4 ++-- exporter/structs.go | 1 + test/github_exporter_test.go | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/exporter/metrics.go b/exporter/metrics.go index 679b2a0b..32618825 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -44,7 +44,7 @@ func AddMetrics() map[string]*prometheus.Desc { APIMetrics["ReleaseDownloads"] = prometheus.NewDesc( prometheus.BuildFQName("github", "repo", "release_downloads"), "Download count for a given release", - []string{"repo", "user", "release", "name", "created_at"}, nil, + []string{"repo", "user", "release", "name", "tag", "created_at"}, nil, ) APIMetrics["Limit"] = prometheus.NewDesc( prometheus.BuildFQName("github", "rate", "limit"), @@ -77,7 +77,7 @@ func (e *Exporter) processMetrics(data []*Datum, rates *RateLimits, ch chan<- pr for _, release := range x.Releases { for _, asset := range release.Assets { - ch <- prometheus.MustNewConstMetric(e.APIMetrics["ReleaseDownloads"], prometheus.GaugeValue, float64(asset.Downloads), x.Name, x.Owner.Login, release.Name, asset.Name, asset.CreatedAt) + ch <- prometheus.MustNewConstMetric(e.APIMetrics["ReleaseDownloads"], prometheus.GaugeValue, float64(asset.Downloads), x.Name, x.Owner.Login, release.Name, asset.Name, release.Tag, asset.CreatedAt) } } prCount := 0 diff --git a/exporter/structs.go b/exporter/structs.go index 283037d6..858b876f 100644 --- a/exporter/structs.go +++ b/exporter/structs.go @@ -44,6 +44,7 @@ type Datum struct { type Release struct { Name string `json:"name"` Assets []Asset `json:"assets"` + Tag string `json:"tag_name"` } type Pull struct { diff --git a/test/github_exporter_test.go b/test/github_exporter_test.go index ae454e9c..35421868 100644 --- a/test/github_exporter_test.go +++ b/test/github_exporter_test.go @@ -47,10 +47,10 @@ func TestGithubExporter(t *testing.T) { 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`)). Assert(bodyContains(`github_repo_watchers{archived="false",fork="false",language="Go",license="mit",private="false",repo="myRepo",user="myOrg"} 5`)). - Assert(bodyContains(`github_repo_release_downloads{created_at="2019-02-28T08:25:53Z",name="myRepo_1.3.0_checksums.txt",release="1.3.0",repo="myRepo",user="myOrg"} 7292`)). - Assert(bodyContains(`github_repo_release_downloads{created_at="2019-02-28T08:25:53Z",name="myRepo_1.3.0_windows_amd64.tar.gz",release="1.3.0",repo="myRepo",user="myOrg"} 21`)). - Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_checksums.txt",release="2.0.0",repo="myRepo",user="myOrg"} 14564`)). - Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_windows_amd64.tar.gz",release="2.0.0",repo="myRepo",user="myOrg"} 55`)). + Assert(bodyContains(`github_repo_release_downloads{created_at="2019-02-28T08:25:53Z",name="myRepo_1.3.0_checksums.txt",release="1.3.0",repo="myRepo",tag="1.3.0",user="myOrg"} 7292`)). + Assert(bodyContains(`github_repo_release_downloads{created_at="2019-02-28T08:25:53Z",name="myRepo_1.3.0_windows_amd64.tar.gz",release="1.3.0",repo="myRepo",tag="1.3.0",user="myOrg"} 21`)). + Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_checksums.txt",release="2.0.0",repo="myRepo",tag="2.0.0",user="myOrg"} 14564`)). + Assert(bodyContains(`github_repo_release_downloads{created_at="2019-05-02T15:22:16Z",name="myRepo_2.0.0_windows_amd64.tar.gz",release="2.0.0",repo="myRepo",tag="2.0.0",user="myOrg"} 55`)). Status(http.StatusOK). End() }