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
9+ from gh_actions_exporter .config import Relabel , Settings
810from gh_actions_exporter .types import WebHook
911from prometheus_client import Counter , Histogram
1012
@@ -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,24 @@ 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 (self , relabel : Relabel , labels : List [str ]) -> Dict [str , str or None ]:
132+ result = {
133+ relabel .label : None
134+ }
135+ for label in relabel .values :
136+ if label in labels :
137+ result [relabel .label ] = label
138+ return result
139+
140+ def job_labels (self , webhook : WebHook , settings : Settings ) -> dict :
127141 labels = dict (
128142 runner_type = self .runner_type (webhook ),
129143 job_name = webhook .workflow_job .name ,
130144 repository_visibility = webhook .repository .visibility ,
131145 repository = webhook .repository .full_name ,
132146 )
147+ for relabel in settings .job_relabelling :
148+ labels .update (self .relabel_job (relabel , webhook .workflow_job .labels ))
133149 return labels
134150
135151 def handle_workflow_rebuild (self , webhook : WebHook ):
@@ -165,8 +181,8 @@ def handle_workflow_duration(self, webhook: WebHook):
165181 - webhook .workflow_run .run_started_at .timestamp ())
166182 self .workflow_duration .labels (** labels ).observe (duration )
167183
168- def handle_job_status (self , webhook : WebHook ):
169- labels = self .job_labels (webhook )
184+ def handle_job_status (self , webhook : WebHook , settings : Settings ):
185+ labels = self .job_labels (webhook , settings )
170186 if webhook .workflow_job .conclusion :
171187 if webhook .workflow_job .conclusion == 'success' :
172188 self .job_status_success .labels (** labels ).inc ()
@@ -180,8 +196,8 @@ def handle_job_status(self, webhook: WebHook):
180196 elif webhook .workflow_job .status == 'queued' :
181197 self .job_status_queued .labels (** labels ).inc ()
182198
183- def handle_job_duration (self , webhook : WebHook ):
184- labels = self .job_labels (webhook )
199+ def handle_job_duration (self , webhook : WebHook , settings : Settings ):
200+ labels = self .job_labels (webhook , settings )
185201 if webhook .workflow_job .conclusion :
186202 duration = (webhook .workflow_job .completed_at .timestamp ()
187203 - webhook .workflow_job .started_at .timestamp ())
0 commit comments