-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathmetrics.go
More file actions
141 lines (122 loc) · 5.5 KB
/
metrics.go
File metadata and controls
141 lines (122 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package exporter
import (
"strconv"
"time"
"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 {
APIMetrics := make(map[string]*prometheus.Desc)
APIMetrics["Stars"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "stars"),
"Total number of Stars for given repository",
[]string{"repo", "user", "private", "fork", "archived", "license", "language"}, nil,
)
APIMetrics["OpenIssues"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "open_issues"),
"Total number of open issues for given repository",
[]string{"repo", "user", "private", "fork", "archived", "license", "language"}, nil,
)
APIMetrics["PullRequestCount"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "pull_request_count"),
"Total number of pull requests for given repository",
[]string{"repo", "user", "language"}, nil,
)
APIMetrics["PullRequestLongRunningCount"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "pull_request_long_running_count"),
"Total number of long running pull requests for given repository",
[]string{"repo", "user", "language"}, nil,
)
APIMetrics["Watchers"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "watchers"),
"Total number of watchers/subscribers for given repository",
[]string{"repo", "user", "private", "fork", "archived", "license", "language"}, nil,
)
APIMetrics["Forks"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "forks"),
"Total number of forks for given repository",
[]string{"repo", "user", "private", "fork", "archived", "license", "language"}, nil,
)
APIMetrics["Size"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "size_kb"),
"Size in KB for given repository",
[]string{"repo", "user", "private", "fork", "archived", "license", "language"}, nil,
)
APIMetrics["ReleaseDownloads"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "repo", "release_downloads"),
"Download count for a given release",
[]string{"repo", "user", "release", "name", "created_at"}, nil,
)
APIMetrics["Limit"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "rate", "limit"),
"Number of API queries allowed in a 60 minute window",
[]string{}, nil,
)
APIMetrics["Remaining"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "rate", "remaining"),
"Number of API queries remaining in the current window",
[]string{}, nil,
)
APIMetrics["Reset"] = prometheus.NewDesc(
prometheus.BuildFQName("github", "rate", "reset"),
"The time at which the current rate limit window resets in UTC epoch seconds",
[]string{}, nil,
)
return APIMetrics
}
// processMetrics - processes the response data and sets the metrics using it as a source
func (e *Exporter) processMetrics(data []*Datum, rates *RateLimits, ch chan<- prometheus.Metric) error {
currentTime := time.Now()
longRunningPRsTimeDiff := e.Config.PRLongRunningTimeDiff
// APIMetrics - range through the data slice
for _, x := range data {
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Stars"], prometheus.GaugeValue, x.Stars, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Forks"], prometheus.GaugeValue, x.Forks, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Watchers"], prometheus.GaugeValue, x.Watchers, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Size"], prometheus.GaugeValue, x.Size, x.Name, x.Owner.Login, strconv.FormatBool(x.Private), strconv.FormatBool(x.Fork), strconv.FormatBool(x.Archived), x.License.Key, x.Language)
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)
}
}
prCount := 0
prLongRunning := 0
for _, pull := range x.Pulls {
prCount++
if currentTime.Sub(pull.CreaatedAt).Hours() > longRunningPRsTimeDiff {
prLongRunning++
}
}
// fmt.Printf("prCount: %d | prLongRunning: %d ", prCount, prLongRunning)
// issueCount = x.OpenIssue - prCount
ch <- prometheus.MustNewConstMetric(e.APIMetrics["OpenIssues"],
prometheus.GaugeValue,
x.OpenIssues,
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,
x.Owner.Login,
x.Language)
// prCount
ch <- prometheus.MustNewConstMetric(e.APIMetrics["PullRequestLongRunningCount"],
prometheus.GaugeValue,
float64(prLongRunning),
x.Name,
x.Owner.Login,
x.Language)
}
// Set Rate limit stats
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Limit"], prometheus.GaugeValue, rates.Limit)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Remaining"], prometheus.GaugeValue, rates.Remaining)
ch <- prometheus.MustNewConstMetric(e.APIMetrics["Reset"], prometheus.GaugeValue, rates.Reset)
return nil
}