Skip to content

Add gauge metric for number of self-hosted org runners #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions internal/server/billing_metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/google/go-github/v50/github"
"golang.org/x/oauth2"
)

type BillingMetricsExporter struct {
Expand All @@ -18,12 +17,7 @@ type BillingMetricsExporter struct {
}

func NewBillingMetricsExporter(logger log.Logger, opts Opts) *BillingMetricsExporter {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: opts.GitHubAPIToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client := getGithubClient(opts.GitHubAPIToken)

return &BillingMetricsExporter{
Logger: logger,
Expand Down
25 changes: 25 additions & 0 deletions internal/server/github_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package server

import (
"context"

"github.com/google/go-github/v50/github"
"golang.org/x/oauth2"
)

var client *github.Client

func getGithubClient(githubToken string) *github.Client {
if client != nil {
return client
}

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)

return client
}
14 changes: 11 additions & 3 deletions internal/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ var (

totalMinutesUsedUbuntuActions = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "actions_total_minutes_used_ubuntu_minutes",
Help: "Total minutes used for Ubuntu type for the GitHub Actions. To be deprecate, use actions_total_minutes_used_by_host_minutes",
Help: "Total minutes used for Ubuntu type for the GitHub Actions. To be deprecated, use actions_total_minutes_used_by_host_minutes",
},
[]string{"org", "user"},
)

totalMinutesUsedMacOSActions = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "actions_total_minutes_used_macos_minutes",
Help: "Total minutes used for MacOS type for the GitHub Actions. To be deprecate, use actions_total_minutes_used_by_host_minutes",
Help: "Total minutes used for MacOS type for the GitHub Actions. To be deprecated, use actions_total_minutes_used_by_host_minutes",
},
[]string{"org", "user"},
)

totalMinutesUsedWindowsActions = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "actions_total_minutes_used_windows_minutes",
Help: "Total minutes used for Windows type for the GitHub Actions. To be deprecate, use actions_total_minutes_used_by_host_minutes",
Help: "Total minutes used for Windows type for the GitHub Actions. To be deprecated, use actions_total_minutes_used_by_host_minutes",
},
[]string{"org", "user"},
)
Expand All @@ -88,6 +88,13 @@ var (
},
[]string{"org", "user", "host_type"},
)

numberOfSelfHostedRunners = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "github_actions_num_self_hosted_org_runners",
Help: "Number of self-hosted org runners for the GitHub Actions.",
},
[]string{"org"},
)
)

func init() {
Expand All @@ -104,6 +111,7 @@ func init() {
prometheus.MustRegister(totalMinutesUsedMacOSActions)
prometheus.MustRegister(totalMinutesUsedWindowsActions)
prometheus.MustRegister(totalMinutesUsedByHostTypeActions)
prometheus.MustRegister(numberOfSelfHostedRunners)
}

type WorkflowObserver interface {
Expand Down
61 changes: 61 additions & 0 deletions internal/server/runner_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package server

import (
"context"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/google/go-github/v50/github"
)

type RunnerMetricsExporter struct {
GHClient *github.Client
Logger log.Logger
Opts Opts
}

func NewRunnerMetricsExporter(logger log.Logger, opts Opts) *RunnerMetricsExporter {
client := getGithubClient(opts.GitHubAPIToken)

return &RunnerMetricsExporter{
GHClient: client,
Logger: logger,
Opts: opts,
}
}

func (e *RunnerMetricsExporter) StartOrgRunnerMetricsCollection(ctx context.Context) {
if e.Opts.GitHubOrg == "" {
_ = level.Info(e.Logger).Log("msg", "Github org is not set, no org runner metrics will be collected.")
return
}
if e.Opts.GitHubAPIToken == "" {
_ = level.Info(e.Logger).Log("msg", "Github token is not set, no org runner metrics will be collected.")
return
}

ticker := time.NewTicker(time.Duration(e.Opts.RunnersAPIPollSeconds) * time.Second)
go func() {
for {
select {
case <-ticker.C:
e.collectOrgRunnerMetrics(ctx)
case <-ctx.Done():
ticker.Stop()
_ = level.Info(e.Logger).Log("msg", "Stopped polling for org runner metrics.")
return
}
}
}()
}

func (e *RunnerMetricsExporter) collectOrgRunnerMetrics(ctx context.Context) {
runners, _, err := e.GHClient.Actions.ListOrganizationRunners(ctx, e.Opts.GitHubOrg, nil)

if err != nil {
_ = e.Logger.Log("msg", "Failed to retrieve org runners for org ", e.Opts.GitHubOrg, " ", err)
}

numberOfSelfHostedRunners.WithLabelValues(e.Opts.GitHubOrg).Set(float64(runners.TotalCount))
}
6 changes: 6 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Opts struct {
GitHubOrg string
GitHubUser string
BillingAPIPollSeconds int

GitHubRunnersRepos []string
RunnersAPIPollSeconds int
}

type Server struct {
Expand Down Expand Up @@ -55,6 +58,9 @@ func NewServer(logger log.Logger, opts Opts) *Server {
_ = level.Info(logger).Log("msg", fmt.Sprintf("not exporting user billing: %v", err))
}

runnerMetricsExporter := NewRunnerMetricsExporter(logger, opts)
runnerMetricsExporter.StartOrgRunnerMetricsCollection(context.TODO())

muxIngress := http.NewServeMux()
httpServerIngress := &http.Server{
Handler: muxIngress,
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (
gitHubOrg = kingpin.Flag("gh.github-org", "GitHub Organization.").Envar("GITHUB_ORG").Default("").String()
gitHubUser = kingpin.Flag("gh.github-user", "GitHub User.").Default("").String()
gitHubBillingPollingSeconds = kingpin.Flag("gh.billing-poll-seconds", "Frequency at which to poll billing API.").Default("5").Int()
gitHubRunnersRepos = kingpin.Flag("gh.runners-repos", "The name of the repositories where runners are configures.").Default("").Strings()
gitHubRunnersPollingSeconds = kingpin.Flag("gh.runners-poll-seconds", "Frequency at which to poll runners API.").Default("5").Int()
)

func init() {
Expand Down Expand Up @@ -62,6 +64,8 @@ func main() {
GitHubUser: *gitHubUser,
GitHubOrg: *gitHubOrg,
BillingAPIPollSeconds: *gitHubBillingPollingSeconds,
GitHubRunnersRepos: *gitHubRunnersRepos,
RunnersAPIPollSeconds: *gitHubRunnersPollingSeconds,
})
go func() {
err := srv.Serve(context.Background())
Expand Down