@@ -2,6 +2,7 @@ package main
22
33import (
44 "errors"
5+ "fmt"
56 "time"
67
78 "github.com/google/go-github/v61/github"
@@ -10,6 +11,10 @@ import (
1011 "github.com/webdevops/go-common/utils/to"
1112)
1213
14+ const (
15+ CUSTOMPROP_LABEL_FMT = "prop_%s"
16+ )
17+
1318type (
1419 MetricsCollectorGithubWorkflows struct {
1520 collector.Processor
@@ -27,16 +32,24 @@ type (
2732func (m * MetricsCollectorGithubWorkflows ) Setup (collector * collector.Collector ) {
2833 m .Processor .Setup (collector )
2934
35+ var customPropLabels []string
36+ for _ , customProp := range Opts .GitHub .Repositories .CustomProperties {
37+ customPropLabels = append (customPropLabels , fmt .Sprintf (CUSTOMPROP_LABEL_FMT , customProp ))
38+ }
39+
3040 m .prometheus .repository = prometheus .NewGaugeVec (
3141 prometheus.GaugeOpts {
3242 Name : "github_repository_info" ,
3343 Help : "GitHub repository info" ,
3444 },
35- []string {
36- "org" ,
37- "repo" ,
38- "defaultBranch" ,
39- },
45+ append (
46+ []string {
47+ "org" ,
48+ "repo" ,
49+ "defaultBranch" ,
50+ },
51+ customPropLabels ... ,
52+ ),
4053 )
4154 m .Collector .RegisterMetricList ("repository" , m .prometheus .repository , true )
4255
@@ -45,13 +58,16 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
4558 Name : "github_workflow_info" ,
4659 Help : "GitHub workflow info" ,
4760 },
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+ ),
5571 )
5672 m .Collector .RegisterMetricList ("workflow" , m .prometheus .workflow , true )
5773
@@ -133,6 +149,30 @@ func (m *MetricsCollectorGithubWorkflows) getRepoList(org string) ([]*github.Rep
133149 opts .Page = response .NextPage
134150 }
135151
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+
136176 return repositories , nil
137177}
138178
@@ -213,11 +253,29 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
213253 }
214254
215255 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 {
217271 "org" : org ,
218272 "repo" : repo .GetName (),
219273 "defaultBranch" : to .String (repo .DefaultBranch ),
220- })
274+ }
275+ for labelName , labelValue := range propLabels {
276+ labels [labelName ] = labelValue
277+ }
278+ repositoryMetric .AddInfo (labels )
221279
222280 if repo .GetDefaultBranch () == "" {
223281 // repo doesn't have default branch
@@ -229,14 +287,19 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
229287 panic (err )
230288 }
231289
290+ // workflow info metrics
232291 for _ , workflow := range workflows {
233- workflowMetric . AddInfo ( prometheus.Labels {
292+ labels := prometheus.Labels {
234293 "org" : org ,
235294 "repo" : repo .GetName (),
236295 "workflow" : workflow .GetName (),
237296 "state" : workflow .GetState (),
238297 "path" : workflow .GetPath (),
239- })
298+ }
299+ for labelName , labelValue := range propLabels {
300+ labels [labelName ] = labelValue
301+ }
302+ workflowMetric .AddInfo (labels )
240303 }
241304
242305 if len (workflows ) >= 1 {
0 commit comments