33from prometheus_client import (CONTENT_TYPE_LATEST , REGISTRY ,
44 CollectorRegistry , generate_latest )
55from prometheus_client .multiprocess import MultiProcessCollector
6+ from typing import Dict , List
67from fastapi .requests import Request
78from fastapi .responses import Response
8- from gh_actions_exporter .types import WebHook
9+ from gh_actions_exporter .config import Relabel , RelabelType , Settings
10+ from gh_actions_exporter .types import WebHook , WorkflowJob
911from prometheus_client import Counter , Histogram
1012
1113
@@ -23,8 +25,8 @@ def prometheus_metrics(request: Request) -> Response:
2325
2426
2527class Metrics (object ):
26-
27- def __init__ ( self ):
28+ def __init__ ( self , settings : Settings ):
29+ self . settings = settings
2830 self .workflow_labelnames = [
2931 'repository' ,
3032 'workflow_name' ,
@@ -36,6 +38,9 @@ def __init__(self):
3638 'repository_visibility' ,
3739 'runner_type'
3840 ]
41+ for relabel in self .settings .job_relabelling :
42+ self .job_labelnames .append (relabel .label )
43+
3944 self .workflow_rebuild = Counter (
4045 'github_actions_workflow_rebuild_count' , 'The number of workflow rebuild' ,
4146 labelnames = self .workflow_labelnames
@@ -123,13 +128,38 @@ def runner_type(self, webhook: WebHook) -> str:
123128 return 'self-hosted'
124129 return 'github-hosted'
125130
126- def job_labels (self , webhook : WebHook ) -> dict :
131+ def relabel_job_labels (self , relabel : Relabel , labels : List [str ]) -> Dict [str , str or None ]:
132+ result = {
133+ relabel .label : relabel .default
134+ }
135+ for label in relabel .values :
136+ if label in labels :
137+ result [relabel .label ] = label
138+ return result
139+
140+ def relabel_job_names (self , relabel : Relabel , job : WorkflowJob ) -> dict :
141+ if job .status == 'queued' :
142+ return dict ()
143+ result = {
144+ relabel .label : relabel .default
145+ }
146+ for label in relabel .values :
147+ if label in job .runner_name :
148+ result [relabel .label ] = label
149+ return result
150+
151+ def job_labels (self , webhook : WebHook , settings : Settings ) -> dict :
127152 labels = dict (
128153 runner_type = self .runner_type (webhook ),
129154 job_name = webhook .workflow_job .name ,
130155 repository_visibility = webhook .repository .visibility ,
131156 repository = webhook .repository .full_name ,
132157 )
158+ for relabel in settings .job_relabelling :
159+ if relabel .type == RelabelType .label :
160+ labels .update (self .relabel_job_labels (relabel , webhook .workflow_job .labels ))
161+ elif relabel .type == RelabelType .name :
162+ labels .update (self .relabel_job_names (relabel , webhook .workflow_job ))
133163 return labels
134164
135165 def handle_workflow_rebuild (self , webhook : WebHook ):
@@ -165,8 +195,8 @@ def handle_workflow_duration(self, webhook: WebHook):
165195 - webhook .workflow_run .run_started_at .timestamp ())
166196 self .workflow_duration .labels (** labels ).observe (duration )
167197
168- def handle_job_status (self , webhook : WebHook ):
169- labels = self .job_labels (webhook )
198+ def handle_job_status (self , webhook : WebHook , settings : Settings ):
199+ labels = self .job_labels (webhook , settings )
170200 if webhook .workflow_job .conclusion :
171201 if webhook .workflow_job .conclusion == 'success' :
172202 self .job_status_success .labels (** labels ).inc ()
@@ -180,8 +210,8 @@ def handle_job_status(self, webhook: WebHook):
180210 elif webhook .workflow_job .status == 'queued' :
181211 self .job_status_queued .labels (** labels ).inc ()
182212
183- def handle_job_duration (self , webhook : WebHook ):
184- labels = self .job_labels (webhook )
213+ def handle_job_duration (self , webhook : WebHook , settings : Settings ):
214+ labels = self .job_labels (webhook , settings )
185215 if webhook .workflow_job .conclusion :
186216 duration = (webhook .workflow_job .completed_at .timestamp ()
187217 - webhook .workflow_job .started_at .timestamp ())
0 commit comments