From f417342a8ce9540fcdb98077af6bdd3635792693 Mon Sep 17 00:00:00 2001 From: labbs Date: Wed, 20 Jan 2021 15:52:57 +0100 Subject: [PATCH 1/8] add the workflow_id --- metrics/get_jobs_from_github.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metrics/get_jobs_from_github.go b/metrics/get_jobs_from_github.go index 5603038..2683d71 100644 --- a/metrics/get_jobs_from_github.go +++ b/metrics/get_jobs_from_github.go @@ -18,7 +18,7 @@ var ( Name: "github_job", Help: "job status", }, - []string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "event", "status"}, + []string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "workflow_id", "event", "status"}, ) ) @@ -37,6 +37,7 @@ type job struct { Status string `json:"status"` Conclusion string `json:"conclusion"` UpdatedAt string `json:"updated_at"` + WorkflowID int `json:"workflow_id"` } func GetJobsFromGithub() { @@ -69,7 +70,7 @@ func GetJobsFromGithub() { } else if r.Status == "queued" { s = 4 } - JobsGauge.WithLabelValues(repo, strconv.Itoa(r.ID), r.NodeID, r.HeadBranch, r.HeadSha, strconv.Itoa(r.RunNumber), r.Event, r.Status).Set(s) + JobsGauge.WithLabelValues(repo, strconv.Itoa(r.ID), r.NodeID, r.HeadBranch, r.HeadSha, strconv.Itoa(r.RunNumber), strconv.Itoa(r.WorkflowID), r.Event, r.Status).Set(s) } } From 5db457811c6ae7b070b7cc1a553c52faf125c186 Mon Sep 17 00:00:00 2001 From: labbs Date: Wed, 20 Jan 2021 15:55:16 +0100 Subject: [PATCH 2/8] update doc --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 12cc91e..e7ff8f9 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Gauge type | node_id | Node ID (github actions) (mandatory ??) | | repo | Repository like \/\ | | run_number | Build id for the repo (incremental id => 1/2/3/4/...) | +| workflow_id | Workflow ID | | status | Workflow status (completed/in_progress) | ### github_runner_status From be179340c9a13fa3a555c5f339b11b6d30ffa134 Mon Sep 17 00:00:00 2001 From: labbs Date: Thu, 21 Jan 2021 18:41:23 +0100 Subject: [PATCH 3/8] enable cache --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index aeca0f7..45aaeca 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,7 @@ func main() { // runWeb start http server func runWeb(ctx *cli.Context) { + go metrics.WorkflowsCache() go metrics.GetRunnersFromGithub() go metrics.GetRunnersOrganizationFromGithub() go metrics.GetJobsFromGithub() From 0612ac4abd3c55838ef1e06240ce1372292af99f Mon Sep 17 00:00:00 2001 From: labbs Date: Thu, 21 Jan 2021 19:37:47 +0100 Subject: [PATCH 4/8] move workflow to cache --- metrics/get_billable_from_github.go | 45 +++++------------------------ 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/metrics/get_billable_from_github.go b/metrics/get_billable_from_github.go index f22a805..bc6db4a 100644 --- a/metrics/get_billable_from_github.go +++ b/metrics/get_billable_from_github.go @@ -22,19 +22,6 @@ var ( ) ) -type workflowsReturn struct { - TotalCount int `json:"total_count"` - Workflows []workflow `json:"workflows"` -} - -type workflow struct { - ID int `json:"id"` - NodeID string `json:"node_id"` - Name string `json:"name"` - Path string `json:"path"` - State string `json:"state"` -} - type UBUNTU struct { TotalMs float64 `json:"total_ms"` } @@ -59,41 +46,25 @@ func GetBillableFromGithub() { client := &http.Client{} for { for _, repo := range config.Github.Repositories { - var p workflowsReturn - req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows", nil) - req.Header.Set("Authorization", "token "+config.Github.Token) - resp, err := client.Do(req) - defer resp.Body.Close() - if err != nil { - log.Fatal(err) - } - if resp.StatusCode != 200 { - log.Fatalf("the status code returned by the server is different from 200: %d", resp.StatusCode) - } - err = json.NewDecoder(resp.Body).Decode(&p) - if err != nil { - log.Fatal(err) - } - - for _, w := range p.Workflows { + for k, v := range workflows[repo] { var bill Bill - req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows/"+strconv.Itoa(w.ID)+"/timing", nil) + req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows/"+strconv.Itoa(k)+"/timing", nil) req.Header.Set("Authorization", "token "+config.Github.Token) - resp2, err := client.Do(req) - defer resp2.Body.Close() + resp, err := client.Do(req) + defer resp.Body.Close() if err != nil { log.Fatal(err) } if resp.StatusCode != 200 { log.Fatalf("the status code returned by the server is different from 200: %d", resp.StatusCode) } - err = json.NewDecoder(resp2.Body).Decode(&bill) + err = json.NewDecoder(resp.Body).Decode(&bill) if err != nil { log.Fatal(err) } - WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(w.ID), w.NodeID, w.Name, w.State, "MACOS").Set(bill.Billable.MACOS.TotalMs / 1000) - WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(w.ID), w.NodeID, w.Name, w.State, "WINDOWS").Set(bill.Billable.WINDOWS.TotalMs / 1000) - WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(w.ID), w.NodeID, w.Name, w.State, "UBUNTU").Set(bill.Billable.UBUNTU.TotalMs / 1000) + WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(v.ID), v.NodeID, v.Name, v.State, "MACOS").Set(bill.Billable.MACOS.TotalMs / 1000) + WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(v.ID), v.NodeID, v.Name, v.State, "WINDOWS").Set(bill.Billable.WINDOWS.TotalMs / 1000) + WorkflowBillGauge.WithLabelValues(repo, strconv.Itoa(v.ID), v.NodeID, v.Name, v.State, "UBUNTU").Set(bill.Billable.UBUNTU.TotalMs / 1000) } } From 70c448e0cf36567cc1f6c7d07db17d3c8a8f0fd8 Mon Sep 17 00:00:00 2001 From: labbs Date: Thu, 21 Jan 2021 19:38:07 +0100 Subject: [PATCH 5/8] add workflow name from cache --- metrics/get_jobs_from_github.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metrics/get_jobs_from_github.go b/metrics/get_jobs_from_github.go index 2683d71..586794d 100644 --- a/metrics/get_jobs_from_github.go +++ b/metrics/get_jobs_from_github.go @@ -18,7 +18,7 @@ var ( Name: "github_job", Help: "job status", }, - []string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "workflow_id", "event", "status"}, + []string{"repo", "id", "node_id", "head_branch", "head_sha", "run_number", "workflow_id", "workflow", "event", "status"}, ) ) @@ -70,7 +70,7 @@ func GetJobsFromGithub() { } else if r.Status == "queued" { s = 4 } - JobsGauge.WithLabelValues(repo, strconv.Itoa(r.ID), r.NodeID, r.HeadBranch, r.HeadSha, strconv.Itoa(r.RunNumber), strconv.Itoa(r.WorkflowID), r.Event, r.Status).Set(s) + 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) } } From 860dccb4c1604ae0a6a298d48223dc1a4de414c6 Mon Sep 17 00:00:00 2001 From: labbs Date: Thu, 21 Jan 2021 19:38:16 +0100 Subject: [PATCH 6/8] add workflows cache --- metrics/worflows_cache.go | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 metrics/worflows_cache.go diff --git a/metrics/worflows_cache.go b/metrics/worflows_cache.go new file mode 100644 index 0000000..67e5d85 --- /dev/null +++ b/metrics/worflows_cache.go @@ -0,0 +1,56 @@ +package metrics + +import ( + "encoding/json" + "github-actions-exporter/config" + "log" + "net/http" + "time" +) + +var workflows = map[string]map[int]workflow{} + +type workflowsReturn struct { + TotalCount int `json:"total_count"` + Workflows []workflow `json:"workflows"` +} + +type workflow struct { + ID int `json:"id"` + NodeID string `json:"node_id"` + Name string `json:"name"` + Path string `json:"path"` + State string `json:"state"` +} + +func WorkflowsCache() { + client := &http.Client{} + + for { + for _, repo := range config.Github.Repositories { + var p workflowsReturn + req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows", nil) + req.Header.Set("Authorization", "token "+config.Github.Token) + resp, err := client.Do(req) + defer resp.Body.Close() + if err != nil { + log.Fatal(err) + } + if resp.StatusCode != 200 { + log.Fatalf("the status code returned by the server is different from 200: %d", resp.StatusCode) + } + err = json.NewDecoder(resp.Body).Decode(&p) + if err != nil { + log.Fatal(err) + } + + s := make(map[int]workflow) + for _, w := range p.Workflows { + s[w.ID] = w + } + + workflows[repo] = s + } + time.Sleep(time.Duration(60) * time.Second) + } +} From 93784b8a90e0b3d8600e380440d0e4fa6105224e Mon Sep 17 00:00:00 2001 From: labbs Date: Thu, 21 Jan 2021 19:43:41 +0100 Subject: [PATCH 7/8] update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e7ff8f9..bd640d8 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Gauge type | repo | Repository like \/\ | | run_number | Build id for the repo (incremental id => 1/2/3/4/...) | | workflow_id | Workflow ID | +| workflow | Workflow Name | | status | Workflow status (completed/in_progress) | ### github_runner_status From 8c0be062f4f54da882926ecb60fa05c95e13248e Mon Sep 17 00:00:00 2001 From: labbs Date: Thu, 21 Jan 2021 19:43:54 +0100 Subject: [PATCH 8/8] bump version --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 45aaeca..dcd2087 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ import ( "github-actions-exporter/metrics" ) -var version = "v1.4" +var version = "v1.5" // main init configuration func main() {