mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 00:16:23 +02:00
feat: add type hints to all public functions in trustgraph/base Add type annotations to 23 modules covering: - Metrics classes (ConsumerMetrics, ProducerMetrics, etc.) - Spec classes (ConsumerSpec, ProducerSpec, SubscriberSpec, etc.) - Service classes with add_args() and run() methods - Utility functions (logging, pubsub, clients) - AsyncProcessor methods All 93 public functions now fully typed. Refs #785 * refactor: deduplicate imports and move __future__ after docstrings Addresses review feedback on PR #803: - Remove duplicate 'from argparse import ArgumentParser' across 12 files - Move 'from __future__ import annotations' to line 1 in all files - Clean up excessive blank lines
144 lines
4.6 KiB
Python
144 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from prometheus_client import start_http_server, Info, Enum, Histogram
|
|
from prometheus_client import Counter
|
|
|
|
class ConsumerMetrics:
|
|
"""
|
|
Metrics tracking and reporting for flow consumers.
|
|
|
|
This class manages prometheus metrics specifically related to consumers
|
|
within the flow, including state, requests, processing time, and queues.
|
|
"""
|
|
|
|
def __init__(self, processor: str, flow: str, name: str) -> None:
|
|
|
|
self.processor = processor
|
|
self.flow = flow
|
|
self.name = name
|
|
|
|
if not hasattr(__class__, "state_metric"):
|
|
__class__.state_metric = Enum(
|
|
'consumer_state', 'Consumer state',
|
|
["processor", "flow", "name"],
|
|
states=['stopped', 'running']
|
|
)
|
|
|
|
if not hasattr(__class__, "request_metric"):
|
|
__class__.request_metric = Histogram(
|
|
'request_latency', 'Request latency (seconds)',
|
|
["processor", "flow", "name"],
|
|
)
|
|
|
|
if not hasattr(__class__, "processing_metric"):
|
|
__class__.processing_metric = Counter(
|
|
'processing_count', 'Processing count',
|
|
["processor", "flow", "name", "status"],
|
|
)
|
|
|
|
if not hasattr(__class__, "rate_limit_metric"):
|
|
__class__.rate_limit_metric = Counter(
|
|
'rate_limit_count', 'Rate limit event count',
|
|
["processor", "flow", "name"],
|
|
)
|
|
|
|
def process(self, status: str) -> None:
|
|
__class__.processing_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name,
|
|
status=status
|
|
).inc()
|
|
|
|
def rate_limit(self) -> None:
|
|
__class__.rate_limit_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name,
|
|
).inc()
|
|
|
|
def state(self, state: str) -> None:
|
|
__class__.state_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name,
|
|
).state(state)
|
|
|
|
def record_time(self) -> Any:
|
|
return __class__.request_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name,
|
|
).time()
|
|
|
|
class ProducerMetrics:
|
|
|
|
def __init__(self, processor: str, flow: str, name: str) -> None:
|
|
|
|
self.processor = processor
|
|
self.flow = flow
|
|
self.name = name
|
|
|
|
if not hasattr(__class__, "producer_metric"):
|
|
__class__.producer_metric = Counter(
|
|
'producer_count', 'Output items produced',
|
|
["processor", "flow", "name"],
|
|
)
|
|
|
|
def inc(self) -> None:
|
|
__class__.producer_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name
|
|
).inc()
|
|
|
|
class ProcessorMetrics:
|
|
def __init__(self, processor: str) -> None:
|
|
|
|
self.processor = processor
|
|
|
|
if not hasattr(__class__, "processor_metric"):
|
|
__class__.processor_metric = Info(
|
|
'processor', 'Processor configuration',
|
|
["processor"]
|
|
)
|
|
|
|
def info(self, info: dict[str, str]) -> None:
|
|
__class__.processor_metric.labels(
|
|
processor = self.processor
|
|
).info(info)
|
|
|
|
class SubscriberMetrics:
|
|
|
|
def __init__(self, processor: str, flow: str, name: str) -> None:
|
|
|
|
self.processor = processor
|
|
self.flow = flow
|
|
self.name = name
|
|
|
|
if not hasattr(__class__, "state_metric"):
|
|
__class__.state_metric = Enum(
|
|
'subscriber_state', 'Subscriber state',
|
|
["processor", "flow", "name"],
|
|
states=['stopped', 'running']
|
|
)
|
|
|
|
if not hasattr(__class__, "received_metric"):
|
|
__class__.received_metric = Counter(
|
|
'received_count', 'Received count',
|
|
["processor", "flow", "name"],
|
|
)
|
|
|
|
if not hasattr(__class__, "dropped_metric"):
|
|
__class__.dropped_metric = Counter(
|
|
'dropped_count', 'Dropped messages count',
|
|
["processor", "flow", "name"],
|
|
)
|
|
|
|
def received(self) -> None:
|
|
__class__.received_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name,
|
|
).inc()
|
|
|
|
def state(self, state: str) -> None:
|
|
|
|
__class__.state_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name,
|
|
).state(state)
|
|
|
|
def dropped(self, state: str) -> None:
|
|
__class__.dropped_metric.labels(
|
|
processor = self.processor, flow = self.flow, name = self.name,
|
|
).inc()
|