observability.prompt_turn_metrics
Prompt-turn completion metrics emission for agent-runtimes.
Emits OTEL metrics to the Datalayer OTEL service on every completed prompt turn. The exporter is configured lazily and reads:
- per-request user JWT token from transport route Authorization headers
DATALAYER_OTEL_SERVICE_NAMEfor service.name resource attributeDATALAYER_OTLP_METRICS_URL/OTEL_EXPORTER_OTLP_METRICS_ENDPOINT(explicit metrics endpoint override)DATALAYER_OTLP_URL/OTEL_EXPORTER_OTLP_ENDPOINT/DATALAYER_OTEL_RUN_URL(base OTLP endpoint)
extract_bearer_token
def extract_bearer_token(auth_header: str | None) -> str | None
Extract a bearer token from an Authorization header value.
extract_jwt_token
def extract_jwt_token(*header_values: str | None) -> str | None
Extract JWT token from common auth header value forms.
Supports:
Authorization: Bearer <jwt>Authorization: token <jwt>- raw JWT values (e.g., X-External-Token)
decode_user_uid
def decode_user_uid(token: str | None) -> str | None
Extract the Datalayer user_uid from a JWT token.
Follows the platform convention: payload.user.uid first,
then payload.sub.
extract_user_id_from_jwt
def extract_user_id_from_jwt(user_jwt_token: str | None) -> str | None
Best-effort JWT claim extraction without signature verification.
extract_identity_hints
def extract_identity_hints(
identities: list[dict[str, Any]] | None
) -> tuple[str | None, str | None, str | None]
Return (user_id, provider, jwt_token) from identities payload.
PromptTurnMetricsEmitter Objects
class PromptTurnMetricsEmitter()
Emits prompt-turn metrics through OTLP HTTP exporter.
record_prompt_turn_completion
def record_prompt_turn_completion(*,
prompt: str,
response: str,
duration_ms: float,
protocol: str,
stop_reason: str,
success: bool,
model: str | None,
tool_call_count: int,
input_tokens: int | None = None,
output_tokens: int | None = None,
total_tokens: int | None = None,
user_id: str | None = None,
user_provider: str | None = None,
identities_count: int | None = None,
user_jwt_token: str | None = None,
agent_id: str | None = None) -> None
Emit prompt-turn completion metrics.
This function is safe to call from request paths: failures are swallowed and logged without affecting prompt execution.