forked from ChelseaKR/fare-policy-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.py
More file actions
284 lines (249 loc) · 9.41 KB
/
Copy pathtelemetry.py
File metadata and controls
284 lines (249 loc) · 9.41 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""Privacy-safe structured runtime and GenAI telemetry.
All queryable fields are attached to :class:`logging.LogRecord` as ``extra``
attributes. Messages are fixed event names, never JSON strings, and no helper
in this module accepts prompt, question, response, history, citation, request
header, or exception-message content.
"""
from __future__ import annotations
import contextvars
import logging
import math
import os
import time
from collections.abc import Callable, Iterator
from contextlib import AbstractContextManager, contextmanager, nullcontext
from dataclasses import dataclass, field
from typing import Any
from assistant._vendor.genai_telemetry.attributes import (
GEN_AI_OPERATION_NAME,
GEN_AI_REQUEST_MODEL,
GEN_AI_RESPONSE_MODEL,
GEN_AI_SYSTEM,
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,
GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,
GEN_AI_USAGE_INPUT_TOKENS,
GEN_AI_USAGE_OUTPUT_TOKENS,
METRIC_OPERATION_DURATION,
PORTFOLIO_COST_USD,
)
_LOG = logging.getLogger("fare_assistant")
SpanFactory = Callable[[str, dict[str, object]], AbstractContextManager[Any]]
_span_factory: SpanFactory | None = None
_aws_request_id: contextvars.ContextVar[str | None] = contextvars.ContextVar(
"fare_assistant_aws_request_id",
default=None,
)
def function_version() -> str:
"""Return the immutable Lambda version, or a stable local-runtime label."""
return os.environ.get("AWS_LAMBDA_FUNCTION_VERSION") or "local"
@contextmanager
def request_correlation(aws_request_id: str | None) -> Iterator[None]:
"""Correlate one invocation without accepting a client-supplied identifier."""
value = aws_request_id if isinstance(aws_request_id, str) and aws_request_id else None
token = _aws_request_id.set(value)
try:
yield
finally:
_aws_request_id.reset(token)
def _common_fields(event: str) -> dict[str, object]:
return {
"event": event,
# Lambda's Python JSON formatter reserves/omits an ``aws_request_id``
# extra key. Use a distinct name, then require it to equal Lambda's
# built-in ``requestId`` during candidate verification.
"runtime_request_id": _aws_request_id.get(),
"function_version": function_version(),
}
def _emit(level: int, event: str, fields: dict[str, object]) -> None:
"""Emit a fixed-message structured record without exception information."""
_LOG.log(level, event, extra={**_common_fields(event), **fields})
def log_answer_request(
*,
kind: str,
language: str | None,
question_chars: int,
turns: int,
request_duration_ms: int,
cache: str,
model_called: bool,
structured_ok: bool | None,
status_code: int = 200,
direct_health: bool = False,
input_tokens: int = 0,
output_tokens: int = 0,
) -> None:
"""Record one terminal answer outcome using bounded, non-content fields."""
_emit(
logging.INFO,
"answer_request",
{
"kind": kind,
"language": language,
"question_chars": question_chars,
"turns": turns,
"duration_ms": request_duration_ms,
"request_duration_ms": request_duration_ms,
"cache": cache,
"model_called": model_called,
"structured_ok": structured_ok,
"status_code": status_code,
"direct_health": direct_health,
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"completion_recorded": model_called,
},
)
def log_feedback(*, verdict: str, kind: str | None, language: str | None) -> None:
"""Record a bounded feedback classification, never free-form client fields."""
_emit(
logging.INFO,
"feedback",
{
"verdict": verdict,
"kind": kind,
"language": language,
},
)
def log_handler_error(*, route: str, error_type: str) -> None:
"""Record only an exception class for an API handler failure."""
_emit(
logging.ERROR,
"handler_error",
{
"route": route,
"error_type": error_type,
},
)
def log_caller_rate_limited(*, route: str, limit: int) -> None:
"""Record that one caller exceeded its per-window quota.
Deliberately carries no caller key. The limiter works on a keyed, rotating
digest of the source address (``web.ratelimit``), and even that digest stays
out of the logs: emitting it would hand CloudWatch a pseudonymous identifier
that requests could be correlated on within a window, which is exactly the
property ADR 0019 keeps this service free of. The route and the quota that
was hit are enough to tell whether the limiter is working or misconfigured.
"""
_emit(
logging.INFO,
"caller_rate_limited",
{
"route": route,
"limit": limit,
},
)
def log_rate_limit_unavailable(*, route: str, error_type: str) -> None:
"""Record that the per-caller limiter failed open on a backend fault."""
_emit(
logging.WARNING,
"rate_limit_unavailable",
{
"route": route,
"error_type": error_type,
},
)
def log_spend_cutoff_served(*, route: str) -> None:
"""Record one request answered without a model call because spend is cut off."""
_emit(
logging.WARNING,
"spend_cutoff_served",
{
"route": route,
},
)
def log_corpus_version_mismatch(*, serving: str, pinned: str) -> None:
"""Surface a non-sensitive deployment-integrity warning as structured data."""
_emit(
logging.WARNING,
"corpus_version_mismatch",
{
"serving_corpus_version": serving,
"pinned_corpus_version": pinned,
},
)
def set_span_factory(factory: SpanFactory | None) -> None:
"""Install an optional tracer adapter; ``None`` restores the no-op default."""
global _span_factory
_span_factory = factory
@dataclass
class GenAICall:
attributes: dict[str, object] = field(default_factory=dict)
error_type: str | None = None
completion_recorded: bool = False
def record_completion(
self,
*,
model: str,
input_tokens: int,
output_tokens: int,
cost_usd: float | None,
cache_creation_input_tokens: int = 0,
cache_read_input_tokens: int = 0,
) -> None:
counts = (
input_tokens,
output_tokens,
cache_creation_input_tokens,
cache_read_input_tokens,
)
if any(type(count) is not int or count < 0 for count in counts):
raise ValueError("usage counts must be non-negative integers")
if cache_creation_input_tokens + cache_read_input_tokens > input_tokens:
raise ValueError("cache token buckets cannot exceed canonical input total")
if cost_usd is not None and (
isinstance(cost_usd, bool)
or not isinstance(cost_usd, (int, float))
or not math.isfinite(cost_usd)
or cost_usd < 0
):
raise ValueError("cost_usd must be finite and non-negative")
self.attributes.update(
{
GEN_AI_RESPONSE_MODEL: model,
GEN_AI_USAGE_INPUT_TOKENS: input_tokens,
GEN_AI_USAGE_OUTPUT_TOKENS: output_tokens,
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS: cache_creation_input_tokens,
GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS: cache_read_input_tokens,
}
)
if cost_usd is not None:
self.attributes[PORTFOLIO_COST_USD] = round(cost_usd, 6)
self.completion_recorded = True
@contextmanager
def genai_call(system: str, model: str) -> Iterator[GenAICall]:
"""Measure one non-streaming chat call without capturing prompt/response content."""
request_attributes: dict[str, object] = {
GEN_AI_OPERATION_NAME: "chat",
GEN_AI_SYSTEM: system,
GEN_AI_REQUEST_MODEL: model,
}
started = time.perf_counter()
manager = _span_factory("chat", request_attributes) if _span_factory else nullcontext(None)
with manager as span:
call = GenAICall()
try:
yield call
except Exception as exc:
call.error_type = type(exc).__name__
raise
finally:
duration = max(0.0, time.perf_counter() - started)
setter = getattr(span, "set_attribute", None)
if callable(setter):
for name, value in call.attributes.items():
setter(name, value)
if call.error_type is not None:
setter("error.type", call.error_type)
payload: dict[str, object] = {
**request_attributes,
**call.attributes,
METRIC_OPERATION_DURATION: duration,
"input_tokens": call.attributes.get(GEN_AI_USAGE_INPUT_TOKENS),
"output_tokens": call.attributes.get(GEN_AI_USAGE_OUTPUT_TOKENS),
"model_duration_ms": round(duration * 1000),
"estimated_cost_usd": call.attributes.get(PORTFOLIO_COST_USD),
"cost_estimate_available": PORTFOLIO_COST_USD in call.attributes,
"completion_recorded": call.completion_recorded,
"error_type": call.error_type,
}
level = logging.ERROR if call.error_type is not None else logging.INFO
_emit(level, "genai_call", payload)