Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69893de

Browse filesBrowse files
committed
Made additional metrics optional
1 parent 87e2ebe commit 69893de
Copy full SHA for 69893de

File tree

Expand file treeCollapse file tree

4 files changed

+74
-51
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+74
-51
lines changed
Open diff view settings
Collapse file

‎config/config.go‎

Copy file name to clipboardExpand all lines: config/config.go
+18-16Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import (
1313

1414
// Config struct holds all of the runtime confgiguration for the application
1515
type Config struct {
16-
Config *cfg.BaseConfig
17-
APIURL string
18-
Repositories []string
19-
Organisations []string
20-
Users []string
21-
APITokenEnv string
22-
APITokenFile string
23-
APIToken string
16+
Config *cfg.BaseConfig
17+
APIURL string
18+
Repositories []string
19+
Organisations []string
20+
Users []string
21+
APITokenEnv string
22+
APITokenFile string
23+
APIToken string
24+
AdditionalMetrics []string
2425
}
2526

2627
// Init populates the Config struct based on environmental runtime configuration
@@ -36,14 +37,15 @@ func Init() Config {
3637
}
3738

3839
appConfig := Config{
39-
Config: &ac,
40-
APIURL: cfg.GetEnv("API_URL", "https://api.github.com"),
41-
Repositories: strings.Split(os.Getenv("REPOS"), ","),
42-
Organisations: strings.Split(os.Getenv("ORGS"), ","),
43-
Users: strings.Split(os.Getenv("USERS"), ","),
44-
APITokenEnv: os.Getenv("GITHUB_TOKEN"),
45-
APITokenFile: tokenFile,
46-
APIToken: token,
40+
Config: &ac,
41+
APIURL: cfg.GetEnv("API_URL", "https://api.github.com"),
42+
Repositories: strings.Split(os.Getenv("REPOS"), ","),
43+
Organisations: strings.Split(os.Getenv("ORGS"), ","),
44+
Users: strings.Split(os.Getenv("USERS"), ","),
45+
APITokenEnv: os.Getenv("GITHUB_TOKEN"),
46+
APITokenFile: tokenFile,
47+
APIToken: token,
48+
AdditionalMetrics: strings.Split(os.Getenv("ADDITIONAL_METRICS"), ","),
4749
}
4850

4951
return appConfig
Collapse file

‎exporter/gather.go‎

Copy file name to clipboardExpand all lines: exporter/gather.go
+45-24Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ func (e *Exporter) gatherByOrg(client *github.Client) {
7878
opt.Page = resp.NextPage
7979
}
8080

81-
em := e.enrichMetrics(client, allRepos)
81+
e.enrichMetrics(client, allRepos)
8282

83-
e.Repositories = append(e.Repositories, em...)
8483
}
8584
}
8685

@@ -123,9 +122,8 @@ func (e *Exporter) gatherByUser(client *github.Client) {
123122
opt.Page = resp.NextPage
124123
}
125124

126-
em := e.enrichMetrics(client, allRepos)
125+
e.enrichMetrics(client, allRepos)
127126

128-
e.Repositories = append(e.Repositories, em...)
129127
}
130128
}
131129

@@ -176,41 +174,64 @@ func (e *Exporter) gatherByRepo(client *github.Client) {
176174
}
177175

178176
// Enrich the metrics
179-
em := e.enrichMetrics(client, allRepos)
177+
e.enrichMetrics(client, allRepos)
180178

181-
e.Repositories = append(e.Repositories, em...)
182179
}
183180
}
184181

