11package main
22
33import (
4- "github.com/google/go-github/v32/github"
4+ "context"
5+
6+ "github.com/google/go-github/v33/github"
57 "github.com/prometheus/client_golang/prometheus"
68)
79
8- var histogramVec = prometheus .NewHistogramVec (prometheus.HistogramOpts {
9- Name : "workflow_execution_time_seconds" ,
10- Help : "Time that a workflow took to run." ,
11- Buckets : prometheus .ExponentialBuckets (1 , 1.4 , 30 ),
12- },
13- []string {"org" , "repo" , "workflow_name" },
10+ var (
11+ histogramVec = prometheus .NewHistogramVec (prometheus.HistogramOpts {
12+ Name : "workflow_execution_time_seconds" ,
13+ Help : "Time that a workflow took to run." ,
14+ Buckets : prometheus .ExponentialBuckets (1 , 1.4 , 30 ),
15+ },
16+ []string {"org" , "repo" , "workflow_name" },
17+ )
18+
19+ totalMinutesUsedActions = prometheus .NewGaugeVec (prometheus.GaugeOpts {
20+ Name : "actions_total_minutes_used_minutes" ,
21+ Help : "Total minutes used for the GitHub Actions." ,
22+ },
23+ []string {"org" , "user" },
24+ )
25+
26+ includedMinutesUsedActions = prometheus .NewGaugeVec (prometheus.GaugeOpts {
27+ Name : "actions_included_minutes" ,
28+ Help : "Included Minutes for the GitHub Actions." ,
29+ },
30+ []string {"org" , "user" },
31+ )
32+
33+ totalPaidMinutesActions = prometheus .NewGaugeVec (prometheus.GaugeOpts {
34+ Name : "actions_total_paid_minutes" ,
35+ Help : "Paid Minutes for the GitHub Actions." ,
36+ },
37+ []string {"org" , "user" },
38+ )
39+
40+ totalMinutesUsedUbuntuActions = prometheus .NewGaugeVec (prometheus.GaugeOpts {
41+ Name : "actions_total_minutes_used_ubuntu_minutes" ,
42+ Help : "Total minutes used for Ubuntu type for the GitHub Actions." ,
43+ },
44+ []string {"org" , "user" },
45+ )
46+
47+ totalMinutesUsedMacOSActions = prometheus .NewGaugeVec (prometheus.GaugeOpts {
48+ Name : "actions_total_minutes_used_macos_minutes" ,
49+ Help : "Total minutes used for MacOS type for the GitHub Actions." ,
50+ },
51+ []string {"org" , "user" },
52+ )
53+
54+ totalMinutesUsedWindowsActions = prometheus .NewGaugeVec (prometheus.GaugeOpts {
55+ Name : "actions_total_minutes_used_windows_minutes" ,
56+ Help : "Total minutes used for Windows type for the GitHub Actions." ,
57+ },
58+ []string {"org" , "user" },
59+ )
1460)
1561
1662func init () {
1763 //Register metrics with prometheus
1864 prometheus .MustRegister (histogramVec )
65+ prometheus .MustRegister (totalMinutesUsedActions )
66+ prometheus .MustRegister (includedMinutesUsedActions )
67+ prometheus .MustRegister (totalPaidMinutesActions )
68+ prometheus .MustRegister (totalMinutesUsedUbuntuActions )
69+ prometheus .MustRegister (totalMinutesUsedMacOSActions )
70+ prometheus .MustRegister (totalMinutesUsedWindowsActions )
1971}
2072
2173// CollectWorkflowRun collect the workflow execution run metric
@@ -33,3 +85,36 @@ func CollectWorkflowRun(checkRunEvent *github.CheckRunEvent) {
3385
3486 histogramVec .WithLabelValues (org , repo , workflowName ).Observe (executionTime )
3587}
88+
89+ // CollectActionBilling collect the action billing.
90+ func (c * GHActionExporter ) CollectActionBilling () {
91+ if * gitHubOrg != "" {
92+ actionsBilling , _ , err := c .GHClient .Billing .GetActionsBillingOrg (context .TODO (), * gitHubOrg )
93+ if err != nil {
94+ c .Logger .Log ("msg" , "failed to retrive the actions billing for an org" , "org" , * gitHubOrg , "err" , err )
95+ return
96+ }
97+
98+ totalMinutesUsedActions .WithLabelValues (* gitHubOrg , "" ).Set (float64 (actionsBilling .TotalMinutesUsed ))
99+ includedMinutesUsedActions .WithLabelValues (* gitHubOrg , "" ).Set (float64 (actionsBilling .IncludedMinutes ))
100+ totalPaidMinutesActions .WithLabelValues (* gitHubOrg , "" ).Set (float64 (actionsBilling .TotalPaidMinutesUsed ))
101+ totalMinutesUsedUbuntuActions .WithLabelValues (* gitHubOrg , "" ).Set (float64 (actionsBilling .MinutesUsedBreakdown .Ubuntu ))
102+ totalMinutesUsedMacOSActions .WithLabelValues (* gitHubOrg , "" ).Set (float64 (actionsBilling .MinutesUsedBreakdown .MacOS ))
103+ totalMinutesUsedWindowsActions .WithLabelValues (* gitHubOrg , "" ).Set (float64 (actionsBilling .MinutesUsedBreakdown .Windows ))
104+ }
105+
106+ if * gitHubUser != "" {
107+ actionsBilling , _ , err := c .GHClient .Billing .GetActionsBillingUser (context .TODO (), * gitHubUser )
108+ if err != nil {
109+ c .Logger .Log ("msg" , "failed to retrive the actions billing for an user" , "user" , * gitHubUser , "err" , err )
110+ return
111+ }
112+
113+ totalMinutesUsedActions .WithLabelValues ("" , * gitHubUser ).Set (float64 (actionsBilling .TotalMinutesUsed ))
114+ includedMinutesUsedActions .WithLabelValues ("" , * gitHubUser ).Set (float64 (actionsBilling .IncludedMinutes ))
115+ totalPaidMinutesActions .WithLabelValues ("" , * gitHubUser ).Set (float64 (actionsBilling .TotalPaidMinutesUsed ))
116+ totalMinutesUsedUbuntuActions .WithLabelValues ("" , * gitHubUser ).Set (float64 (actionsBilling .MinutesUsedBreakdown .Ubuntu ))
117+ totalMinutesUsedMacOSActions .WithLabelValues ("" , * gitHubUser ).Set (float64 (actionsBilling .MinutesUsedBreakdown .MacOS ))
118+ totalMinutesUsedWindowsActions .WithLabelValues ("" , * gitHubUser ).Set (float64 (actionsBilling .MinutesUsedBreakdown .Windows ))
119+ }
120+ }
0 commit comments