Skip to content

Commit 5572724

Browse files
committed
fix runner-type logic and add runner lables to job metrics
1 parent 9804f49 commit 5572724

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

docs/runner-type/runner-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Runner Types
2+
3+
Metrics are labelled with the runner type. The logic for determining the runner type is as follows -
4+
5+
If the runner tag is in the [github_hosted_runner_labels](./gh_actions_exporter/config.py) list the runner type will be `github-hosted` else the runner type will be `self-hosted`. Additional `github-hosted` runner tags can be added via config.

gh_actions_exporter/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class Settings(BaseSettings):
4747
github_app_id: Optional[int]
4848
github_app_installation_id: Optional[int]
4949
github_app_private_key: Optional[SecretStr]
50+
github_hosted_runner_labels: Optional[List[str]] = [
51+
"ubuntu-latest",
52+
"ubuntu-24.04",
53+
"ubuntu-22.04",
54+
"ubuntu-20.04",
55+
"windows-latest",
56+
"windows-2022",
57+
"windows-2019",
58+
"macos-latest",
59+
"macos-14",
60+
"macos-13",
61+
"macos-12",
62+
"macos-11"
63+
]
5064

5165
class Config:
5266
config: ConfigFile = ConfigFile()

gh_actions_exporter/metrics.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self, settings: Settings):
2525
self.job_labelnames = self.common_labelnames.copy() + [
2626
"job_name",
2727
"runner_type",
28+
"runner_labels",
2829
]
2930
for relabel in self.settings.job_relabelling:
3031
self.job_labelnames.append(relabel.label)
@@ -148,11 +149,14 @@ def workflow_labels(self, webhook: WebHook) -> dict:
148149
branch=branch,
149150
event=webhook.workflow_run.event,
150151
)
151-
152+
152153
def runner_type(self, webhook: WebHook) -> str:
153-
if "self-hosted" in webhook.workflow_job.labels:
154-
return "self-hosted"
155-
return "github-hosted"
154+
if set(webhook.workflow_job.labels) <= set(self.settings.github_hosted_runner_labels):
155+
return "github-hosted"
156+
return "self-hosted"
157+
158+
def runner_labels(self, webhook: WebHook) -> str:
159+
return ','.join(webhook.workflow_job.labels)
156160

157161
def relabel_job_labels(
158162
self, relabel: Relabel, labels: List[str]
@@ -180,6 +184,7 @@ def job_labels(self, webhook: WebHook, settings: Settings) -> dict:
180184
repository_visibility=webhook.repository.visibility,
181185
repository=webhook.repository.full_name,
182186
workflow_name=webhook.workflow_job.workflow_name,
187+
runner_labels=self.runner_labels(webhook),
183188
)
184189

185190
for relabel in settings.job_relabelling:

tests/api/metrics/test_job.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,20 @@ def test_skipped_job(override_job_config, client, workflow_job, headers):
125125
workflow_job["workflow_job"]["completed_at"] = "2021-11-29T14:59:57Z"
126126
response = client.post("/webhook", json=workflow_job, headers=headers)
127127
assert response.status_code == 202
128+
129+
@pytest.mark.parametrize("labels,expected_runner_type", [
130+
(["warp-ubuntu-latest-x64-2x"], "self-hosted"),
131+
(["self-hosted","linux"], "self-hosted"),
132+
(["ubuntu-latest"], "github-hosted")
133+
])
134+
def test_runner_type_and_labels(client, workflow_job, headers, labels, expected_runner_type):
135+
workflow_job["workflow_job"]["labels"] = labels
136+
response = client.post("/webhook", json=workflow_job, headers=headers)
137+
assert response.status_code == 202
138+
139+
metrics = client.get("/metrics")
140+
assert metrics.status_code == 200
141+
142+
for line in metrics.text.split("\n"):
143+
if "github_actions_job_start_duration_seconds_bucket{" in line:
144+
assert "runner_labels=\"%s\",runner_type=\"%s\"" % (','.join(labels), expected_runner_type) in line

0 commit comments

Comments
 (0)