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,40 @@ 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+ print (job .runner_name )
148+ print (label )
149+ if label in job .runner_name :
150+ result [relabel .label ] = label
151+ return result
152+
153+ def job_labels (self , webhook : WebHook , settings : Settings ) -> dict :
127154 labels = dict (
128155 runner_type = self .runner_type (webhook ),
129156 job_name = webhook .workflow_job .name ,
130157 repository_visibility = webhook .repository .visibility ,
131158 repository = webhook .repository .full_name ,
132159 )
160+ for relabel in settings .job_relabelling :
161+ if relabel .type == RelabelType .label :
162+ labels .update (self .relabel_job_labels (relabel , webhook .workflow_job .labels ))
163+ elif relabel .type == RelabelType .name :
164+ labels .update (self .relabel_job_names (relabel , webhook .workflow_job ))
133165 return labels
134166
135167 def handle_workflow_rebuild (self , webhook : WebHook ):
@@ -165,8 +197,8 @@ def handle_workflow_duration(self, webhook: WebHook):
165197 - webhook .workflow_run .run_started_at .timestamp ())
166198 self .workflow_duration .labels (** labels ).observe (duration )
167199
168- def handle_job_status (self , webhook : WebHook ):
169- labels = self .job_labels (webhook )
200+ def handle_job_status (self , webhook : WebHook , settings : Settings ):
201+ labels = self .job_labels (webhook , settings )
170202 if webhook .workflow_job .conclusion :
171203 if webhook .workflow_job .conclusion == 'success' :
172204 self .job_status_success .labels (** labels ).inc ()
@@ -180,8 +212,8 @@ def handle_job_status(self, webhook: WebHook):
180212 elif webhook .workflow_job .status == 'queued' :
181213 self .job_status_queued .labels (** labels ).inc ()
182214
183- def handle_job_duration (self , webhook : WebHook ):
184- labels = self .job_labels (webhook )
215+ def handle_job_duration (self , webhook : WebHook , settings : Settings ):
216+ labels = self .job_labels (webhook , settings )
185217 if webhook .workflow_job .conclusion :
186218 duration = (webhook .workflow_job .completed_at .timestamp ()
187219 - webhook .workflow_job .started_at .timestamp ())
0 commit comments