Skip to content

Commit 0998c40

Browse files
committed
Merge branch 'release/v1.5'
2 parents 2e2bc95 + 8c0be06 commit 0998c40

5 files changed

+71
-40
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Gauge type
3737
| node_id | Node ID (github actions) (mandatory ??) |
3838
| repo | Repository like \<org>/\<repo> |
3939
| run_number | Build id for the repo (incremental id => 1/2/3/4/...) |
40+
| workflow_id | Workflow ID |
41+
| workflow | Workflow Name |
4042
| status | Workflow status (completed/in_progress) |
4143

4244
### github_runner_status

main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github-actions-exporter/metrics"
2020
)
2121

22-
var version = "v1.4"
22+
var version = "v1.5"
2323

2424
// main init configuration
2525
func main() {
@@ -34,6 +34,7 @@ func main() {
3434

3535
// runWeb start http server
3636
func runWeb(ctx *cli.Context) {
37+
go metrics.WorkflowsCache()
3738
go metrics.GetRunnersFromGithub()
3839
go metrics.GetRunnersOrganizationFromGithub()
3940
go metrics.GetJobsFromGithub()

metrics/get_billable_from_github.go

+8-37
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ var (
2222
)
2323
)
2424

25-
type workflowsReturn struct {
26-
TotalCount int `json:"total_count"`
27-
Workflows []workflow `json:"workflows"`
28-
}
29-
30-
type workflow struct {
31-
ID int `json:"id"`
32-
NodeID string `json:"node_id"`
33-
Name string `json:"name"`
34-
Path string `json:"path"`
35-
State string `json:"state"`
36-
}
37-
3825
type UBUNTU struct {
3926
TotalMs float64 `json:"total_ms"`
4027
}
@@ -59,41 +46,25 @@ func GetBillableFromGithub() {
5946
client := &http.Client{}
6047
for {
6148
for _, repo := range config.Github.Repositories {
62-
var p workflowsReturn
63-
req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows", nil)
64-
req.Header.Set("Authorization", "token "+config.Github.Token)
65-
resp, err := client.Do(req)
66-
defer resp.Body.Close()
67-
if err != nil {
68-
log.Fatal(err)
69-
}
70-
if resp.StatusCode != 200 {
71-
log.Fatalf("the status code returned by the server is different from 200: %d", resp.StatusCode)
72-
}
73-
err = json.NewDecoder(resp.Body).Decode(&p)
74-
if err != nil {
75-
log.Fatal(err)
76-
}
77-
78-
for _, w := range p.Workflows {
49+
for k, v := range workflows[repo] {
7950
var bill Bill
80-
req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows/"+strconv.Itoa(w.ID)+"/timing", nil)
51+
req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows/"+strconv.Itoa(k)+"/timing", nil)
8152
req.Header.Set("Authorization", "token "+config.Github.Token)
82-
resp2, err := client.Do(req)
83-
defer resp2.Body.Close()
53+
resp, err := client.Do(req)
54+
defer resp.Body.Close()
8455
if err != nil {
8556
log.Fatal(err)
8657
}
8758
if resp.StatusCode != 200 {
8859
log.Fatalf("the status code returned by the server is different from 200: %d", resp.StatusCode)
8960
}
90-
err = json.NewDecoder(resp2.Body).Decode(&bill)
61+
err = json.NewDecoder(resp.Body).Decode(&bill)
9162
if err != nil {
9263
log.Fatal(err)
9364
}
94-
WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(w.ID), w.NodeID, w.Name, w.State, "MACOS").Set(bill.Billable.MACOS.TotalMs / 1000)
95-
WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(w.ID), w.NodeID, w.Name, w.State, "WINDOWS").Set(bill.Billable.WINDOWS.TotalMs / 1000)
96-
WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(w.ID), w.NodeID, w.Name, w.State, "UBUNTU").Set(bill.Billable.UBUNTU.TotalMs / 1000)
65+
WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(v.ID), v.NodeID, v.Name, v.State, "MACOS").Set(bill.Billable.MACOS.TotalMs / 1000)
66+
WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(v.ID), v.NodeID, v.Name, v.State, "WINDOWS").Set(bill.Billable.WINDOWS.TotalMs / 1000)
67+
WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(v.ID), v.NodeID, v.Name, v.State, "UBUNTU").Set(bill.Billable.UBUNTU.TotalMs / 1000)
9768
}
9869
}
9970

metrics/get_jobs_from_github.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
Name: "github_job",
1919
Help: "job status",
2020
},
21-
[]string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "event", "status"},
21+
[]string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "workflow_id", "workflow", "event", "status"},
2222
)
2323
)
2424

@@ -37,6 +37,7 @@ type job struct {
3737
Status string `json:"status"`
3838
Conclusion string `json:"conclusion"`
3939
UpdatedAt string `json:"updated_at"`
40+
WorkflowID int `json:"workflow_id"`
4041
}
4142

4243
func GetJobsFromGithub() {
@@ -69,7 +70,7 @@ func GetJobsFromGithub() {
6970
} else if r.Status == "queued" {
7071
s = 4
7172
}
72-
JobsGauge.WithLabelValues(repo, strconv.Itoa(r.ID), r.NodeID, r.HeadBranch, r.HeadSha, strconv.Itoa(r.RunNumber), r.Event, r.Status).Set(s)
73+
JobsGauge.WithLabelValues(repo, strconv.Itoa(r.ID), r.NodeID, r.HeadBranch, r.HeadSha, strconv.Itoa(r.RunNumber), strconv.Itoa(r.WorkflowID), workflows[repo][r.WorkflowID].Name, r.Event, r.Status).Set(s)
7374
}
7475
}
7576

metrics/worflows_cache.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package metrics
2+
3+
import (
4+
"encoding/json"
5+
"github-actions-exporter/config"
6+
"log"
7+
"net/http"
8+
"time"
9+
)
10+
11+
var workflows = map[string]map[int]workflow{}
12+
13+
type workflowsReturn struct {
14+
TotalCount int `json:"total_count"`
15+
Workflows []workflow `json:"workflows"`
16+
}
17+
18+
type workflow struct {
19+
ID int `json:"id"`
20+
NodeID string `json:"node_id"`
21+
Name string `json:"name"`
22+
Path string `json:"path"`
23+
State string `json:"state"`
24+
}
25+
26+
func WorkflowsCache() {
27+
client := &http.Client{}
28+
29+
for {
30+
for _, repo := range config.Github.Repositories {
31+
var p workflowsReturn
32+
req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows", nil)
33+
req.Header.Set("Authorization", "token "+config.Github.Token)
34+
resp, err := client.Do(req)
35+
defer resp.Body.Close()
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
if resp.StatusCode != 200 {
40+
log.Fatalf("the status code returned by the server is different from 200: %d", resp.StatusCode)
41+
}
42+
err = json.NewDecoder(resp.Body).Decode(&p)
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
47+
s := make(map[int]workflow)
48+
for _, w := range p.Workflows {
49+
s[w.ID] = w
50+
}
51+
52+
workflows[repo] = s
53+
}
54+
time.Sleep(time.Duration(60) * time.Second)
55+
}
56+
}

0 commit comments

Comments
 (0)