chore: initialize monorepo with project scaffolding, configs, and CI setup
This commit is contained in:
40
apps/blockchain-node/src/aitbc_chain/metrics.py
Normal file
40
apps/blockchain-node/src/aitbc_chain/metrics.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from threading import Lock
|
||||
from typing import Dict
|
||||
|
||||
|
||||
@dataclass
|
||||
class MetricValue:
|
||||
name: str
|
||||
value: float
|
||||
|
||||
|
||||
class MetricsRegistry:
|
||||
def __init__(self) -> None:
|
||||
self._counters: Dict[str, float] = {}
|
||||
self._gauges: Dict[str, float] = {}
|
||||
self._lock = Lock()
|
||||
|
||||
def increment(self, name: str, amount: float = 1.0) -> None:
|
||||
with self._lock:
|
||||
self._counters[name] = self._counters.get(name, 0.0) + amount
|
||||
|
||||
def set_gauge(self, name: str, value: float) -> None:
|
||||
with self._lock:
|
||||
self._gauges[name] = value
|
||||
|
||||
def render_prometheus(self) -> str:
|
||||
with self._lock:
|
||||
lines: list[str] = []
|
||||
for name, value in sorted(self._counters.items()):
|
||||
lines.append(f"# TYPE {name} counter")
|
||||
lines.append(f"{name} {value}")
|
||||
for name, value in sorted(self._gauges.items()):
|
||||
lines.append(f"# TYPE {name} gauge")
|
||||
lines.append(f"{name} {value}")
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
metrics_registry = MetricsRegistry()
|
||||
Reference in New Issue
Block a user