Make metrics uniform

This commit is contained in:
Cyber MacGeddon 2025-04-23 10:46:38 +01:00
parent 0fbe6f6676
commit 3c5f6edc54
7 changed files with 48 additions and 28 deletions

View file

@ -33,7 +33,7 @@ class AsyncProcessor:
self.pulsar_client = PulsarClient(**params) self.pulsar_client = PulsarClient(**params)
# Initialise metrics, records the parameters # Initialise metrics, records the parameters
ProcessorMetrics(id=self.id).info({ ProcessorMetrics(processor = self.id).info({
k: str(params[k]) k: str(params[k])
for k in params for k in params
if k != "id" if k != "id"

View file

@ -12,7 +12,7 @@ class ConsumerSpec(Spec):
def add(self, flow, processor, definition): def add(self, flow, processor, definition):
consumer_metrics = ConsumerMetrics( consumer_metrics = ConsumerMetrics(
flow.id, f"{flow.name}-{self.name}" processor = flow.id, flow = flow.name, name = self.name,
) )
consumer = Consumer( consumer = Consumer(

View file

@ -4,79 +4,89 @@ from prometheus_client import Counter
class ConsumerMetrics: class ConsumerMetrics:
def __init__(self, id, flow=None): def __init__(self, processor, flow, name):
self.id = id self.processor = processor
self.flow = flow self.flow = flow
self.name = name
if not hasattr(__class__, "state_metric"): if not hasattr(__class__, "state_metric"):
__class__.state_metric = Enum( __class__.state_metric = Enum(
'consumer_state', 'Consumer state', 'consumer_state', 'Consumer state',
["id", "flow"], ["processor", "flow", "name"],
states=['stopped', 'running'] states=['stopped', 'running']
) )
if not hasattr(__class__, "request_metric"): if not hasattr(__class__, "request_metric"):
__class__.request_metric = Histogram( __class__.request_metric = Histogram(
'request_latency', 'Request latency (seconds)', 'request_latency', 'Request latency (seconds)',
["id", "flow"], ["processor", "flow", "name"],
) )
if not hasattr(__class__, "processing_metric"): if not hasattr(__class__, "processing_metric"):
__class__.processing_metric = Counter( __class__.processing_metric = Counter(
'processing_count', 'Processing count', 'processing_count', 'Processing count',
["id", "flow", "status"] ["processor", "flow", "name"],
) )
if not hasattr(__class__, "rate_limit_metric"): if not hasattr(__class__, "rate_limit_metric"):
__class__.rate_limit_metric = Counter( __class__.rate_limit_metric = Counter(
'rate_limit_count', 'Rate limit event count', 'rate_limit_count', 'Rate limit event count',
["id", "flow"] ["processor", "flow", "name"],
) )
def process(self, status): def process(self, status):
__class__.processing_metric.labels( __class__.processing_metric.labels(
id=self.id, flow=self.flow, status=status processor = self.processor, flow=self.flow, status=status
).inc() ).inc()
def rate_limit(self): def rate_limit(self):
__class__.rate_limit_metric.labels( __class__.rate_limit_metric.labels(
id=self.id, flow=self.flow processor = self.processor, flow=self.flow
).inc() ).inc()
def state(self, state): def state(self, state):
__class__.state_metric.labels( __class__.state_metric.labels(
id=self.id, flow=self.flow processor = self.processor, flow=self.flow
).state(state) ).state(state)
def record_time(self): def record_time(self):
return __class__.request_metric.labels( return __class__.request_metric.labels(
id=self.id, flow=self.flow processor = self.processor, flow=self.flow
).time() ).time()
class ProducerMetrics: class ProducerMetrics:
def __init__(self, id, flow=None):
self.id = id def __init__(self, processor, flow, name):
self.processor = processor
self.flow = flow self.flow = flow
self.name = name
if not hasattr(__class__, "output_metric"): if not hasattr(__class__, "output_metric"):
__class__.output_metric = Counter( __class__.output_metric = Counter(
'output_count', 'Output items created', 'output_count', 'Output items created',
["id", "flow"] ["processor", "flow", "name"],
) )
def inc(self): def inc(self):
__class__.output_metric.labels(id=self.id, flow=self.flow).inc() __class__.output_metric.labels(
processor = self.processor, flow = self.flow, name = self.name
).inc()
class ProcessorMetrics: class ProcessorMetrics:
def __init__(self, id): def __init__(self, processor):
self.id = id self.processor = processor
if not hasattr(__class__, "processor_metric"): if not hasattr(__class__, "processor_metric"):
__class__.processor_metric = Info( __class__.processor_metric = Info(
'processor', 'Processor configuration', 'processor', 'Processor configuration',
["id"] ["processor"]
) )
def info(self, info): def info(self, info):
__class__.processor_metric.labels(id=self.id).info(info) __class__.processor_metric.labels(
processor = self.processor
).info(info)

View file

@ -11,7 +11,7 @@ class ProducerSpec(Spec):
def add(self, flow, processor, definition): def add(self, flow, processor, definition):
producer_metrics = ProducerMetrics( producer_metrics = ProducerMetrics(
flow.id, f"{flow.name}-{self.name}" processor = flow.id, flow = flow.name, name = self.name
) )
producer = Producer( producer = Producer(

View file

@ -117,7 +117,7 @@ class RequestResponseSpec(Spec):
def add(self, flow, processor, definition): def add(self, flow, processor, definition):
producer_metrics = ProducerMetrics( producer_metrics = ProducerMetrics(
flow.id, f"{flow.name}-{self.response_name}" process = flow.id, name = flow.name, name = self.response_name
) )
rr = self.impl( rr = self.impl(

View file

@ -13,7 +13,7 @@ class SubscriberSpec(Spec):
# FIXME: Metrics not used # FIXME: Metrics not used
subscriber_metrics = ConsumerMetrics( subscriber_metrics = ConsumerMetrics(
flow.id, f"{flow.name}-{self.name}" processor = flow.id, flow = flow.name, name = self.name
) )
subscriber = Subscriber( subscriber = Subscriber(

View file

@ -68,12 +68,22 @@ class Processor(AsyncProcessor):
} }
) )
config_request_metrics = ConsumerMetrics(id + "-config-request") config_request_metrics = ConsumerMetrics(
config_response_metrics = ProducerMetrics(id + "-config-response") processor = self.id, flow = None, name = "config-request"
config_push_metrics = ProducerMetrics(id + "-config-push") )
config_response_metrics = ProducerMetrics(
processor = self.id, flow = None, name = "config-response"
)
config_push_metrics = ProducerMetrics(
processor = self.id, flow = None, name = "config-push"
)
flow_request_metrics = ConsumerMetrics(id + "-flow-request") flow_request_metrics = ConsumerMetrics(
flow_response_metrics = ProducerMetrics(id + "-flow-response") processor = self.id, flow = None, name = "flow-request"
)
flow_response_metrics = ProducerMetrics(
processor = self.id, flow = None, name = "flow-response"
)
self.config_request_consumer = Consumer( self.config_request_consumer = Consumer(
taskgroup = self.taskgroup, taskgroup = self.taskgroup,