@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"time"
6
7
7
8
"github.com/google/go-github/v61/github"
@@ -10,6 +11,10 @@ import (
10
11
"github.com/webdevops/go-common/utils/to"
11
12
)
12
13
14
+ const (
15
+ CUSTOMPROP_LABEL_FMT = "prop_%s"
16
+ )
17
+
13
18
type (
14
19
MetricsCollectorGithubWorkflows struct {
15
20
collector.Processor
@@ -27,16 +32,24 @@ type (
27
32
func (m * MetricsCollectorGithubWorkflows ) Setup (collector * collector.Collector ) {
28
33
m .Processor .Setup (collector )
29
34
35
+ var customPropLabels []string
36
+ for _ , customProp := range Opts .GitHub .Repositories .CustomProperties {
37
+ customPropLabels = append (customPropLabels , fmt .Sprintf (CUSTOMPROP_LABEL_FMT , customProp ))
38
+ }
39
+
30
40
m .prometheus .repository = prometheus .NewGaugeVec (
31
41
prometheus.GaugeOpts {
32
42
Name : "github_repository_info" ,
33
43
Help : "GitHub repository info" ,
34
44
},
35
- []string {
36
- "org" ,
37
- "repo" ,
38
- "defaultBranch" ,
39
- },
45
+ append (
46
+ []string {
47
+ "org" ,
48
+ "repo" ,
49
+ "defaultBranch" ,
50
+ },
51
+ customPropLabels ... ,
52
+ ),
40
53
)
41
54
m .Collector .RegisterMetricList ("repository" , m .prometheus .repository , true )
42
55
@@ -45,13 +58,16 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
45
58
Name : "github_workflow_info" ,
46
59
Help : "GitHub workflow info" ,
47
60
},
48
- []string {
49
- "org" ,
50
- "repo" ,
51
- "workflow" ,
52
- "state" ,
53
- "path" ,
54
- },
61
+ append (
62
+ []string {
63
+ "org" ,
64
+ "repo" ,
65
+ "workflow" ,
66
+ "state" ,
67
+ "path" ,
68
+ },
69
+ customPropLabels ... ,
70
+ ),
55
71
)
56
72
m .Collector .RegisterMetricList ("workflow" , m .prometheus .workflow , true )
57
73
@@ -133,6 +149,30 @@ func (m *MetricsCollectorGithubWorkflows) getRepoList(org string) ([]*github.Rep
133
149
opts .Page = response .NextPage
134
150
}
135
151
152
+ if len (Opts .GitHub .Repositories .CustomProperties ) >= 1 {
153
+ for _ , repository := range repositories {
154
+ var err error
155
+ var repoCustomProperties []* github.CustomPropertyValue
156
+ for {
157
+ repoCustomProperties , _ , err = githubClient .Repositories .GetAllCustomPropertyValues (m .Context (), org , repository .GetName ())
158
+ var ghRateLimitError * github.RateLimitError
159
+ if ok := errors .As (err , & ghRateLimitError ); ok {
160
+ m .Logger ().Debugf ("GetAllCustomPropertyValues ratelimited. Pausing until %s" , ghRateLimitError .Rate .Reset .Time .String ())
161
+ time .Sleep (time .Until (ghRateLimitError .Rate .Reset .Time ))
162
+ continue
163
+ } else if err != nil {
164
+ panic (err )
165
+ }
166
+ break
167
+ }
168
+
169
+ repository .CustomProperties = map [string ]string {}
170
+ for _ , property := range repoCustomProperties {
171
+ repository .CustomProperties [property .PropertyName ] = property .GetValue ()
172
+ }
173
+ }
174
+ }
175
+
136
176
return repositories , nil
137
177
}
138
178
@@ -213,11 +253,29 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
213
253
}
214
254
215
255
for _ , repo := range repositories {
216
- repositoryMetric .AddInfo (prometheus.Labels {
256
+ // build custom properties
257
+ propLabels := prometheus.Labels {}
258
+ if len (Opts .GitHub .Repositories .CustomProperties ) >= 1 {
259
+ for _ , customProp := range Opts .GitHub .Repositories .CustomProperties {
260
+ labelName := fmt .Sprintf (CUSTOMPROP_LABEL_FMT , customProp )
261
+ propLabels [labelName ] = ""
262
+
263
+ if val , exists := repo .CustomProperties [customProp ]; exists {
264
+ propLabels [labelName ] = val
265
+ }
266
+ }
267
+ }
268
+
269
+ // repo info metric
270
+ labels := prometheus.Labels {
217
271
"org" : org ,
218
272
"repo" : repo .GetName (),
219
273
"defaultBranch" : to .String (repo .DefaultBranch ),
220
- })
274
+ }
275
+ for labelName , labelValue := range propLabels {
276
+ labels [labelName ] = labelValue
277
+ }
278
+ repositoryMetric .AddInfo (labels )
221
279
222
280
if repo .GetDefaultBranch () == "" {
223
281
// repo doesn't have default branch
@@ -229,14 +287,19 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
229
287
panic (err )
230
288
}
231
289
290
+ // workflow info metrics
232
291
for _ , workflow := range workflows {
233
- workflowMetric . AddInfo ( prometheus.Labels {
292
+ labels := prometheus.Labels {
234
293
"org" : org ,
235
294
"repo" : repo .GetName (),
236
295
"workflow" : workflow .GetName (),
237
296
"state" : workflow .GetState (),
238
297
"path" : workflow .GetPath (),
239
- })
298
+ }
299
+ for labelName , labelValue := range propLabels {
300
+ labels [labelName ] = labelValue
301
+ }
302
+ workflowMetric .AddInfo (labels )
240
303
}
241
304
242
305
if len (workflows ) >= 1 {
0 commit comments