this repo has no description
at main 72 lines 1.9 kB view raw
1import logging 2 3from prometheus_client import Counter, Histogram, start_http_server 4 5NAMESPACE = "followgraph_indexer" 6 7logger = logging.getLogger(__name__) 8 9 10class PromMetrics: 11 _instance = None 12 13 def __new__(cls): 14 if cls._instance is None: 15 cls._instance = super().__new__(cls) 16 cls._instance._initialized = False 17 return cls._instance 18 19 def __init__(self): 20 if self._initialized: 21 return 22 23 self.events_received = Counter( 24 name="events_received", 25 namespace=NAMESPACE, 26 documentation="Number of events received", 27 ) 28 29 self.events_handled = Counter( 30 name="events_handled", 31 namespace=NAMESPACE, 32 documentation="Number of events handled", 33 labelnames=["kind", "status"], 34 ) 35 36 self.insert_duration = Histogram( 37 name="insert_duration_seconds", 38 namespace=NAMESPACE, 39 buckets=( 40 0.001, 41 0.005, 42 0.01, 43 0.025, 44 0.05, 45 0.1, 46 0.25, 47 0.5, 48 1.0, 49 2.5, 50 5.0, 51 10.0, 52 ), 53 labelnames=["kind", "status"], 54 documentation="Time taken to insert a batch", 55 ) 56 57 self.inserted = Counter( 58 name="inserted", 59 namespace=NAMESPACE, 60 documentation="Number of items inserted", 61 labelnames=["kind", "status"], 62 ) 63 64 self._initialized = True 65 66 def start_http(self, port: int, addr: str = "0.0.0.0"): 67 logger.info(f"Starting Prometheus client on {addr}:{port}") 68 start_http_server(port=port, addr=addr) 69 logger.info(f"Prometheus client running on {addr}:{port}") 70 71 72prom_metrics = PromMetrics()