From 09e42fe19efcaf682a3ce8c85dd5360ab2062e85 Mon Sep 17 00:00:00 2001 From: zealgt Date: Wed, 4 Jan 2023 17:20:43 +0700 Subject: [PATCH] Support to set name label for each github_workflow_status metrics --- .gitignore | 2 +- README.md | 17 ++++++++++------- configs/config.example.yml | 5 +++-- src/metrics/fetchWorkflows.js | 11 ++++++++--- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 4286b59..7af50a7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ node_modules/ .DS_Store -config/config.yml +configs/config.yml # Logs logs diff --git a/README.md b/README.md index a70dc14..e1e3166 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Docker image : https://hub.docker.com/repository/docker/zealgt/github-actions-pr - Monitor API rate limit remaining - Support both Personal access tokens (classic) and Fine-grained personal access tokens - Require only necessary permissions +- Easy to setup!! ## Getting started @@ -45,11 +46,14 @@ runner: # optional org: organization_name workflow: # optional enabled: true - owner: owner_name # apply to all list (optional) + owner: owner_name # optional, apply to all list list: - - owner: owner_name # override workflow.owner (optional) + - owner: owner_name # optional, override workflow.owner + name: custom_name # optional, name for github_workflow_status repo: repo_name_1 - repo: repo_name_2 + - repo: repo_name_3 + - repo: repo_name_4 ``` ## Example metrics @@ -67,9 +71,8 @@ github_runner_status{id="145",name="Runner 3",os="Linux"} 1 # HELP github_workflow_status Latest workflow status for each repository (0=unknown, 1=queued, 2=in_progress, 3=cancelled, 4=failure, 5=success) # TYPE github_workflow_status gauge -github_workflow_status{repo_name="repo-1",workflow_name="Development"} 5 -github_workflow_status{repo_name="repo-2",workflow_name="Development"} 2 -github_workflow_status{repo_name="repo-3",workflow_name="Production"} 1 -github_workflow_status{repo_name="repo-4",workflow_name="Production"} 5 -github_workflow_status{repo_name="repo-5",workflow_name="Production"} 5 +github_workflow_status{name="custom_name",repo_name="repo_name_1",workflow_name="Development"} 5 +github_workflow_status{name="repo_name_2",repo_name="repo_name_2",workflow_name="Development"} 2 +github_workflow_status{name="repo_name_3",repo_name="repo_name_3",workflow_name="Production"} 1 +github_workflow_status{name="repo_name_4",repo_name="repo_name_4",workflow_name="Production"} 1 ``` diff --git a/configs/config.example.yml b/configs/config.example.yml index 2e553b8..4cd7afd 100644 --- a/configs/config.example.yml +++ b/configs/config.example.yml @@ -6,8 +6,9 @@ runner: # optional org: organization_name workflow: # optional enabled: true - owner: owner_name # apply to all list (optional) + owner: owner_name # optional, apply to all list list: - - owner: owner_name # override workflow.owner (optional) + - owner: owner_name # optional, override workflow.owner + name: custom_name # optional, name for github_workflow_status repo: repo_name_1 - repo: repo_name_2 diff --git a/src/metrics/fetchWorkflows.js b/src/metrics/fetchWorkflows.js index 76d77b8..b00987f 100644 --- a/src/metrics/fetchWorkflows.js +++ b/src/metrics/fetchWorkflows.js @@ -14,7 +14,7 @@ const mapStatus = { const workflowGauge = new Gauge({ name: 'github_workflow_status', help: 'Latest workflow status for each repository (0=unknown, 1=queued, 2=in_progress, 3=cancelled, 4=failure, 5=success)', - labelNames: ['repo_name', 'workflow_name'], + labelNames: ['name', 'repo_name', 'workflow_name'], }) const getWorkflowStatus = (status, conclusion) => { @@ -41,14 +41,19 @@ const fetchWorkflows = async () => { .map((v, i) => { if (v.status === 'fulfilled') return v.value?.data?.workflow_runs?.[0] console.log( - `Error while fetch workflow for repository name ${repoList[i]} - ${v.reason.toString()}` + `Error while fetch workflow for repository name ${list[i].repo} - ${v.reason.toString()}` ) }) .filter((v) => v) workflowList.forEach(({ repository, name, status, conclusion }) => { + const { name: displayName } = list.find((v) => v.repo === repository.name) || {} workflowGauge.set( - { repo_name: repository.name, workflow_name: name }, + { + name: displayName || repository.name, + repo_name: repository.name, + workflow_name: name, + }, getWorkflowStatus(status, conclusion) ) })