-
Notifications
You must be signed in to change notification settings - Fork 3
/
collector.go
84 lines (66 loc) · 1.99 KB
/
collector.go
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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/google/go-github/v33/github"
"github.com/prometheus/client_golang/prometheus"
)
type githubRunners struct {
*github.Runners
}
var orgRunnerStatus = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "github_organization_runner_status",
Help: "Status of the self-hosted Github runners in the entire organization.",
},
[]string{"name", "id"},
)
func collectMetrics(t time.Duration) {
for {
orgRunners, err := listOfOrgRunners()
if err != nil {
log.Fatalf("error retrieving the list of organization errors: %s", err)
}
err = orgRunners.setRunnerStatusMetric()
if err != nil {
log.Fatalf("error setting the runner status metrics:", err)
}
time.Sleep(t)
}
}
func init() {
prometheus.MustRegister(orgRunnerStatus)
}
// listOfOrgRunners returns the runners in an organizations
func listOfOrgRunners() (*githubRunners, error) {
ctx := context.Background()
r, _, err := ghClient.Client.Actions.ListOrganizationRunners(ctx, ghClient.Organization, nil)
if err != nil {
return nil, fmt.Errorf("error retrieving runners: %s", err)
}
return &githubRunners{
Runners: r,
}, nil
}
// setRunnerStatusMetric sets the status of the runners; 0=offline, 1=idle/online, 2=active/busy: https://docs.github.com/en/actions/hosting-your-own-runners/monitoring-and-troubleshooting-self-hosted-runners
//
func (r *githubRunners) setRunnerStatusMetric() error {
for _, v := range r.Runners.Runners {
if *v.Status == "online" || *v.Status == "idle" {
if *v.Busy {
// Runner is online & busy (executing a job).
orgRunnerStatus.WithLabelValues(*v.Name, string(*v.ID)).Set(2)
} else {
// Runner is online & idle (waiting for job).
orgRunnerStatus.WithLabelValues(*v.Name, string(*v.ID)).Set(1)
}
} else if *v.Status == "offline" {
// Runner is offline.
orgRunnerStatus.WithLabelValues(*v.Name, string(*v.ID)).Set(0)
} else {
return fmt.Errorf("unknown status detected: %s", *v.Status)
}
}
return nil
}