|
13 | 13 | from typing import TYPE_CHECKING |
14 | 14 |
|
15 | 15 | if TYPE_CHECKING: |
16 | | - from typing import Any, Dict, List, Optional |
| 16 | + from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple |
| 17 | + from typing_extensions import TypedDict |
| 18 | + |
17 | 19 | from sentry_sdk.tracing import Span |
18 | 20 |
|
| 21 | + # Source paths: list of attribute chains to try in order. |
| 22 | + # e.g. [("meta", "billed_units", "input_tokens"), ("meta", "tokens", "input_tokens")] |
| 23 | + SourcePaths = Sequence[Tuple[str, ...]] |
| 24 | + |
| 25 | + # Maps a SPANDATA key to source paths on the response object. |
| 26 | + # e.g. {SPANDATA.GEN_AI_RESPONSE_ID: [("id",)]} |
| 27 | + SourceMapping = Dict[str, SourcePaths] |
| 28 | + |
| 29 | + class UsageConfig(TypedDict, total=False): |
| 30 | + """Declarative token usage extraction paths (from response object).""" |
| 31 | + |
| 32 | + input_tokens: SourcePaths |
| 33 | + output_tokens: SourcePaths |
| 34 | + total_tokens: SourcePaths |
| 35 | + |
| 36 | + class ResponseConfig(TypedDict, total=False): |
| 37 | + """Declarative response span data config.""" |
| 38 | + |
| 39 | + # Attributes always extracted from the response object. |
| 40 | + sources: SourceMapping |
| 41 | + # Attributes extracted only when PII sending is enabled. |
| 42 | + pii_sources: SourceMapping |
| 43 | + # Custom extractor for response text (PII only). |
| 44 | + # Returns list of text strings, or None. |
| 45 | + extract_text: Callable[[Any], Optional[List[str]]] |
| 46 | + # Declarative token usage paths. |
| 47 | + usage: UsageConfig |
| 48 | + |
| 49 | + class OperationConfig(TypedDict, total=False): |
| 50 | + """Full declarative config for an AI operation (chat, embeddings, etc.).""" |
| 51 | + |
| 52 | + # Key/value pairs set on every span unconditionally. |
| 53 | + static: Dict[str, Any] |
| 54 | + # Maps kwarg names to SPANDATA keys (always set if present in kwargs). |
| 55 | + params: Dict[str, str] |
| 56 | + # Maps kwarg names to SPANDATA keys (only set when PII is enabled). |
| 57 | + pii_params: Dict[str, str] |
| 58 | + # Extracts messages from kwargs for the span. |
| 59 | + extract_messages: Callable[[Dict[str, Any]], Optional[List[Dict[str, Any]]]] |
| 60 | + # SPANDATA key for messages (default: GEN_AI_REQUEST_MESSAGES). |
| 61 | + message_target: str |
| 62 | + # Non-streaming response config. |
| 63 | + response: ResponseConfig |
| 64 | + # Streaming response config (different attribute paths). |
| 65 | + stream_response: ResponseConfig |
| 66 | + # Source paths to extract a full response object from a stream-end event |
| 67 | + # (V1 pattern: reuse "response" config after extracting). |
| 68 | + stream_response_object: SourcePaths |
| 69 | + |
19 | 70 |
|
20 | 71 | def set_request_span_data(span, kwargs, integration, config, span_data=None): |
21 | 72 | # type: (Span, Dict[str, Any], Any, Dict[str, Any], Dict[str, Any] | None) -> None |
22 | | - """ |
23 | | - Set input span data from a declarative config. |
24 | | -
|
25 | | - Config keys: |
26 | | - static: dict - key/value pairs to set unconditionally |
27 | | - params: dict - kwargs key -> span attr (always set if present) |
28 | | - pii_params: dict - kwargs key -> span attr (only when PII allowed) |
29 | | - extract_messages: callable(kwargs) -> list or None |
30 | | - message_target: str - span attr for messages (default: GEN_AI_REQUEST_MESSAGES) |
31 | | -
|
32 | | - span_data: additional key/value pairs for dynamic per-call values |
33 | | - """ |
| 73 | + """Set request/static span data from a declarative config.""" |
34 | 74 | for key, value in config.get("static", {}).items(): |
35 | 75 | set_data_normalized(span, key, value) |
36 | 76 | if span_data: |
@@ -66,16 +106,7 @@ def set_response_span_data( |
66 | 106 | span, response, include_pii, response_config, collected_text=None |
67 | 107 | ): |
68 | 108 | # type: (Span, Any, bool, Dict[str, Any], Optional[List[str]]) -> None |
69 | | - """ |
70 | | - Set response span data from a declarative config. |
71 | | -
|
72 | | - response_config keys: |
73 | | - sources: dict - always set from response object |
74 | | - pii_sources: dict - only when PII allowed |
75 | | - extract_text: (response) -> list[str] | None (PII only) |
76 | | - usage: dict with input_tokens/output_tokens source paths |
77 | | - collected_text: pre-collected streaming text (overrides extract_text) |
78 | | - """ |
| 109 | + """Set response span data from a declarative config.""" |
79 | 110 | set_span_data_from_sources( |
80 | 111 | span, response, response_config.get("sources", {}), require_truthy=False |
81 | 112 | ) |
|
0 commit comments