182+
func (e *Exporter) metricEnabled(option string) bool {
183+
184+
for _, v := range e.Config.AdditionalMetrics {
185+
if v == option {
186+
return true
187+
}
188+
}
189+
190+
return false
191+
}
192+
185193
// Adds metrics not available in the standard repository response
186194
// Also adds them to the metrics struct format for processing
187-
func (e *Exporter) enrichMetrics(client *github.Client, repos []*github.Repository) []RepositoryMetrics {
188-
189-
// First, let's create an empty struct we can return
190-
em := []RepositoryMetrics{}
195+
func (e *Exporter) enrichMetrics(client *github.Client, repos []*github.Repository) {
191196

192197
// Let's then range over the repositories fed to the struct
193198
for _, y := range repos {
194199

200+
// TODO Stage a better word?
195201
// TODO - Fix pagination
196-
pulls, _, err := client.PullRequests.List(context.Background(), *y.Owner.Login, *y.Name, nil)
197-
if err != nil {
198-
e.Log.Errorf("Error obtaining pull metrics: %v", err)
202+
pulls := 0.0
203+
if e.metricEnabled("pulls") {
204+
p, _, err := client.PullRequests.List(context.Background(), *y.Owner.Login, *y.Name, nil)
205+
if err != nil {
206+
e.Log.Errorf("Error obtaining pull metrics: %v", err)
207+
}
208+
209+
pulls = float64(len(p))
199210
}
200211

201212
// TODO - Fix pagination
202-
releases, _, err := client.Repositories.ListReleases(context.Background(), *y.Owner.Login, *y.Name, nil)
203-
if err != nil {
204-
e.Log.Errorf("Error obtaining release metrics: %v", err)
213+
releases := 0.0
214+
if e.metricEnabled("releases") {
215+
r, _, err := client.Repositories.ListReleases(context.Background(), *y.Owner.Login, *y.Name, nil)
216+
if err != nil {
217+
e.Log.Errorf("Error obtaining release metrics: %v", err)
218+
}
219+
220+
releases = float64(len(r))
205221
}
206222

207223
// TODO - Fix pagination
208-
commits, _, err := client.Repositories.ListCommits(context.Background(), *y.Owner.Login, *y.Name, nil)
209-
if err != nil {
210-
e.Log.Errorf("Error obtaining commit metrics: %v", err)
224+
commits := 0.0
225+
if e.metricEnabled("commits") {
226+
c, _, err := client.Repositories.ListCommits(context.Background(), *y.Owner.Login, *y.Name, nil)
227+
if err != nil {
228+
e.Log.Errorf("Error obtaining commit metrics: %v", err)
229+
}
230+
releases = float64(len(c))
231+
211232
}
212233

213-
em = append(em, RepositoryMetrics{
234+
e.Repositories = append(e.Repositories, RepositoryMetrics{
214235
Name: e.derefString(y.Name),
215236
Owner: e.derefString(y.Owner.Login),
216237
Archived: strconv.FormatBool(e.derefBool(y.Archived)),
@@ -219,15 +240,15 @@ func (e *Exporter) enrichMetrics(client *github.Client, repos []*github.Reposito
219240
ForksCount: float64(e.derefInt(y.ForksCount)),
220241
WatchersCount: float64(e.derefInt(y.WatchersCount)),
221242
StargazersCount: float64(e.derefInt(y.StargazersCount)),
222-
PullsCount: float64(len(pulls)),
223-
CommitsCount: float64(len(commits)),
243+
PullsCount: pulls,
244+
CommitsCount: commits,
224245
OpenIssuesCount: float64(e.derefInt(y.OpenIssuesCount)),
225246
Size: float64(e.derefInt(y.Size)),
226-
Releases: float64(len(releases)),
247+
Releases: releases,
227248
License: y.License.GetKey(),
228249
Language: e.derefString(y.Language),
229250
})
251+
230252
}
231253

232-
return em
233254
}
Collapse file

‎exporter/metrics.go‎

Copy file name to clipboardExpand all lines: exporter/metrics.go
+10-5Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,30 @@ func (e *Exporter) derefInt(i *int) int {
106106
}
107107

108108
// processMetrics - processes the response data and sets the metrics using it as a source
109-
func (e *Exporter) processMetrics(ch chan<- prometheus.Metric) error {
109+
func (e *Exporter) processMetrics(ch chan<- prometheus.Metric) {
110110

111111
// Range through Repository metrics
112112
for _, x := range e.Repositories {
113113
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Stars"], prometheus.GaugeValue, x.StargazersCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
114114
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Forks"], prometheus.GaugeValue, x.ForksCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
115115
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Watchers"], prometheus.GaugeValue, x.WatchersCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
116116
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Size"], prometheus.GaugeValue, x.Size, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
117-
ch <- prometheus.MustNewConstMetric(e.APIMetrics["PullRequests"], prometheus.GaugeValue, x.PullsCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
118117
ch <- prometheus.MustNewConstMetric(e.APIMetrics["OpenIssues"], prometheus.GaugeValue, x.OpenIssuesCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
119-
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Releases"], prometheus.GaugeValue, x.Releases, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
120-
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Commits"], prometheus.GaugeValue, x.CommitsCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
121118

119+
if e.metricEnabled("pulls") {
120+
ch <- prometheus.MustNewConstMetric(e.APIMetrics["PullRequests"], prometheus.GaugeValue, x.PullsCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
121+
}
122+
if e.metricEnabled("releases") {
123+
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Releases"], prometheus.GaugeValue, x.Releases, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
124+
}
125+
if e.metricEnabled("commits") {
126+
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Commits"], prometheus.GaugeValue, x.CommitsCount, x.Name, x.Owner, x.Private, x.Fork, x.Archived, x.License, x.Language)
127+
}
122128
}
123129

124130
// Set Rate limit stats
125131
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Limit"], prometheus.GaugeValue, e.RateLimits.Limit)
126132
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Remaining"], prometheus.GaugeValue, e.RateLimits.Remaining)
127133
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Reset"], prometheus.GaugeValue, e.RateLimits.Reset)
128134

129-
return nil
130135
}
Collapse file

‎exporter/prometheus.go‎

Copy file name to clipboardExpand all lines: exporter/prometheus.go
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
3434
e.gatherRates(client)
3535

3636
// Set prometheus gauge metrics using the data gathered
37-
err := e.processMetrics(ch)
38-
39-
if err != nil {
40-
e.Log.Error("Error Processing Metrics", err)
41-
return
42-
}
37+
e.processMetrics(ch)
4338

4439
e.Log.Info("All Metrics successfully collected")
4540

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.