1import logging
2
3from prometheus_client import Counter, Histogram, start_http_server
4
5NAMESPACE = "skyembed"
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.embedding_performed = Counter(
24 name="embedding_performed",
25 namespace=NAMESPACE,
26 documentation="Number of embeddings performed",
27 labelnames=["status"],
28 )
29
30 self.embedding_duration = Histogram(
31 name="embedding_duration_seconds",
32 namespace=NAMESPACE,
33 buckets=(
34 0.001,
35 0.005,
36 0.01,
37 0.025,
38 0.05,
39 0.1,
40 0.25,
41 0.5,
42 1.0,
43 2.5,
44 5.0,
45 10.0,
46 ),
47 labelnames=["status"],
48 documentation="Time taken to create an embedding",
49 )
50
51 self.events_handled = Counter(
52 name="events_handled",
53 namespace=NAMESPACE,
54 documentation="Number of events handled",
55 labelnames=["kind", "status"],
56 )
57
58 self.upserts = Counter(
59 name="upserts",
60 namespace=NAMESPACE,
61 documentation="Number of database upserts",
62 labelnames=["kind", "status"],
63 )
64
65 self.upsert_duration = Histogram(
66 name="upsert_duration_seconds",
67 namespace=NAMESPACE,
68 buckets=(
69 0.001,
70 0.005,
71 0.01,
72 0.025,
73 0.05,
74 0.1,
75 0.25,
76 0.5,
77 1.0,
78 2.5,
79 5.0,
80 10.0,
81 ),
82 labelnames=["kind", "status"],
83 documentation="Time taken to perform an upsert",
84 )
85
86 self._initialized = True
87
88 def start_http(self, port: int, addr: str = "0.0.0.0"):
89 logger.info(f"Starting Prometheus client on {addr}:{port}")
90 start_http_server(port=port, addr=addr)
91 logger.info(f"Prometheus client running on {addr}:{port}")
92
93
94prom_metrics = PromMetrics()