-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathget_billable_from_github.go
More file actions
53 lines (45 loc) · 1.92 KB
/
get_billable_from_github.go
File metadata and controls
53 lines (45 loc) · 1.92 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
package metrics
import (
"context"
"strconv"
"strings"
"time"
"github.com/spendesk/github-actions-exporter/pkg/config"
"github.com/google/go-github/github"
"github.com/prometheus/client_golang/prometheus"
)
var (
workflowBillGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "github_workflow_usage_seconds",
Help: "Number of billable seconds used by a specific workflow during the current billing cycle. Any job re-runs are also included in the usage. Only apply to workflows in private repositories that use GitHub-hosted runners.",
},
[]string{"repo", "id", "node_id", "name", "state", "os"},
)
)
// getBillableFromGithub - return billable informations for MACOS, WINDOWS and UBUNTU runners.
func getBillableFromGithub() {
for {
for _, repo := range repositories {
for k, v := range workflows[repo] {
r := strings.Split(repo, "/")
for {
resp, _, err := client.Actions.GetWorkflowUsageByID(context.Background(), r[0], r[1], k)
if rl_err, ok := err.(*github.RateLimitError); ok {
logger.Infof("GetWorkflowUsageByID ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
logger.Infof("GetWorkflowUsageByID error for %s: %s", repo, err.Error())
break
}
workflowBillGauge.WithLabelValues(repo, strconv.FormatInt(*v.ID, 10), *v.NodeID, *v.Name, *v.State, "MACOS").Set(float64(resp.GetBillable().MacOS.GetTotalMS()) / 1000)
workflowBillGauge.WithLabelValues(repo, strconv.FormatInt(*v.ID, 10), *v.NodeID, *v.Name, *v.State, "WINDOWS").Set(float64(resp.GetBillable().Windows.GetTotalMS()) / 1000)
workflowBillGauge.WithLabelValues(repo, strconv.FormatInt(*v.ID, 10), *v.NodeID, *v.Name, *v.State, "UBUNTU").Set(float64(resp.GetBillable().Ubuntu.GetTotalMS()) / 1000)
break
}
}
}
time.Sleep(time.Duration(config.Github.Refresh) * 5 * time.Second)
}
}