This repository was archived by the owner on Nov 24, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmetrics.py
More file actions
154 lines (127 loc) · 4.86 KB
/
Copy pathmetrics.py
File metadata and controls
154 lines (127 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Prometheus metrics for AI provider calls (Deepgram, OpenAI).
The AI enrichment runs in a call_processor subprocess, which records into its
own registry and hands a snapshot() back to the API process to merge().
"""
from typing import Any, Optional
from prometheus_client import (
CONTENT_TYPE_LATEST,
CollectorRegistry,
Counter,
Gauge,
GCCollector,
PlatformCollector,
ProcessCollector,
disable_created_metrics,
generate_latest,
)
PROVIDERS = ("deepgram", "openai")
# Known (provider, operation) pairs, exposed with value 0 before the first call.
OPERATIONS = (
("deepgram", "transcription"),
("deepgram", "realtime_transcription"),
("deepgram", "tts"),
("openai", "embedding"),
("openai", "clean"),
("openai", "summary"),
("openai", "sentiment"),
)
RESULTS = ("success", "failure")
UNKNOWN = -1
# Drop the *_created series: they add noise and nothing alerts on them.
disable_created_metrics()
_registry: CollectorRegistry
_credit_exhausted: Gauge
_calls: Counter
def reset() -> None:
"""Start from a fresh registry (process start, and between tests)."""
global _registry, _credit_exhausted, _calls
_registry = CollectorRegistry()
ProcessCollector(registry=_registry)
PlatformCollector(registry=_registry)
GCCollector(registry=_registry)
_credit_exhausted = Gauge(
"satellite_ai_credit_exhausted",
"1 if the provider reported no credit left, 0 if its last call succeeded, -1 if not known yet.",
["provider"],
registry=_registry,
)
_calls = Counter(
"satellite_ai_calls",
"AI provider calls by operation and result.",
["provider", "operation", "result"],
registry=_registry,
)
for provider in PROVIDERS:
_credit_exhausted.labels(provider).set(UNKNOWN)
for provider, operation in OPERATIONS:
for result in RESULTS:
_calls.labels(provider, operation, result)
reset()
def _status_code(error: BaseException) -> Optional[int]:
status = getattr(error, "status_code", None) # openai
if status is None:
response = getattr(error, "response", None) # httpx.HTTPStatusError
status = getattr(response, "status_code", None)
if status is None:
status = getattr(error, "status", None) # DeepgramApiError (str)
try:
return int(status)
except (TypeError, ValueError):
return None
def is_credit_error(error: Optional[BaseException]) -> bool:
"""True when the provider says the account has no credit left.
Deepgram answers 402 (Insufficient credits); OpenAI answers 429 with
code "insufficient_quota". Wrapped exceptions are followed via
__cause__/__context__.
"""
seen = set()
while error is not None and id(error) not in seen:
seen.add(id(error))
status = _status_code(error)
if status == 402:
return True
if status == 429 and getattr(error, "code", None) == "insufficient_quota":
return True
error = error.__cause__ or error.__context__
return False
def record_call(provider: str, operation: str, *, ok: bool, error: Optional[BaseException] = None) -> None:
_calls.labels(provider, operation, "success" if ok else "failure").inc()
if ok:
_credit_exhausted.labels(provider).set(0)
elif is_credit_error(error):
_credit_exhausted.labels(provider).set(1)
def snapshot() -> dict[str, Any]:
calls = []
for metric in _calls.collect():
for sample in metric.samples:
if sample.value:
labels = sample.labels
calls.append([labels["provider"], labels["operation"], labels["result"], int(sample.value)])
credit = {}
for metric in _credit_exhausted.collect():
for sample in metric.samples:
credit[sample.labels["provider"]] = int(sample.value)
return {"calls": calls, "credit_exhausted": credit}
def merge(data: Any) -> None:
"""Merge a snapshot() taken in another process. Malformed input is ignored."""
if not isinstance(data, dict):
return
calls = data.get("calls")
credit = data.get("credit_exhausted")
if isinstance(calls, list):
for item in calls:
if not (isinstance(item, list) and len(item) == 4):
continue
provider, operation, result, count = item
if result not in RESULTS or not isinstance(count, int) or count <= 0:
continue
_calls.labels(str(provider), str(operation), result).inc(count)
if isinstance(credit, dict):
for provider, value in credit.items():
if provider in PROVIDERS and value in (0, 1):
_credit_exhausted.labels(provider).set(value)
CONTENT_TYPE = CONTENT_TYPE_LATEST
def render() -> str:
"""Return the metrics in Prometheus text exposition format."""
return generate_latest(_registry).decode("utf-8")