parlot.core

parlot-core: shared semantic conventions, base processor, and utilities.

  1"""parlot-core: shared semantic conventions, base processor, and utilities."""
  2
  3from .attrs import *  # noqa: F401, F403 — re-export all attribute constants
  4from .context import ParlotContext
  5from .escalation import human_escalation, record_human_rep
  6from .metadata import set_session_attribute, set_session_metadata
  7from .platform_refs import add_platform_ref, stamp_framework_refs, stamp_platform_refs
  8from .processor import ParlotBaseProcessor, assert_sync_span_processors
  9from .provider import (
 10    adopt_existing_tracer_provider,
 11    attach_span_processor_if_absent,
 12    build_filtered_tracer_provider,
 13    build_otlp_http_exporter,
 14    build_parlot_client_headers,
 15    build_resource,
 16    build_tracer_provider,
 17    resolve_api_key,
 18    resolve_capture_genai_content,
 19    resolve_endpoint,
 20    resolve_service_name,
 21    try_set_global_tracer_provider,
 22    HEADER_SDK_NAME,
 23    HEADER_SDK_VERSION,
 24    HEADER_INGESTION_VERSION,
 25    INGESTION_PROTOCOL_VERSION,
 26)
 27from .bootstrap import fetch_telemetry_bootstrap
 28from .parlotize import (
 29    BaseParlotizeResult,
 30    ParlotizeProtocol,
 31    base_parlotize,
 32    configure_parlot_logging,
 33    log_parlotize_ready,
 34    normalize_glob_or_bool,
 35)
 36from .genai_content_capture import should_capture_genai_content
 37from .export import ExportFilterSpanExporter, QuietOTLPSpanExporter
 38from .intent import derive_intent
 39from .logs_capture import should_capture_logs
 40from .recording import should_record
 41from .recording_source import RecordingSource, stamp_external_recording
 42from .recording_stamp import (
 43    stamp_recording_disabled,
 44    stamp_recording_error,
 45    stamp_recording_started,
 46    stamp_recording_uploaded,
 47)
 48from .recording_timeline import (
 49    media_segments_from_speech,
 50    set_recording_anchor_wall_ms,
 51    speech_wall_ms_from_span,
 52)
 53from .recording_upload import (
 54    UploadGrantOutcome,
 55    complete_recording,
 56    fetch_upload_grant,
 57    put_bytes_to_presigned_url,
 58    recording_grant_error,
 59)
 60from .metrics import (
 61    LONG_LIVED_METRICS_EXPORT_INTERVAL_MS,
 62    ParlotMetricsRecorder,
 63    QuietOTLPMetricExporter,
 64    UsageMetricsRecorder,
 65    build_parlot_meter_provider,
 66)
 67from .runtime import ParlotRuntimeContext, runtime_from_bootstrap
 68from .sdk_version import resolve_parlot_sdk_version, stamp_session_sdk_version
 69from .session import (
 70    SessionState,
 71    clear_active_session,
 72    get_active_session,
 73    get_active_session_span,
 74    session_owned,
 75    set_active_session,
 76)
 77from .session_lifecycle import (
 78    DEFAULT_CLOSE_FLUSH_ATTEMPT_TIMEOUT_MS,
 79    DEFAULT_CLOSE_FLUSH_BACKOFFS_S,
 80    DEFAULT_CLOSE_FLUSH_BUDGET_S,
 81    emit_parlot_session_close,
 82    end_session_span_ok,
 83    force_flush_tracer_provider,
 84    force_flush_with_budget,
 85    start_parlot_session_span,
 86)
 87from .session_logs import SessionLogsCollector
 88from .session_registry import ConversationSessionRegistry
 89from .span_rename import apply_genai_span_rename, remap_native_span_name
 90from .topology import SessionTopology
 91from .turn_emit import emit_parlot_turn_root, stamp_turn_utterance_text
 92
 93__all__ = [
 94    "BaseParlotizeResult",
 95    "ConversationSessionRegistry",
 96    "DEFAULT_CLOSE_FLUSH_ATTEMPT_TIMEOUT_MS",
 97    "DEFAULT_CLOSE_FLUSH_BACKOFFS_S",
 98    "DEFAULT_CLOSE_FLUSH_BUDGET_S",
 99    "ParlotizeProtocol",
100    "ParlotContext",
101    "ParlotRuntimeContext",
102    "SessionLogsCollector",
103    "apply_genai_span_rename",
104    "attach_span_processor_if_absent",
105    "base_parlotize",
106    "build_filtered_tracer_provider",
107    "configure_parlot_logging",
108    "emit_parlot_session_close",
109    "emit_parlot_turn_root",
110    "end_session_span_ok",
111    "ExportFilterSpanExporter",
112    "QuietOTLPSpanExporter",
113    "force_flush_tracer_provider",
114    "force_flush_with_budget",
115    "LONG_LIVED_METRICS_EXPORT_INTERVAL_MS",
116    "log_parlotize_ready",
117    "normalize_glob_or_bool",
118    "ParlotBaseProcessor",
119    "ParlotMetricsRecorder",
120    "QuietOTLPMetricExporter",
121    "UsageMetricsRecorder",
122    "assert_sync_span_processors",
123    "SessionState",
124    "SessionTopology",
125    "add_platform_ref",
126    "adopt_existing_tracer_provider",
127    "build_otlp_http_exporter",
128    "build_parlot_client_headers",
129    "build_parlot_meter_provider",
130    "build_resource",
131    "build_tracer_provider",
132    "HEADER_SDK_NAME",
133    "HEADER_SDK_VERSION",
134    "HEADER_INGESTION_VERSION",
135    "INGESTION_PROTOCOL_VERSION",
136    "clear_active_session",
137    "derive_intent",
138    "fetch_telemetry_bootstrap",
139    "get_active_session",
140    "get_active_session_span",
141    "human_escalation",
142    "record_human_rep",
143    "remap_native_span_name",
144    "resolve_api_key",
145    "resolve_capture_genai_content",
146    "resolve_endpoint",
147    "resolve_service_name",
148    "resolve_parlot_sdk_version",
149    "runtime_from_bootstrap",
150    "session_owned",
151    "set_active_session",
152    "set_session_attribute",
153    "set_session_metadata",
154    "should_capture_genai_content",
155    "should_record",
156    "RecordingSource",
157    "stamp_external_recording",
158    "stamp_recording_disabled",
159    "stamp_recording_error",
160    "stamp_recording_started",
161    "stamp_recording_uploaded",
162    "set_recording_anchor_wall_ms",
163    "media_segments_from_speech",
164    "speech_wall_ms_from_span",
165    "UploadGrantOutcome",
166    "fetch_upload_grant",
167    "recording_grant_error",
168    "complete_recording",
169    "put_bytes_to_presigned_url",
170    "should_capture_logs",
171    "stamp_framework_refs",
172    "stamp_platform_refs",
173    "stamp_session_sdk_version",
174    "stamp_turn_utterance_text",
175    "start_parlot_session_span",
176    "try_set_global_tracer_provider",
177]
@dataclass
class BaseParlotizeResult:
134@dataclass
135class BaseParlotizeResult:
136    """Resolved context from ``base_parlotize`` for framework adapters."""
137
138    context: ParlotContext
139    endpoint: str
140    api_key: str
141    tracer_provider: Any
142    agent_id: str
143    agent_version: str
144    capture_genai_content: Optional[bool]
145    capture_logs: bool | list[str] | None
146    log_level: Optional[str]
147    # OTel service.name: service_name kwarg or agent_id.
148    service_name: str = ""

Resolved context from base_parlotize for framework adapters.

BaseParlotizeResult( context: ParlotContext, endpoint: str, api_key: str, tracer_provider: Any, agent_id: str, agent_version: str, capture_genai_content: bool | None, capture_logs: bool | list[str] | None, log_level: str | None, service_name: str = '')
context: ParlotContext
endpoint: str
api_key: str
tracer_provider: Any
agent_id: str
agent_version: str
capture_genai_content: bool | None
capture_logs: bool | list[str] | None
log_level: str | None
service_name: str = ''
@dataclass
class ConversationSessionRegistry(typing.Generic[~S]):
 26@dataclass
 27class ConversationSessionRegistry(Generic[S]):
 28    """Keyed map of open ``parlot.session`` spans + framework session state.
 29
 30    Used by conversation/thread-scoped adapters (Pipecat, LangGraph). LiveKit
 31    keeps its job ContextVar bootstrap and does not use this registry.
 32    """
 33
 34    _sessions: dict[str, tuple[Span, S]] = field(default_factory=dict)
 35    _atexit_registered: bool = False
 36    _atexit_reason: str = "process_exit"
 37
 38    def get(self, key: str) -> tuple[Span, S] | None:
 39        return self._sessions.get(key)
 40
 41    def get_state(self, key: str) -> S | None:
 42        pair = self._sessions.get(key)
 43        return pair[1] if pair else None
 44
 45    def keys(self) -> list[str]:
 46        return list(self._sessions)
 47
 48    def values(self) -> list[tuple[Span, S]]:
 49        return list(self._sessions.values())
 50
 51    def __len__(self) -> int:
 52        return len(self._sessions)
 53
 54    def __bool__(self) -> bool:
 55        return bool(self._sessions)
 56
 57    def last_opened_state(self) -> S | None:
 58        """Insertion-order last session (single-bot local runners)."""
 59        if not self._sessions:
 60            return None
 61        _span, state = next(reversed(list(self._sessions.values())))
 62        return state
 63
 64    def put(self, key: str, span: Span, state: S) -> None:
 65        self._sessions[key] = (span, state)
 66        set_active_session(span, state)
 67
 68    def ensure(
 69        self,
 70        key: str,
 71        factory: Callable[[], tuple[Span, S] | None],
 72    ) -> S | None:
 73        """Return existing session (rebinding active) or create via ``factory``.
 74
 75        ``factory`` returns ``(span, state)`` or ``None`` when it cannot open
 76        (e.g. no tracer).
 77        """
 78        existing = self._sessions.get(key)
 79        if existing is not None:
 80            span, state = existing
 81            set_active_session(span, state)
 82            return state
 83        created = factory()
 84        if created is None:
 85            return None
 86        span, state = created
 87        self.put(key, span, state)
 88        return state
 89
 90    def pop(self, key: str) -> tuple[Span, S] | None:
 91        return self._sessions.pop(key, None)
 92
 93    def close(
 94        self,
 95        key: str,
 96        *,
 97        closer: Callable[[Span, S], None],
 98        clear_active: bool = True,
 99    ) -> bool:
100        """Pop ``key`` and run ``closer(session_span, state)``.
101
102        Returns True when a session was closed.
103        """
104        pair = self._sessions.pop(key, None)
105        if pair is None:
106            return False
107        session_span, state = pair
108        closer(session_span, state)
109        if clear_active:
110            active = get_active_session()
111            if active is state:
112                clear_active_session()
113        return True
114
115    def end_and_clear(self, session_span: Span, state: S) -> None:
116        """End the session span OK and clear active when it matches ``state``."""
117        end_session_span_ok(session_span)
118        active = get_active_session()
119        if active is state:
120            clear_active_session()
121
122    def flush_all(self, closer: Callable[[str], None]) -> None:
123        for key in list(self._sessions):
124            closer(key)
125
126    def ensure_atexit(self, flush_all: Callable[[], None]) -> None:
127        if self._atexit_registered:
128            return
129        atexit.register(flush_all)
130        self._atexit_registered = True

Keyed map of open parlot.session spans + framework session state.

Used by conversation/thread-scoped adapters (Pipecat, LangGraph). LiveKit keeps its job ContextVar bootstrap and does not use this registry.

ConversationSessionRegistry( _sessions: dict[str, tuple[opentelemetry.trace.span.Span, ~S]] = <factory>, _atexit_registered: bool = False, _atexit_reason: str = 'process_exit')
def get(self, key: str) -> tuple[opentelemetry.trace.span.Span, ~S] | None:
38    def get(self, key: str) -> tuple[Span, S] | None:
39        return self._sessions.get(key)
def get_state(self, key: str) -> ~S | None:
41    def get_state(self, key: str) -> S | None:
42        pair = self._sessions.get(key)
43        return pair[1] if pair else None
def keys(self) -> list[str]:
45    def keys(self) -> list[str]:
46        return list(self._sessions)
def values(self) -> list[tuple[opentelemetry.trace.span.Span, ~S]]:
48    def values(self) -> list[tuple[Span, S]]:
49        return list(self._sessions.values())
def last_opened_state(self) -> ~S | None:
57    def last_opened_state(self) -> S | None:
58        """Insertion-order last session (single-bot local runners)."""
59        if not self._sessions:
60            return None
61        _span, state = next(reversed(list(self._sessions.values())))
62        return state

Insertion-order last session (single-bot local runners).

def put(self, key: str, span: opentelemetry.trace.span.Span, state: ~S) -> None:
64    def put(self, key: str, span: Span, state: S) -> None:
65        self._sessions[key] = (span, state)
66        set_active_session(span, state)
def ensure( self, key: str, factory: Callable[[], tuple[opentelemetry.trace.span.Span, ~S] | None]) -> ~S | None:
68    def ensure(
69        self,
70        key: str,
71        factory: Callable[[], tuple[Span, S] | None],
72    ) -> S | None:
73        """Return existing session (rebinding active) or create via ``factory``.
74
75        ``factory`` returns ``(span, state)`` or ``None`` when it cannot open
76        (e.g. no tracer).
77        """
78        existing = self._sessions.get(key)
79        if existing is not None:
80            span, state = existing
81            set_active_session(span, state)
82            return state
83        created = factory()
84        if created is None:
85            return None
86        span, state = created
87        self.put(key, span, state)
88        return state

Return existing session (rebinding active) or create via factory.

factory returns (span, state) or None when it cannot open (e.g. no tracer).

def pop(self, key: str) -> tuple[opentelemetry.trace.span.Span, ~S] | None:
90    def pop(self, key: str) -> tuple[Span, S] | None:
91        return self._sessions.pop(key, None)
def close( self, key: str, *, closer: Callable[[opentelemetry.trace.span.Span, ~S], NoneType], clear_active: bool = True) -> bool:
 93    def close(
 94        self,
 95        key: str,
 96        *,
 97        closer: Callable[[Span, S], None],
 98        clear_active: bool = True,
 99    ) -> bool:
100        """Pop ``key`` and run ``closer(session_span, state)``.
101
102        Returns True when a session was closed.
103        """
104        pair = self._sessions.pop(key, None)
105        if pair is None:
106            return False
107        session_span, state = pair
108        closer(session_span, state)
109        if clear_active:
110            active = get_active_session()
111            if active is state:
112                clear_active_session()
113        return True

Pop key and run closer(session_span, state).

Returns True when a session was closed.

def end_and_clear(self, session_span: opentelemetry.trace.span.Span, state: ~S) -> None:
115    def end_and_clear(self, session_span: Span, state: S) -> None:
116        """End the session span OK and clear active when it matches ``state``."""
117        end_session_span_ok(session_span)
118        active = get_active_session()
119        if active is state:
120            clear_active_session()

End the session span OK and clear active when it matches state.

def flush_all(self, closer: Callable[[str], NoneType]) -> None:
122    def flush_all(self, closer: Callable[[str], None]) -> None:
123        for key in list(self._sessions):
124            closer(key)
def ensure_atexit(self, flush_all: Callable[[], NoneType]) -> None:
126    def ensure_atexit(self, flush_all: Callable[[], None]) -> None:
127        if self._atexit_registered:
128            return
129        atexit.register(flush_all)
130        self._atexit_registered = True
DEFAULT_CLOSE_FLUSH_ATTEMPT_TIMEOUT_MS = 4000
DEFAULT_CLOSE_FLUSH_BACKOFFS_S = (1.0, 3.0, 6.0)
DEFAULT_CLOSE_FLUSH_BUDGET_S = 12.0
@runtime_checkable
class ParlotizeProtocol(typing.Protocol):
 80@runtime_checkable
 81class ParlotizeProtocol(Protocol):
 82    """Common parameters every adapter ``parlotize()`` must accept.
 83
 84    Framework packages may add extra keyword-only parameters (e.g. LiveKit
 85    ``record=`` / ``auto_escalate_sip=``).
 86    """
 87
 88    def __call__(
 89        self,
 90        agent_id: str,
 91        *,
 92        endpoint: Optional[str] = None,
 93        api_key: Optional[str] = None,
 94        capture_genai_content: Optional[bool] = None,
 95        service_name: Optional[str] = None,
 96        tracer_provider: TracerProvider | None = None,
 97        version: Optional[str] = None,
 98        capture_logs: bool | list[str] | None = None,
 99        log_level: Optional[str] = None,
100        **kwargs: Any,
101    ) -> ParlotContext:
102        """Shared parameter surface for every adapter ``parlotize()``.
103
104        Args:
105            agent_id: Required canonical deployment identity stamped on
106                ``session.agent_id``. Must be non-empty after stripping whitespace.
107            endpoint: Parlot OTLP collector base URL (e.g.
108                ``https://ingest.parlot.ai``). Spans export to
109                ``{endpoint}/v1/traces``. If omitted, reads ``PARLOT_ENDPOINT``.
110            api_key: Org-scoped API key minted in Parlot **Settings → API Keys**.
111                If omitted, reads ``PARLOT_API_KEY``. Required for remote telemetry
112                bootstrap and recording grants.
113            capture_genai_content: Process-wide override for LLM message bodies and
114                tool input/output payloads. If ``False``, payloads are omitted while
115                preserving span durations, tokens, and turn text. Precedence: job
116                metadata → this kwarg → Settings → Generative AI (default: on).
117            service_name: OpenTelemetry resource ``service.name``. Defaults to
118                ``agent_id`` when omitted or blank.
119            tracer_provider: Existing OpenTelemetry ``TracerProvider`` to adopt. If
120                omitted, adapters build one with Parlot's OTLP exporter (or adopt an
121                already-registered provider when another adapter configured first).
122            version: Deployment version stamped on ``gen_ai.agent.version``.
123                Pass ``version=`` to set it; otherwise ``\"unknown\"``.
124            capture_logs: Intercept Python ``logging`` during active sessions and
125                stream to the session Logs tab. Boolean or agent-id glob patterns.
126                Precedence: job metadata → this kwarg → Settings → Logs (default: on).
127            log_level: Minimum level for session log capture (e.g. ``"INFO"``,
128                ``"WARNING"``). Defaults to ``"INFO"``.
129            **kwargs: Framework-specific options (ignored by the shared surface).
130        """
131        ...

Common parameters every adapter parlotize() must accept.

Framework packages may add extra keyword-only parameters (e.g. LiveKit record= / auto_escalate_sip=).

ParlotizeProtocol(*args, **kwargs)
1866def _no_init_or_replace_init(self, *args, **kwargs):
1867    cls = type(self)
1868
1869    if cls._is_protocol:
1870        raise TypeError('Protocols cannot be instantiated')
1871
1872    # Already using a custom `__init__`. No need to calculate correct
1873    # `__init__` to call. This can lead to RecursionError. See bpo-45121.
1874    if cls.__init__ is not _no_init_or_replace_init:
1875        return
1876
1877    # Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`.
1878    # The first instantiation of the subclass will call `_no_init_or_replace_init` which
1879    # searches for a proper new `__init__` in the MRO. The new `__init__`
1880    # replaces the subclass' old `__init__` (ie `_no_init_or_replace_init`). Subsequent
1881    # instantiation of the protocol subclass will thus use the new
1882    # `__init__` and no longer call `_no_init_or_replace_init`.
1883    for base in cls.__mro__:
1884        init = base.__dict__.get('__init__', _no_init_or_replace_init)
1885        if init is not _no_init_or_replace_init:
1886            cls.__init__ = init
1887            break
1888    else:
1889        # should not happen
1890        cls.__init__ = object.__init__
1891
1892    cls.__init__(self, *args, **kwargs)
@dataclass
class ParlotContext:
13@dataclass
14class ParlotContext:
15    """Owns bootstrap runtime and process-local collectors for one SDK instance."""
16
17    runtime: Optional[ParlotRuntimeContext] = None
18    session_logs: SessionLogsCollector = field(default_factory=SessionLogsCollector)
19
20    def __post_init__(self) -> None:
21        self.session_logs.bind_context(self)
22
23    def shutdown(self) -> None:
24        self.session_logs.shutdown()
25        self.runtime = None

Owns bootstrap runtime and process-local collectors for one SDK instance.

ParlotContext( runtime: ParlotRuntimeContext | None = None, session_logs: SessionLogsCollector = <factory>)
runtime: ParlotRuntimeContext | None = None
session_logs: SessionLogsCollector
def shutdown(self) -> None:
23    def shutdown(self) -> None:
24        self.session_logs.shutdown()
25        self.runtime = None
@dataclass(frozen=True)
class ParlotRuntimeContext:
12@dataclass(frozen=True)
13class ParlotRuntimeContext:
14    endpoint: str
15    api_key: str
16    tenant_id: str
17    content_bucket: str
18    r2_endpoint: str
19    recording_globs: tuple[str, ...] = ()
20    recording_agents: tuple[tuple[str, bool], ...] = ()
21    logs_globs: tuple[str, ...] = ()
22    logs_agents: tuple[tuple[str, bool], ...] = ()
23    logs_agent_min_levels: tuple[tuple[str, str], ...] = ()
24    logs_min_level: str = DEFAULT_LOGS_MIN_LEVEL
25    # True when bootstrap JSON included a ``logs`` object (even empty).
26    logs_policy_present: bool = False
27    capture_genai_content_globs: tuple[str, ...] = ()
28    capture_genai_content_agents: tuple[tuple[str, bool], ...] = ()
29    # True when bootstrap JSON included a ``capture_genai_content`` object (even empty).
30    capture_genai_content_policy_present: bool = False
31
32    def recording_agents_map(self) -> dict[str, bool]:
33        return dict(self.recording_agents)
34
35    def logs_agents_map(self) -> dict[str, bool]:
36        return dict(self.logs_agents)
37
38    def logs_agent_min_levels_map(self) -> dict[str, str]:
39        return dict(self.logs_agent_min_levels)
40
41    def capture_genai_content_agents_map(self) -> dict[str, bool]:
42        return dict(self.capture_genai_content_agents)
ParlotRuntimeContext( endpoint: str, api_key: str, tenant_id: str, content_bucket: str, r2_endpoint: str, recording_globs: tuple[str, ...] = (), recording_agents: tuple[tuple[str, bool], ...] = (), logs_globs: tuple[str, ...] = (), logs_agents: tuple[tuple[str, bool], ...] = (), logs_agent_min_levels: tuple[tuple[str, str], ...] = (), logs_min_level: str = 'INFO', logs_policy_present: bool = False, capture_genai_content_globs: tuple[str, ...] = (), capture_genai_content_agents: tuple[tuple[str, bool], ...] = (), capture_genai_content_policy_present: bool = False)
endpoint: str
api_key: str
tenant_id: str
content_bucket: str
r2_endpoint: str
recording_globs: tuple[str, ...] = ()
recording_agents: tuple[tuple[str, bool], ...] = ()
logs_globs: tuple[str, ...] = ()
logs_agents: tuple[tuple[str, bool], ...] = ()
logs_agent_min_levels: tuple[tuple[str, str], ...] = ()
logs_min_level: str = 'INFO'
logs_policy_present: bool = False
capture_genai_content_globs: tuple[str, ...] = ()
capture_genai_content_agents: tuple[tuple[str, bool], ...] = ()
capture_genai_content_policy_present: bool = False
def recording_agents_map(self) -> dict[str, bool]:
32    def recording_agents_map(self) -> dict[str, bool]:
33        return dict(self.recording_agents)
def logs_agents_map(self) -> dict[str, bool]:
35    def logs_agents_map(self) -> dict[str, bool]:
36        return dict(self.logs_agents)
def logs_agent_min_levels_map(self) -> dict[str, str]:
38    def logs_agent_min_levels_map(self) -> dict[str, str]:
39        return dict(self.logs_agent_min_levels)
def capture_genai_content_agents_map(self) -> dict[str, bool]:
41    def capture_genai_content_agents_map(self) -> dict[str, bool]:
42        return dict(self.capture_genai_content_agents)
class SessionLogsCollector:
241class SessionLogsCollector:
242    """Per-``ParlotContext`` session log capture pipeline."""
243
244    def __init__(self) -> None:
245        self._lock = threading.Lock()
246        self._buffer: deque[SessionLogRecord] = deque(maxlen=_MAX_BUFFER)
247        self._consecutive_failures = 0
248        self._circuit_open_until = 0.0
249        self._last_flush_at = 0.0
250        self._endpoint = ""
251        self._api_key = ""
252        self._flush_thread: threading.Thread | None = None
253        self._stop = threading.Event()
254        self._handler: Optional[SessionLogHandler] = None
255        self._installed = False
256        self._atexit_registered = False
257        self._capture_logs_config: CaptureLogsConfig = None
258        self._capture_logs_level: Optional[str] = None
259        self._session_resolver: Optional[SessionResolver] = None
260        self._agent_id_resolver: Optional[Callable[[], str]] = None
261        self._metadata_resolver: Optional[Callable[[], Optional[bool]]] = None
262        self._context_ref: Optional[weakref.ref[ParlotContext]] = None
263
264    def bind_context(self, context: ParlotContext) -> None:
265        self._context_ref = weakref.ref(context)
266
267    def _runtime(self):
268        ctx = self._context_ref() if self._context_ref is not None else None
269        return ctx.runtime if ctx is not None else None
270
271    def set_capture_logs_config(
272        self,
273        capture_logs: CaptureLogsConfig = None,
274        *,
275        log_level: Optional[str] = None,
276    ) -> None:
277        self._capture_logs_config = capture_logs
278        if log_level is not None:
279            self._capture_logs_level = normalize_log_level(log_level)
280
281    def set_resolvers(
282        self,
283        *,
284        session_resolver: Optional[SessionResolver] = None,
285        agent_id_resolver: Optional[Callable[[], str]] = None,
286        metadata_resolver: Optional[Callable[[], Optional[bool]]] = None,
287    ) -> None:
288        """Optional framework hooks (e.g. LiveKit job bootstrap fallback)."""
289        if session_resolver is not None:
290            self._session_resolver = session_resolver
291        if agent_id_resolver is not None:
292            self._agent_id_resolver = agent_id_resolver
293        if metadata_resolver is not None:
294            self._metadata_resolver = metadata_resolver
295
296    def _resolve_min_level(self) -> str:
297        if self._capture_logs_level:
298            return self._capture_logs_level
299        runtime = self._runtime()
300        if runtime is not None:
301            agent_id = ""
302            if self._agent_id_resolver is not None:
303                try:
304                    agent_id = self._agent_id_resolver() or ""
305                except Exception:
306                    agent_id = ""
307            if agent_id:
308                agent_levels = runtime.logs_agent_min_levels_map()
309                if agent_id in agent_levels:
310                    return normalize_log_level(agent_levels[agent_id])
311            if runtime.logs_min_level:
312                return normalize_log_level(runtime.logs_min_level)
313        return DEFAULT_LOGS_MIN_LEVEL
314
315    def capture_logs_enabled_for_agent(self, agent_name: str = "") -> bool:
316        runtime = self._runtime()
317        metadata = None
318        if self._metadata_resolver is not None:
319            try:
320                metadata = self._metadata_resolver()
321            except Exception:
322                metadata = None
323        name = agent_name
324        if not name and self._agent_id_resolver is not None:
325            try:
326                name = self._agent_id_resolver() or ""
327            except Exception:
328                name = ""
329        return should_capture_logs(
330            name,
331            metadata_capture_logs=metadata,
332            capture_logs_config=self._capture_logs_config,
333            bootstrap_globs=list(runtime.logs_globs) if runtime else None,
334            bootstrap_agents=runtime.logs_agents_map() if runtime else None,
335            bootstrap_present=runtime is not None and runtime.logs_policy_present,
336        )
337
338    def handle_record(
339        self, handler: logging.Handler, record: logging.LogRecord
340    ) -> None:
341        name = record.name or ""
342        if any(name == p or name.startswith(p + ".") for p in _SKIP_LOGGER_PREFIXES):
343            return
344        level_name = record.levelname or "INFO"
345        if not level_at_least(level_name, self._resolve_min_level()):
346            return
347        if not self.capture_logs_enabled_for_agent():
348            return
349        session = _resolve_session_fields(self._session_resolver)
350        if not session or not session.get("session_id"):
351            return
352        message = handler.format(record) if handler.formatter else record.getMessage()
353        if not isinstance(message, str):
354            message = str(message)
355        trace_id, span_id = _current_trace_span()
356        ts = datetime.fromtimestamp(record.created, tz=timezone.utc).strftime(
357            "%Y-%m-%d %H:%M:%S.%f"
358        )[:-3]
359        attrs: dict[str, str] = {}
360        if record.pathname:
361            attrs["pathname"] = str(record.pathname)[:512]
362        if record.lineno:
363            attrs["lineno"] = str(record.lineno)
364        if record.funcName:
365            attrs["funcName"] = str(record.funcName)[:128]
366        event = SessionLogRecord(
367            session_id=str(session["session_id"]),
368            conversation_id=str(session.get("conversation_id") or session["session_id"]),
369            ts=ts,
370            level=normalize_log_level(level_name),
371            logger_name=name,
372            message=message,
373            turn_index=int(session.get("turn_index") or 0),
374            trace_id=trace_id,
375            span_id=span_id,
376            attributes=attrs,
377        )
378        with self._lock:
379            self._buffer.append(event)
380
381    def init(self, *, endpoint: str = "", api_key: str = "") -> None:
382        """Install root handler + start background flusher."""
383        with self._lock:
384            self._endpoint = (endpoint or "").rstrip("/")
385            self._api_key = api_key or ""
386            if self._handler is None:
387                self._handler = SessionLogHandler(self)
388                self._handler.setLevel(logging.DEBUG)
389                self._handler.setFormatter(logging.Formatter("%(message)s"))
390            if not self._installed:
391                logging.root.addHandler(self._handler)
392                self._installed = True
393            if self._flush_thread is None or not self._flush_thread.is_alive():
394                self._stop.clear()
395                self._flush_thread = threading.Thread(
396                    target=self._flush_loop,
397                    name="parlot-session-logs-flush",
398                    daemon=True,
399                )
400                self._flush_thread.start()
401            if not self._atexit_registered:
402                atexit.register(self.shutdown)
403                self._atexit_registered = True
404
405    def drain(self) -> list[SessionLogRecord]:
406        with self._lock:
407            events = list(self._buffer)
408            self._buffer.clear()
409            return events
410
411    def snapshot(self) -> list[SessionLogRecord]:
412        with self._lock:
413            return list(self._buffer)
414
415    def _circuit_open(self) -> bool:
416        return time.time() < self._circuit_open_until
417
418    def _flush_loop(self) -> None:
419        while not self._stop.wait(_FLUSH_INTERVAL_S):
420            try:
421                self._try_flush()
422            except Exception:
423                continue
424
425    def _try_flush(self, *, force: bool = False) -> None:
426        if self._circuit_open():
427            with self._lock:
428                self._buffer.clear()
429            return
430        if not self._endpoint or not self._api_key:
431            return
432
433        if not force:
434            now = time.time()
435            if now - self._last_flush_at < _FLUSH_INTERVAL_S * 0.5:
436                return
437
438        events = self.drain()
439        if not events:
440            return
441
442        ok = self._post_events(events)
443        self._last_flush_at = time.time()
444        if ok:
445            self._consecutive_failures = 0
446            return
447
448        self._consecutive_failures += 1
449        if self._consecutive_failures >= _CIRCUIT_FAILURES:
450            self._circuit_open_until = time.time() + _CIRCUIT_COOLDOWN_S
451            self._consecutive_failures = 0
452
453    def _post_events(self, events: list[SessionLogRecord]) -> bool:
454        try:
455            import httpx
456        except Exception:
457            return False
458
459        url = f"{self._endpoint}/v1/logs"
460        try:
461            payload = _encode_otlp_logs(events[:_MAX_BATCH])
462        except Exception:
463            return False
464        if not payload:
465            return True
466        from parlot.core.provider import build_parlot_client_headers
467
468        headers = build_parlot_client_headers(self._api_key)
469        headers["Content-Type"] = "application/x-protobuf"
470        try:
471            with httpx.Client(timeout=3.0) as client:
472                res = client.post(
473                    url,
474                    content=payload,
475                    headers=headers,
476                )
477                return 200 <= res.status_code < 300
478        except Exception:
479            return False
480
481    def shutdown(self) -> None:
482        """Stop capture and push any remaining buffered logs. Never raises."""
483        try:
484            self._stop.set()
485            thread = self._flush_thread
486            if (
487                thread is not None
488                and thread.is_alive()
489                and thread is not threading.current_thread()
490            ):
491                thread.join(timeout=1.0)
492            self._flush_thread = None
493            with self._lock:
494                if self._installed and self._handler is not None:
495                    try:
496                        logging.root.removeHandler(self._handler)
497                    except Exception:
498                        pass
499                    self._installed = False
500            self._try_flush(force=True)
501        except Exception:
502            return

Per-ParlotContext session log capture pipeline.

def bind_context(self, context: ParlotContext) -> None:
264    def bind_context(self, context: ParlotContext) -> None:
265        self._context_ref = weakref.ref(context)
def set_capture_logs_config( self, capture_logs: bool | Sequence[str] | None = None, *, log_level: str | None = None) -> None:
271    def set_capture_logs_config(
272        self,
273        capture_logs: CaptureLogsConfig = None,
274        *,
275        log_level: Optional[str] = None,
276    ) -> None:
277        self._capture_logs_config = capture_logs
278        if log_level is not None:
279            self._capture_logs_level = normalize_log_level(log_level)
def set_resolvers( self, *, session_resolver: Callable[[], dict[str, Any] | None] | None = None, agent_id_resolver: Callable[[], str] | None = None, metadata_resolver: Callable[[], bool | None] | None = None) -> None:
281    def set_resolvers(
282        self,
283        *,
284        session_resolver: Optional[SessionResolver] = None,
285        agent_id_resolver: Optional[Callable[[], str]] = None,
286        metadata_resolver: Optional[Callable[[], Optional[bool]]] = None,
287    ) -> None:
288        """Optional framework hooks (e.g. LiveKit job bootstrap fallback)."""
289        if session_resolver is not None:
290            self._session_resolver = session_resolver
291        if agent_id_resolver is not None:
292            self._agent_id_resolver = agent_id_resolver
293        if metadata_resolver is not None:
294            self._metadata_resolver = metadata_resolver

Optional framework hooks (e.g. LiveKit job bootstrap fallback).

def capture_logs_enabled_for_agent(self, agent_name: str = '') -> bool:
315    def capture_logs_enabled_for_agent(self, agent_name: str = "") -> bool:
316        runtime = self._runtime()
317        metadata = None
318        if self._metadata_resolver is not None:
319            try:
320                metadata = self._metadata_resolver()
321            except Exception:
322                metadata = None
323        name = agent_name
324        if not name and self._agent_id_resolver is not None:
325            try:
326                name = self._agent_id_resolver() or ""
327            except Exception:
328                name = ""
329        return should_capture_logs(
330            name,
331            metadata_capture_logs=metadata,
332            capture_logs_config=self._capture_logs_config,
333            bootstrap_globs=list(runtime.logs_globs) if runtime else None,
334            bootstrap_agents=runtime.logs_agents_map() if runtime else None,
335            bootstrap_present=runtime is not None and runtime.logs_policy_present,
336        )
def handle_record(self, handler: logging.Handler, record: logging.LogRecord) -> None:
338    def handle_record(
339        self, handler: logging.Handler, record: logging.LogRecord
340    ) -> None:
341        name = record.name or ""
342        if any(name == p or name.startswith(p + ".") for p in _SKIP_LOGGER_PREFIXES):
343            return
344        level_name = record.levelname or "INFO"
345        if not level_at_least(level_name, self._resolve_min_level()):
346            return
347        if not self.capture_logs_enabled_for_agent():
348            return
349        session = _resolve_session_fields(self._session_resolver)
350        if not session or not session.get("session_id"):
351            return
352        message = handler.format(record) if handler.formatter else record.getMessage()
353        if not isinstance(message, str):
354            message = str(message)
355        trace_id, span_id = _current_trace_span()
356        ts = datetime.fromtimestamp(record.created, tz=timezone.utc).strftime(
357            "%Y-%m-%d %H:%M:%S.%f"
358        )[:-3]
359        attrs: dict[str, str] = {}
360        if record.pathname:
361            attrs["pathname"] = str(record.pathname)[:512]
362        if record.lineno:
363            attrs["lineno"] = str(record.lineno)
364        if record.funcName:
365            attrs["funcName"] = str(record.funcName)[:128]
366        event = SessionLogRecord(
367            session_id=str(session["session_id"]),
368            conversation_id=str(session.get("conversation_id") or session["session_id"]),
369            ts=ts,
370            level=normalize_log_level(level_name),
371            logger_name=name,
372            message=message,
373            turn_index=int(session.get("turn_index") or 0),
374            trace_id=trace_id,
375            span_id=span_id,
376            attributes=attrs,
377        )
378        with self._lock:
379            self._buffer.append(event)
def init(self, *, endpoint: str = '', api_key: str = '') -> None:
381    def init(self, *, endpoint: str = "", api_key: str = "") -> None:
382        """Install root handler + start background flusher."""
383        with self._lock:
384            self._endpoint = (endpoint or "").rstrip("/")
385            self._api_key = api_key or ""
386            if self._handler is None:
387                self._handler = SessionLogHandler(self)
388                self._handler.setLevel(logging.DEBUG)
389                self._handler.setFormatter(logging.Formatter("%(message)s"))
390            if not self._installed:
391                logging.root.addHandler(self._handler)
392                self._installed = True
393            if self._flush_thread is None or not self._flush_thread.is_alive():
394                self._stop.clear()
395                self._flush_thread = threading.Thread(
396                    target=self._flush_loop,
397                    name="parlot-session-logs-flush",
398                    daemon=True,
399                )
400                self._flush_thread.start()
401            if not self._atexit_registered:
402                atexit.register(self.shutdown)
403                self._atexit_registered = True

Install root handler + start background flusher.

def drain(self) -> list[parlot.core.session_logs.SessionLogRecord]:
405    def drain(self) -> list[SessionLogRecord]:
406        with self._lock:
407            events = list(self._buffer)
408            self._buffer.clear()
409            return events
def snapshot(self) -> list[parlot.core.session_logs.SessionLogRecord]:
411    def snapshot(self) -> list[SessionLogRecord]:
412        with self._lock:
413            return list(self._buffer)
def shutdown(self) -> None:
481    def shutdown(self) -> None:
482        """Stop capture and push any remaining buffered logs. Never raises."""
483        try:
484            self._stop.set()
485            thread = self._flush_thread
486            if (
487                thread is not None
488                and thread.is_alive()
489                and thread is not threading.current_thread()
490            ):
491                thread.join(timeout=1.0)
492            self._flush_thread = None
493            with self._lock:
494                if self._installed and self._handler is not None:
495                    try:
496                        logging.root.removeHandler(self._handler)
497                    except Exception:
498                        pass
499                    self._installed = False
500            self._try_flush(force=True)
501        except Exception:
502            return

Stop capture and push any remaining buffered logs. Never raises.

def apply_genai_span_rename( span: opentelemetry.sdk.trace.ReadableSpan, *, remap_fn: Callable[[str, Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None], str | None], stage_for_native: Mapping[str, str] | None = None, llm_names: Collection[str] = (), tool_names: Collection[str] = (), set_chat_op: bool = True, set_tool_op: bool = True, preserve_ops: frozenset[str] | None = None, resolve_stage_key: Callable[[str], str] | None = None, after_rename: Callable[[opentelemetry.sdk.trace.ReadableSpan, str, str, Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]]], NoneType] | None = None) -> None:
 82def apply_genai_span_rename(
 83    span: ReadableSpan,
 84    *,
 85    remap_fn: Callable[[str, Mapping[str, AttributeValue] | None], str | None],
 86    stage_for_native: Mapping[str, str] | None = None,
 87    llm_names: Collection[str] = (),
 88    tool_names: Collection[str] = (),
 89    set_chat_op: bool = True,
 90    set_tool_op: bool = True,
 91    preserve_ops: frozenset[str] | None = None,
 92    resolve_stage_key: Callable[[str], str] | None = None,
 93    after_rename: Callable[
 94        [ReadableSpan, str, str, Mapping[str, AttributeValue]], None
 95    ]
 96    | None = None,
 97) -> None:
 98    """Mutate ``span._name`` and GenAI op / stage attrs for export.
 99
100    ``after_rename(span, native, exported, attrs)`` runs when the adapter needs
101    extra logic (e.g. LiveKit evaluation op preservation, tool name sync).
102    When provided, it replaces the default chat/tool op stamping.
103    """
104    native = span.name or ""
105    attrs = span.attributes or {}
106    exported = remap_fn(native, attrs)
107    if exported is None:
108        return
109    if exported != native:
110        span._name = exported  # type: ignore[attr-defined]
111
112    stage_key = resolve_stage_key(native) if resolve_stage_key else native
113    if stage_for_native:
114        stage = stage_for_native.get(stage_key)
115        if stage and not attrs.get(ATTR_AGENT_STAGE):
116            ParlotBaseProcessor._set(span, ATTR_AGENT_STAGE, stage)
117
118    if after_rename is not None:
119        after_rename(span, native, exported, attrs)
120        return
121
122    preserve = preserve_ops or frozenset({GEN_AI_OP_GENERATE_CONTENT})
123    if set_chat_op and native in llm_names:
124        existing_op = str(attrs.get(ATTR_GEN_AI_OP_NAME) or "").strip().lower()
125        if existing_op not in preserve:
126            ParlotBaseProcessor._set(span, ATTR_GEN_AI_OP_NAME, GEN_AI_OP_CHAT)
127    elif set_tool_op and native in tool_names:
128        ParlotBaseProcessor._set(span, ATTR_GEN_AI_OP_NAME, GEN_AI_OP_EXECUTE_TOOL)

Mutate span._name and GenAI op / stage attrs for export.

after_rename(span, native, exported, attrs) runs when the adapter needs extra logic (e.g. LiveKit evaluation op preservation, tool name sync). When provided, it replaces the default chat/tool op stamping.

def attach_span_processor_if_absent( provider: Any, factory: Any, *, processor_type: type | None = None) -> bool:
170def attach_span_processor_if_absent(
171    provider: Any,
172    factory: Any,
173    *,
174    processor_type: type | None = None,
175) -> bool:
176    """Attach a span processor when the SDK provider does not already have one.
177
178    ``factory`` is a zero-arg callable that returns a ``SpanProcessor``.
179    ``processor_type`` defaults to the type of the first factory result check —
180    when omitted, skips the duplicate check and always adds.
181
182    Returns True when a processor was attached.
183    """
184    from opentelemetry.sdk.trace import TracerProvider as SdkTracerProvider
185
186    if not isinstance(provider, SdkTracerProvider):
187        return False
188
189    if processor_type is not None:
190        try:
191            active = getattr(provider, "_active_span_processor", None)
192            procs = getattr(active, "_span_processors", None) or getattr(
193                active, "_processors", None
194            )
195            if procs and any(isinstance(p, processor_type) for p in procs):
196                return False
197        except Exception:
198            pass
199    try:
200        provider.add_span_processor(factory())
201        return True
202    except Exception:
203        logger.debug("Could not attach span processor", exc_info=True)
204        return False

Attach a span processor when the SDK provider does not already have one.

factory is a zero-arg callable that returns a SpanProcessor. processor_type defaults to the type of the first factory result check — when omitted, skips the duplicate check and always adds.

Returns True when a processor was attached.

def base_parlotize( agent_id: str, *, endpoint: str | None = None, api_key: str | None = None, capture_genai_content: bool | None = None, service_name: str | None = None, tracer_provider: opentelemetry.trace.TracerProvider | None = None, version: str | None = None, capture_logs: bool | list[str] | None = None, log_level: str | None = None) -> BaseParlotizeResult:
174def base_parlotize(
175    agent_id: str,
176    *,
177    endpoint: Optional[str] = None,
178    api_key: Optional[str] = None,
179    capture_genai_content: Optional[bool] = None,
180    service_name: Optional[str] = None,
181    tracer_provider: TracerProvider | None = None,
182    version: Optional[str] = None,
183    capture_logs: bool | list[str] | None = None,
184    log_level: Optional[str] = None,
185) -> BaseParlotizeResult:
186    """Execute shared telemetry configuration common across all adapters.
187
188    Creates a ``ParlotContext``, initializes the session log
189    collector, and fetches remote bootstrap into ``context.runtime``.
190
191    See ``ParlotizeProtocol`` for the shared parameter surface.
192
193    Args:
194        agent_id: Required canonical deployment identity for ``session.agent_id``.
195        endpoint: Parlot OTLP collector base URL. If omitted, reads
196            ``PARLOT_ENDPOINT``.
197        api_key: Org-scoped API key. If omitted, reads ``PARLOT_API_KEY``.
198        capture_genai_content: Process-wide GenAI content capture override.
199        service_name: OTel ``service.name``. Defaults to ``agent_id`` when
200            omitted or blank.
201        tracer_provider: Existing ``TracerProvider`` to adopt, if any.
202        version: Deployment version for ``gen_ai.agent.version``. Defaults to
203            ``\"unknown\"`` when omitted or blank.
204        capture_logs: Session log capture policy (bool or agent-id globs).
205        log_level: Minimum level for session log capture.
206
207    Returns:
208        Resolved endpoint, credentials, provider, ``service_name``, and capture
209        settings for the calling adapter.
210    """
211    from parlot.core.bootstrap import fetch_telemetry_bootstrap
212    from parlot.core.context import ParlotContext
213    from parlot.core.provider import (
214        adopt_existing_tracer_provider,
215        resolve_api_key,
216        resolve_capture_genai_content,
217        resolve_endpoint,
218        resolve_service_name,
219    )
220
221    configure_parlot_logging()
222
223    resolved_agent_id = agent_id.strip()
224    if not resolved_agent_id:
225        raise ValueError("parlotize(agent_id) is required")
226    resolved_version = (version or "").strip() or "unknown"
227    resolved_endpoint = resolve_endpoint(endpoint)
228    resolved_api_key = resolve_api_key(api_key)
229    resolved_capture_content = resolve_capture_genai_content(capture_genai_content)
230    resolved_service_name = resolve_service_name(
231        service_name,
232        agent_id=resolved_agent_id,
233    )
234
235    resolved_capture_logs = normalize_glob_or_bool(capture_logs)
236
237    resolved_log_level = log_level.strip().upper() if log_level else None
238
239    context = ParlotContext()
240    context.session_logs.set_capture_logs_config(
241        resolved_capture_logs,
242        log_level=resolved_log_level,
243    )
244    context.session_logs.init(endpoint=resolved_endpoint, api_key=resolved_api_key)
245
246    if not resolved_endpoint:
247        logger.error("parlot: PARLOT_ENDPOINT is not set")
248    if not resolved_api_key:
249        logger.error(
250            "parlot: PARLOT_API_KEY is not set; telemetry will not authenticate"
251        )
252    elif resolved_endpoint:
253        fetch_telemetry_bootstrap(resolved_endpoint, resolved_api_key, context)
254
255    if tracer_provider is None:
256        tracer_provider = adopt_existing_tracer_provider()
257
258    return BaseParlotizeResult(
259        context=context,
260        endpoint=resolved_endpoint,
261        api_key=resolved_api_key,
262        tracer_provider=tracer_provider,
263        agent_id=resolved_agent_id,
264        agent_version=resolved_version,
265        capture_genai_content=resolved_capture_content,
266        capture_logs=resolved_capture_logs,
267        log_level=resolved_log_level,
268        service_name=resolved_service_name,
269    )

Execute shared telemetry configuration common across all adapters.

Creates a ParlotContext, initializes the session log collector, and fetches remote bootstrap into context.runtime.

See ParlotizeProtocol for the shared parameter surface.

Arguments:
  • agent_id: Required canonical deployment identity for session.agent_id.
  • endpoint: Parlot OTLP collector base URL. If omitted, reads PARLOT_ENDPOINT.
  • api_key: Org-scoped API key. If omitted, reads PARLOT_API_KEY.
  • capture_genai_content: Process-wide GenAI content capture override.
  • service_name: OTel service.name. Defaults to agent_id when omitted or blank.
  • tracer_provider: Existing TracerProvider to adopt, if any.
  • version: Deployment version for gen_ai.agent.version. Defaults to "unknown" when omitted or blank.
  • capture_logs: Session log capture policy (bool or agent-id globs).
  • log_level: Minimum level for session log capture.
Returns:

Resolved endpoint, credentials, provider, service_name, and capture settings for the calling adapter.

def build_filtered_tracer_provider( *, endpoint: str, api_key: str, service_name: str | None = None, service_version: str | None = None, span_processors: Sequence[opentelemetry.sdk.trace.SpanProcessor] = ()) -> opentelemetry.sdk.trace.TracerProvider:
207def build_filtered_tracer_provider(
208    *,
209    endpoint: str,
210    api_key: str,
211    service_name: Optional[str] = None,
212    service_version: Optional[str] = None,
213    span_processors: Sequence[SpanProcessor] = (),
214) -> TracerProvider:
215    """Build a TracerProvider with ``ExportFilterSpanExporter`` over OTLP/HTTP.
216
217    Framework processors go in ``span_processors`` (before the batch exporter).
218    """
219    from parlot.core.export import ExportFilterSpanExporter, QuietOTLPSpanExporter
220
221    resource = build_resource(
222        service_name=service_name,
223        service_version=service_version,
224    )
225    trace_endpoint = endpoint.rstrip("/") + "/v1/traces"
226    otlp = build_otlp_http_exporter(endpoint=endpoint, api_key=api_key)
227    filtered = ExportFilterSpanExporter(otlp)
228    exporter = QuietOTLPSpanExporter(filtered, endpoint_label=trace_endpoint)
229    provider = TracerProvider(resource=resource)
230    for processor in span_processors:
231        provider.add_span_processor(processor)
232    provider.add_span_processor(BatchSpanProcessor(exporter))
233    return provider

Build a TracerProvider with ExportFilterSpanExporter over OTLP/HTTP.

Framework processors go in span_processors (before the batch exporter).

def configure_parlot_logging() -> None:
151def configure_parlot_logging() -> None:
152    """Apply ``PARLOT_DEBUG_LEVEL`` to ``parlot.*`` loggers only.
153
154    Ensures a root handler exists at INFO so messages reach stdout, without
155    flipping the process root to DEBUG (that floods ``websockets`` / ``aiortc``
156    / similar when debugging Parlot span export).
157
158    Sets the ``parlot`` parent so future ``parlot.*`` children with ``NOTSET``
159    inherit, and walks already-created ``parlot.*`` loggers so a prior
160    ``setLevel`` cannot stick — no hardcoded name list to maintain.
161    """
162    level_name = os.getenv("PARLOT_DEBUG_LEVEL", "INFO").upper()
163    level = getattr(logging, level_name, logging.INFO)
164    if not logging.root.handlers:
165        logging.basicConfig(level=logging.INFO)
166    logging.getLogger("parlot").setLevel(level)
167    for name, obj in list(logging.Logger.manager.loggerDict.items()):
168        if name != "parlot" and not name.startswith("parlot."):
169            continue
170        if isinstance(obj, logging.Logger):
171            obj.setLevel(level)

Apply PARLOT_DEBUG_LEVEL to parlot.* loggers only.

Ensures a root handler exists at INFO so messages reach stdout, without flipping the process root to DEBUG (that floods websockets / aiortc / similar when debugging Parlot span export).

Sets the parlot parent so future parlot.* children with NOTSET inherit, and walks already-created parlot.* loggers so a prior setLevel cannot stick — no hardcoded name list to maintain.

def emit_parlot_session_close( tracer: Any, *, session_id: str, conversation_id: str, reason: str = 'completed', framework: str = '', close_error: str | None = None, extra_attrs: Mapping[str, Any] | None = None, copy_from_session_span: Any | None = None, stamp_fn: Callable[[Any], NoneType] | None = None, end_time: int | None = None, state: Any | None = None) -> None:
153def emit_parlot_session_close(
154    tracer: Any,
155    *,
156    session_id: str,
157    conversation_id: str,
158    reason: str = "completed",
159    framework: str = "",
160    close_error: str | None = None,
161    extra_attrs: Mapping[str, Any] | None = None,
162    copy_from_session_span: Any | None = None,
163    stamp_fn: Callable[[Any], None] | None = None,
164    end_time: int | None = None,
165    state: Any | None = None,
166) -> None:
167    """Mint a ``parlot.session.close`` span for collector finalize.
168
169    ``stamp_fn`` applies framework identity / platform refs after base attrs.
170    ``copy_from_session_span`` copies scalar attrs from the live session span
171    (LiveKit uses this so close carries room/job metadata).
172    """
173    if tracer is None:
174        return
175    attrs: dict[str, Any] = {
176        ATTR_SESSION_ID: session_id,
177        ATTR_SESSION_CONVERSATION_ID: conversation_id,
178        ATTR_SESSION_CLOSE_REASON: reason,
179    }
180    if conversation_id:
181        attrs[ATTR_GEN_AI_CONVERSATION_ID] = conversation_id
182    if framework:
183        attrs[ATTR_AGENT_FRAMEWORK] = framework
184    if close_error:
185        attrs[ATTR_SESSION_CLOSE_ERROR] = close_error
186    if copy_from_session_span is not None:
187        _copy_scalar_attrs_from_span(copy_from_session_span, attrs)
188    if extra_attrs:
189        for key, value in extra_attrs.items():
190            if value is None:
191                continue
192            attrs[key] = value
193
194    span = tracer.start_span(SPAN_PARLOT_SESSION_CLOSE, attributes=attrs)
195    try:
196        if stamp_fn is not None:
197            stamp_fn(span)
198        if state is not None:
199            stamp_session_sdk_version(span, state)
200    finally:
201        if end_time is not None and hasattr(span, "end"):
202            span.end(end_time=end_time)
203        else:
204            span.end()

Mint a parlot.session.close span for collector finalize.

stamp_fn applies framework identity / platform refs after base attrs. copy_from_session_span copies scalar attrs from the live session span (LiveKit uses this so close carries room/job metadata).

def emit_parlot_turn_root( tracer: Any, *, session_id: str, conversation_id: str, turn_index: int, participant_role: str = '', utterance_text: str = '', user_text: str = '', agent_text: str = '', prev_trace_id: str = '', start_time_unix_ns: int | None = None, end_time_unix_ns: int | None = None, interrupted: bool = False, e2e_latency_s: float | None = None, llm_ttft_s: float | None = None, tts_ttfb_s: float | None = None, stamp_genai_conversation_id: bool = False, extra_attrs: Mapping[str, Any] | None = None, stamp_fn: Callable[[Any], NoneType] | None = None) -> tuple[str, str]:
 51def emit_parlot_turn_root(
 52    tracer: Any,
 53    *,
 54    session_id: str,
 55    conversation_id: str,
 56    turn_index: int,
 57    participant_role: str = "",
 58    utterance_text: str = "",
 59    user_text: str = "",
 60    agent_text: str = "",
 61    prev_trace_id: str = "",
 62    start_time_unix_ns: int | None = None,
 63    end_time_unix_ns: int | None = None,
 64    interrupted: bool = False,
 65    e2e_latency_s: float | None = None,
 66    llm_ttft_s: float | None = None,
 67    tts_ttfb_s: float | None = None,
 68    stamp_genai_conversation_id: bool = False,
 69    extra_attrs: Mapping[str, Any] | None = None,
 70    stamp_fn: Callable[[Any], None] | None = None,
 71) -> tuple[str, str]:
 72    """
 73    Start and end a ``parlot.turn`` root span in a **new** OTel trace.
 74
 75    When ``start_time_unix_ns`` / ``end_time_unix_ns`` are set, OTLP duration
 76    matches the speech / turn window.
 77
 78    Prefer explicit ``user_text`` / ``agent_text`` when both sides of an exchange
 79    are known; otherwise pass ``utterance_text`` with ``participant_role``.
 80
 81    ``extra_attrs`` carries framework-specific turn attributes (media ms,
 82    diarization, ``agent.framework``, etc.). ``stamp_fn`` runs before the span
 83    ends (platform refs, framework identity).
 84
 85    Returns ``(trace_id, span_id)`` as 32- and 16-char hex strings.
 86    """
 87    trace_id = random.getrandbits(128)
 88    span_id = random.getrandbits(64)
 89    ctx = SpanContext(
 90        trace_id=trace_id,
 91        span_id=span_id,
 92        is_remote=False,
 93        trace_flags=TraceFlags(0x01),
 94    )
 95    parent_ctx = trace.set_span_in_context(NonRecordingSpan(ctx))
 96
 97    start_kw: dict[str, Any] = {"context": parent_ctx}
 98    if start_time_unix_ns is not None:
 99        start_kw["start_time"] = start_time_unix_ns
100
101    span = tracer.start_span(SPAN_PARLOT_TURN, **start_kw)
102    try:
103        span.set_attribute(ATTR_SESSION_ID, session_id)
104        span.set_attribute(ATTR_SESSION_CONVERSATION_ID, conversation_id)
105        if stamp_genai_conversation_id:
106            span.set_attribute(ATTR_GEN_AI_CONVERSATION_ID, conversation_id)
107        span.set_attribute(ATTR_TURN_INDEX, turn_index)
108        if participant_role:
109            span.set_attribute(ATTR_TURN_PARTICIPANT_ROLE, participant_role)
110        if prev_trace_id:
111            span.set_attribute(ATTR_TURN_PREV_TRACE_ID, prev_trace_id)
112        if interrupted:
113            span.set_attribute(ATTR_TURN_INTERRUPTED, True)
114        if e2e_latency_s is not None:
115            span.set_attribute(ATTR_TURN_E2E_LATENCY_S, float(e2e_latency_s))
116        if llm_ttft_s is not None:
117            span.set_attribute(ATTR_TURN_LLM_TTFT_S, float(llm_ttft_s))
118        if tts_ttfb_s is not None:
119            span.set_attribute(ATTR_TURN_TTS_TTFB_S, float(tts_ttfb_s))
120
121        user = (user_text or "").strip()
122        agent = (agent_text or "").strip()
123        if user or agent:
124            if user:
125                stamp_turn_utterance_text(
126                    span, participant_role="user", utterance_text=user
127                )
128            if agent:
129                stamp_turn_utterance_text(
130                    span, participant_role="agent", utterance_text=agent
131                )
132        elif utterance_text:
133            stamp_turn_utterance_text(
134                span,
135                participant_role=participant_role,
136                utterance_text=utterance_text,
137            )
138
139        if extra_attrs:
140            for key, value in extra_attrs.items():
141                if value is None or value == "":
142                    continue
143                span.set_attribute(key, value)
144        if stamp_fn is not None:
145            stamp_fn(span)
146    finally:
147        if end_time_unix_ns is not None:
148            span.end(end_time=end_time_unix_ns)
149        else:
150            span.end()
151
152    return format(trace_id, "032x"), format(span_id, "016x")

Start and end a parlot.turn root span in a new OTel trace.

When start_time_unix_ns / end_time_unix_ns are set, OTLP duration matches the speech / turn window.

Prefer explicit user_text / agent_text when both sides of an exchange are known; otherwise pass utterance_text with participant_role.

extra_attrs carries framework-specific turn attributes (media ms, diarization, agent.framework, etc.). stamp_fn runs before the span ends (platform refs, framework identity).

Returns (trace_id, span_id) as 32- and 16-char hex strings.

def end_session_span_ok(session_span: Any) -> None:
234def end_session_span_ok(session_span: Any) -> None:
235    """Mark a session span OK and end it."""
236    try:
237        session_span.set_status(Status(StatusCode.OK))
238    except Exception:
239        pass
240    session_span.end()

Mark a session span OK and end it.

class ExportFilterSpanExporter(opentelemetry.sdk.trace.export.SpanExporter):
25class ExportFilterSpanExporter(SpanExporter):
26    """Pass through only Conversation Contract + GenAI + voice spans."""
27
28    def __init__(self, exporter: SpanExporter) -> None:
29        self._exporter = exporter
30
31    def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
32        filtered = [span for span in spans if is_exportable_span_name(span.name)]
33        if not filtered:
34            return SpanExportResult.SUCCESS
35        return self._exporter.export(filtered)
36
37    def shutdown(self) -> None:
38        self._exporter.shutdown()
39
40    def force_flush(self, timeout_millis: int = 30000) -> bool:
41        return self._exporter.force_flush(timeout_millis)

Pass through only Conversation Contract + GenAI + voice spans.

ExportFilterSpanExporter(exporter: opentelemetry.sdk.trace.export.SpanExporter)
28    def __init__(self, exporter: SpanExporter) -> None:
29        self._exporter = exporter
def export( self, spans: Sequence[opentelemetry.sdk.trace.ReadableSpan]) -> opentelemetry.sdk.trace.export.SpanExportResult:
31    def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
32        filtered = [span for span in spans if is_exportable_span_name(span.name)]
33        if not filtered:
34            return SpanExportResult.SUCCESS
35        return self._exporter.export(filtered)

Exports a batch of telemetry data.

Arguments:
  • spans: The list of opentelemetry.trace.Span objects to be exported
Returns:

The result of the export

def shutdown(self) -> None:
37    def shutdown(self) -> None:
38        self._exporter.shutdown()

Shuts down the exporter.

Called when the SDK is shut down.

def force_flush(self, timeout_millis: int = 30000) -> bool:
40    def force_flush(self, timeout_millis: int = 30000) -> bool:
41        return self._exporter.force_flush(timeout_millis)

Hint to ensure that the export of any spans the exporter has received prior to the call to ForceFlush SHOULD be completed as soon as possible, preferably before returning from this method.

class QuietOTLPSpanExporter(opentelemetry.sdk.trace.export.SpanExporter):
 44class QuietOTLPSpanExporter(SpanExporter):
 45    """Wraps an OTLP span exporter; quota 429s are swallowed after one process log."""
 46
 47    def __init__(
 48        self,
 49        exporter: SpanExporter,
 50        *,
 51        endpoint_label: str,
 52    ) -> None:
 53        self._exporter = exporter
 54        self._endpoint_label = endpoint_label
 55        self._quota_responses: list[tuple[int, str]] = []
 56        attach_otlp_http_quota_capture(
 57            exporter,
 58            on_quota=self._record_quota_response,
 59        )
 60
 61    def _record_quota_response(self, status_code: int, body_text: str) -> None:
 62        self._quota_responses.append((status_code, body_text))
 63
 64    def _swallow_quota_if_present(self) -> SpanExportResult | None:
 65        if not self._quota_responses:
 66            return None
 67        status_code, body_text = self._quota_responses[-1]
 68        if status_code == 429:
 69            log_parlot_ingest_quota_once(
 70                channel="trace",
 71                status_code=status_code,
 72                body_text=body_text,
 73            )
 74            return SpanExportResult.SUCCESS
 75        return None
 76
 77    def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
 78        self._quota_responses.clear()
 79        try:
 80            result = self._exporter.export(spans)
 81        except Exception as exc:
 82            swallowed = self._swallow_quota_if_present()
 83            if swallowed is not None:
 84                return swallowed
 85            logger.warning(
 86                "Parlot trace export failed (%s): %s",
 87                self._endpoint_label,
 88                _short_error(exc),
 89            )
 90            return SpanExportResult.FAILURE
 91
 92        swallowed = self._swallow_quota_if_present()
 93        if swallowed is not None:
 94            return swallowed
 95        if result == SpanExportResult.FAILURE:
 96            logger.warning(
 97                "Parlot trace export returned FAILURE (%s)",
 98                self._endpoint_label,
 99            )
100        return result
101
102    def shutdown(self) -> None:
103        self._exporter.shutdown()
104
105    def force_flush(self, timeout_millis: int = 30000) -> bool:
106        return self._exporter.force_flush(timeout_millis)

Wraps an OTLP span exporter; quota 429s are swallowed after one process log.

QuietOTLPSpanExporter( exporter: opentelemetry.sdk.trace.export.SpanExporter, *, endpoint_label: str)
47    def __init__(
48        self,
49        exporter: SpanExporter,
50        *,
51        endpoint_label: str,
52    ) -> None:
53        self._exporter = exporter
54        self._endpoint_label = endpoint_label
55        self._quota_responses: list[tuple[int, str]] = []
56        attach_otlp_http_quota_capture(
57            exporter,
58            on_quota=self._record_quota_response,
59        )
def export( self, spans: Sequence[opentelemetry.sdk.trace.ReadableSpan]) -> opentelemetry.sdk.trace.export.SpanExportResult:
 77    def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
 78        self._quota_responses.clear()
 79        try:
 80            result = self._exporter.export(spans)
 81        except Exception as exc:
 82            swallowed = self._swallow_quota_if_present()
 83            if swallowed is not None:
 84                return swallowed
 85            logger.warning(
 86                "Parlot trace export failed (%s): %s",
 87                self._endpoint_label,
 88                _short_error(exc),
 89            )
 90            return SpanExportResult.FAILURE
 91
 92        swallowed = self._swallow_quota_if_present()
 93        if swallowed is not None:
 94            return swallowed
 95        if result == SpanExportResult.FAILURE:
 96            logger.warning(
 97                "Parlot trace export returned FAILURE (%s)",
 98                self._endpoint_label,
 99            )
100        return result

Exports a batch of telemetry data.

Arguments:
  • spans: The list of opentelemetry.trace.Span objects to be exported
Returns:

The result of the export

def shutdown(self) -> None:
102    def shutdown(self) -> None:
103        self._exporter.shutdown()

Shuts down the exporter.

Called when the SDK is shut down.

def force_flush(self, timeout_millis: int = 30000) -> bool:
105    def force_flush(self, timeout_millis: int = 30000) -> bool:
106        return self._exporter.force_flush(timeout_millis)

Hint to ensure that the export of any spans the exporter has received prior to the call to ForceFlush SHOULD be completed as soon as possible, preferably before returning from this method.

def force_flush_tracer_provider(provider: Any | None = None, *, timeout_millis: int = 10000) -> bool:
33def force_flush_tracer_provider(
34    provider: Any | None = None,
35    *,
36    timeout_millis: int = 10_000,
37) -> bool:
38    """Return True when the tracer provider reports a successful flush.
39
40    When ``provider`` is omitted, uses the global tracer provider.
41    """
42    if provider is None:
43        from opentelemetry import trace
44
45        provider = trace.get_tracer_provider()
46    if provider is None:
47        return True
48    force_flush = getattr(provider, "force_flush", None)
49    if not callable(force_flush):
50        return True
51    try:
52        result = force_flush(timeout_millis=timeout_millis)
53    except TypeError:
54        try:
55            result = force_flush()
56        except Exception:
57            logger.warning("parlot: force_flush raised", exc_info=True)
58            return False
59    except Exception:
60        logger.warning("parlot: force_flush raised", exc_info=True)
61        return False
62    if result is None:
63        # Some providers return None on success; treat as ok.
64        return True
65    return bool(result)

Return True when the tracer provider reports a successful flush.

When provider is omitted, uses the global tracer provider.

def force_flush_with_budget( *, budget_s: float = 12.0, backoffs_s: Sequence[float] = (1.0, 3.0, 6.0), attempt_timeout_ms: int = 4000, provider: Any | None = None, flush_fn: Callable[..., bool] | None = None, log: logging.Logger | None = None) -> bool:
 68def force_flush_with_budget(
 69    *,
 70    budget_s: float = DEFAULT_CLOSE_FLUSH_BUDGET_S,
 71    backoffs_s: Sequence[float] = DEFAULT_CLOSE_FLUSH_BACKOFFS_S,
 72    attempt_timeout_ms: int = DEFAULT_CLOSE_FLUSH_ATTEMPT_TIMEOUT_MS,
 73    provider: Any | None = None,
 74    flush_fn: Callable[..., bool] | None = None,
 75    log: logging.Logger | None = None,
 76) -> bool:
 77    """Retry OTLP flush within a wall-clock budget.
 78
 79    Returns True when flush confirmed within the budget; False otherwise.
 80    Default policy matches LiveKit's Room Composite close path (1s/3s/6s
 81    backoff inside ~12s).
 82
 83    ``flush_fn`` defaults to :func:`force_flush_tracer_provider`. Adapters may
 84    pass a local wrapper so tests can patch the adapter module symbol.
 85    """
 86    log = log or logger
 87    do_flush = flush_fn or (
 88        lambda *, timeout_millis: force_flush_tracer_provider(
 89            provider, timeout_millis=timeout_millis
 90        )
 91    )
 92    started = time.monotonic()
 93    attempt = 0
 94    while True:
 95        attempt += 1
 96        remaining_s = budget_s - (time.monotonic() - started)
 97        if remaining_s <= 0:
 98            break
 99        timeout_ms = min(attempt_timeout_ms, max(1, int(remaining_s * 1000)))
100        ok = do_flush(timeout_millis=timeout_ms)
101        elapsed_ms = int((time.monotonic() - started) * 1000)
102        if ok:
103            log.info(
104                "parlot: otlp flush before close ok attempts=%s elapsed_ms=%s",
105                attempt,
106                elapsed_ms,
107            )
108            return True
109        backoff_idx = attempt - 1
110        if backoff_idx >= len(backoffs_s):
111            break
112        backoff_s = backoffs_s[backoff_idx]
113        remaining_s = budget_s - (time.monotonic() - started)
114        if remaining_s <= 0:
115            break
116        sleep_s = min(backoff_s, remaining_s)
117        log.warning(
118            "parlot: otlp flush before close incomplete; retrying "
119            "attempt=%s sleep_s=%.1f remaining_s=%.1f",
120            attempt,
121            sleep_s,
122            remaining_s,
123        )
124        time.sleep(sleep_s)
125    elapsed_ms = int((time.monotonic() - started) * 1000)
126    log.warning(
127        "parlot: otlp flush before close failed attempts=%s elapsed_ms=%s",
128        attempt,
129        elapsed_ms,
130    )
131    return False

Retry OTLP flush within a wall-clock budget.

Returns True when flush confirmed within the budget; False otherwise. Default policy matches LiveKit's Room Composite close path (1s/3s/6s backoff inside ~12s).

flush_fn defaults to force_flush_tracer_provider(). Adapters may pass a local wrapper so tests can patch the adapter module symbol.

LONG_LIVED_METRICS_EXPORT_INTERVAL_MS = 60000
def log_parlotize_ready( framework: str, *, agent_id: str, endpoint: str, api_key: str = '', bootstrap_ok: bool | None = None, log: logging.Logger | None = None) -> None:
32def log_parlotize_ready(
33    framework: str,
34    *,
35    agent_id: str,
36    endpoint: str,
37    api_key: str = "",
38    bootstrap_ok: bool | None = None,
39    log: logging.Logger | None = None,
40) -> None:
41    """Shared ready / warn log after adapter ``parlotize()`` succeeds."""
42    log = log or logger
43    auth_ok = bool(endpoint and api_key)
44    if not auth_ok or bootstrap_ok is False:
45        bootstrap_label = (
46            "failed"
47            if bootstrap_ok is False
48            else ("ok" if bootstrap_ok else "skipped")
49        )
50        # LiveKit historically used "is not ready"; Pipecat used "may not authenticate".
51        # Prefer the broader "not ready" wording when bootstrap is explicitly False.
52        if bootstrap_ok is False:
53            log.warning(
54                "parlot: %s instrumentation installed but telemetry is not ready "
55                "(agent_id=%s endpoint=%s bootstrap=%s); check PARLOT_ENDPOINT / "
56                "PARLOT_API_KEY",
57                framework,
58                agent_id,
59                endpoint or "(unset)",
60                bootstrap_label,
61            )
62        else:
63            log.warning(
64                "parlot: %s instrumentation installed but telemetry may not "
65                "authenticate (agent_id=%s endpoint=%s); check PARLOT_ENDPOINT / "
66                "PARLOT_API_KEY",
67                framework,
68                agent_id,
69                endpoint or "(unset)",
70            )
71        return
72    log.info(
73        "parlot: %s instrumentation ready (agent_id=%s endpoint=%s)",
74        framework,
75        agent_id,
76        endpoint,
77    )

Shared ready / warn log after adapter parlotize() succeeds.

def normalize_glob_or_bool(value: bool | list[str] | None) -> bool | list[str] | None:
23def normalize_glob_or_bool(
24    value: bool | list[str] | None,
25) -> bool | list[str] | None:
26    """Normalize a bool-or-glob-list config (``record`` / ``capture_logs``)."""
27    if isinstance(value, list):
28        return [str(item).strip() for item in value if str(item).strip()]
29    return value

Normalize a bool-or-glob-list config (record / capture_logs).

class ParlotBaseProcessor(opentelemetry.sdk.trace.SpanProcessor):
 23class ParlotBaseProcessor(SpanProcessor):
 24    """
 25    Subclass and override ``on_end`` (or the ``_enrich`` dispatch method) to
 26    add framework-specific span enrichment. Call ``super().on_start()`` /
 27    ``super().on_end()`` so shared debug logging runs.
 28
 29    Do NOT wrap a downstream processor — add alongside BatchSpanProcessor:
 30
 31        provider.add_span_processor(MyFrameworkProcessor())
 32        provider.add_span_processor(BatchSpanProcessor(your_exporter))
 33    """
 34
 35    def on_start(self, span, parent_context=None) -> None:
 36        log_span_event("on_start", span)
 37
 38    def on_end(self, span: ReadableSpan) -> None:
 39        log_span_event("on_end", span)
 40
 41    def shutdown(self) -> None:
 42        pass
 43
 44    def force_flush(self, timeout_millis: int = 30_000) -> bool:
 45        return True
 46
 47    # ------------------------------------------------------------------
 48    # Shared helpers — used by all subclasses
 49    # ------------------------------------------------------------------
 50
 51    @staticmethod
 52    def _mutable_attributes(span: ReadableSpan) -> dict:
 53        """Return a mutable ``span._attributes`` dict (copy if needed)."""
 54        attrs = span._attributes
 55        if attrs is None:
 56            span._attributes = {}
 57            return span._attributes
 58        if getattr(attrs, "_immutable", False) or not isinstance(attrs, dict):
 59            span._attributes = dict(attrs)
 60            return span._attributes
 61        return attrs
 62
 63    @staticmethod
 64    def _set(span: ReadableSpan, key: str, value) -> None:
 65        """Write an attribute into a ReadableSpan after it has ended.
 66
 67        OTel SDK ≥1.44 stores attributes in an immutable ``BoundedAttributes``
 68        by default; mutating it raises ``TypeError``. Copy to a plain dict
 69        when needed so enrichment can still stamp session/turn fields.
 70        """
 71        attrs = ParlotBaseProcessor._mutable_attributes(span)
 72        try:
 73            attrs[key] = value
 74        except TypeError:
 75            span._attributes = dict(attrs)
 76            span._attributes[key] = value
 77
 78    @staticmethod
 79    def _delete(span: ReadableSpan, key: str) -> None:
 80        """Remove an attribute from a ReadableSpan after it has ended."""
 81        attrs = span._attributes
 82        if not attrs or key not in attrs:
 83            return
 84        mutable = ParlotBaseProcessor._mutable_attributes(span)
 85        try:
 86            mutable.pop(key, None)
 87        except TypeError:
 88            span._attributes = {k: v for k, v in dict(mutable).items() if k != key}
 89
 90    @staticmethod
 91    def _add_event(span: ReadableSpan, name: str, attributes: dict) -> None:
 92        """Append a span event to a ReadableSpan after it has ended."""
 93        from opentelemetry.sdk.trace import Event
 94        evt = Event(name=name, attributes=attributes, timestamp=time.time_ns())
 95        if hasattr(span, "_events") and isinstance(span._events, list):
 96            span._events.append(evt)
 97
 98    @staticmethod
 99    def _trace_id_hex(span: ReadableSpan) -> str:
100        return format(span.context.trace_id, "032x")
101
102    @staticmethod
103    def _maybe_update(state: SessionState, attr: str, value) -> None:
104        """Set a state attribute only if it is currently falsy."""
105        if value and not getattr(state, attr, ""):
106            setattr(state, attr, str(value))

Subclass and override on_end (or the _enrich dispatch method) to add framework-specific span enrichment. Call super().on_start() / super().on_end() so shared debug logging runs.

Do NOT wrap a downstream processor — add alongside BatchSpanProcessor:

provider.add_span_processor(MyFrameworkProcessor())
provider.add_span_processor(BatchSpanProcessor(your_exporter))
def on_start(self, span, parent_context=None) -> None:
35    def on_start(self, span, parent_context=None) -> None:
36        log_span_event("on_start", span)

Called when a opentelemetry.trace.Span is started.

This method is called synchronously on the thread that starts the span, therefore it should not block or throw an exception.

Arguments:
  • span: The opentelemetry.trace.Span that just started.
  • parent_context: The parent context of the span that just started.
def on_end(self, span: opentelemetry.sdk.trace.ReadableSpan) -> None:
38    def on_end(self, span: ReadableSpan) -> None:
39        log_span_event("on_end", span)

Called when a opentelemetry.trace.Span is ended.

This method is called synchronously on the thread that ends the span, therefore it should not block or throw an exception.

Arguments:
  • span: The opentelemetry.trace.Span that just ended.
def shutdown(self) -> None:
41    def shutdown(self) -> None:
42        pass

Called when a opentelemetry.sdk.trace.TracerProvider is shutdown.

def force_flush(self, timeout_millis: int = 30000) -> bool:
44    def force_flush(self, timeout_millis: int = 30_000) -> bool:
45        return True

Export all ended spans to the configured Exporter that have not yet been exported.

Arguments:
  • timeout_millis: The maximum amount of time to wait for spans to be exported.
Returns:

False if the timeout is exceeded, True otherwise.

class ParlotMetricsRecorder:
165class ParlotMetricsRecorder:
166    """Records Parlot ``usage.*``, ``turn.*``, and ``session.*`` instruments."""
167
168    def __init__(
169        self,
170        meter_provider: MeterProvider,
171        *,
172        meter_name: str = "parlot.core",
173        framework: str = "",
174        tenant_id: str = "",
175    ) -> None:
176        self._meter_provider = meter_provider
177        self._framework = (framework or "").strip()
178        self._tenant_id = (tenant_id or "").strip()
179        meter = meter_provider.get_meter(meter_name)
180        self._meter = meter
181
182        self._usage_stt = meter.create_counter(
183            METRIC_USAGE_STT_AUDIO_DURATION,
184            unit="s",
185            description="STT audio duration per usage event",
186        )
187        self._usage_tts_audio = meter.create_counter(
188            METRIC_USAGE_TTS_AUDIO_DURATION,
189            unit="s",
190            description="TTS audio duration per usage event",
191        )
192        self._usage_tts_chars = meter.create_counter(
193            METRIC_USAGE_TTS_CHARACTERS,
194            description="TTS characters synthesized per usage event",
195        )
196        self._usage_llm_in = meter.create_counter(
197            METRIC_USAGE_LLM_INPUT_TOKENS,
198            description="LLM input tokens per usage event",
199        )
200        self._usage_llm_out = meter.create_counter(
201            METRIC_USAGE_LLM_OUTPUT_TOKENS,
202            description="LLM output tokens per usage event",
203        )
204
205        self._turn_e2e = meter.create_histogram(
206            "turn.e2e_latency_ms",
207            unit="ms",
208            description="End-to-end latency for one agent turn",
209        )
210        self._turn_llm_ttft = meter.create_histogram(
211            "turn.llm_ttft_ms",
212            unit="ms",
213            description="LLM time to first token for one agent turn",
214        )
215        self._turn_tts_ttfb = meter.create_histogram(
216            "turn.tts_ttfb_ms",
217            unit="ms",
218            description="TTS time to first byte for one agent turn",
219        )
220        self._turn_transcription_delay = meter.create_histogram(
221            "turn.transcription_delay_ms",
222            unit="ms",
223            description="Speech end to transcript available for one agent turn",
224        )
225        self._turn_eou_delay = meter.create_histogram(
226            "turn.eou_delay_ms",
227            unit="ms",
228            description="End-of-utterance detection delay for one agent turn",
229        )
230        self._turn_playback_latency = meter.create_histogram(
231            "turn.playback_latency_ms",
232            unit="ms",
233            description="Playback latency for one agent turn",
234        )
235
236        self._session_turns = meter.create_counter(
237            "session.turn_count",
238            description="Total turns in a session",
239        )
240        self._session_input_tokens = meter.create_counter(
241            "session.total_input_tokens",
242            description="Total input tokens in session",
243        )
244        self._session_output_tokens = meter.create_counter(
245            "session.total_output_tokens",
246            description="Total output tokens in session",
247        )
248
249    @property
250    def meter_provider(self) -> MeterProvider:
251        return self._meter_provider
252
253    @property
254    def meter(self):
255        return self._meter
256
257    def _base_attrs(
258        self,
259        *,
260        session_id: str,
261        model: str = "",
262        provider: str = "",
263        framework: str = "",
264        participant_role: str = "",
265        extra: Optional[Mapping[str, AttributeValue]] = None,
266    ) -> dict[str, AttributeValue]:
267        attrs: dict[str, AttributeValue] = {
268            ATTR_SESSION_ID: session_id,
269        }
270        fw = (framework or self._framework).strip()
271        if fw:
272            attrs[ATTR_AGENT_FRAMEWORK] = fw
273        if self._tenant_id:
274            attrs[ATTR_TENANT_ID] = self._tenant_id
275        if model:
276            attrs[ATTR_GEN_AI_MODEL] = model
277        if provider:
278            attrs[ATTR_GEN_AI_PROVIDER] = provider
279        if participant_role:
280            attrs[ATTR_TURN_PARTICIPANT_ROLE] = participant_role
281        if extra:
282            attrs.update(dict(extra))
283        return attrs
284
285    def record_stt_audio(
286        self,
287        *,
288        session_id: str,
289        audio_seconds: float,
290        model: str = "",
291        provider: str = "",
292        framework: str = "",
293        extra: Optional[Mapping[str, AttributeValue]] = None,
294    ) -> None:
295        if not session_id or audio_seconds <= 0:
296            return
297        self._usage_stt.add(
298            float(audio_seconds),
299            attributes=self._base_attrs(
300                session_id=session_id,
301                model=model,
302                provider=provider,
303                framework=framework,
304                extra=extra,
305            ),
306        )
307
308    def record_tts_audio(
309        self,
310        *,
311        session_id: str,
312        audio_seconds: float,
313        model: str = "",
314        provider: str = "",
315        framework: str = "",
316        extra: Optional[Mapping[str, AttributeValue]] = None,
317    ) -> None:
318        if not session_id or audio_seconds <= 0:
319            return
320        self._usage_tts_audio.add(
321            float(audio_seconds),
322            attributes=self._base_attrs(
323                session_id=session_id,
324                model=model,
325                provider=provider,
326                framework=framework,
327                extra=extra,
328            ),
329        )
330
331    def record_tts_characters(
332        self,
333        *,
334        session_id: str,
335        characters: int,
336        model: str = "",
337        provider: str = "",
338        framework: str = "",
339        extra: Optional[Mapping[str, AttributeValue]] = None,
340    ) -> None:
341        if not session_id or characters <= 0:
342            return
343        self._usage_tts_chars.add(
344            int(characters),
345            attributes=self._base_attrs(
346                session_id=session_id,
347                model=model,
348                provider=provider,
349                framework=framework,
350                extra=extra,
351            ),
352        )
353
354    def record_llm_tokens(
355        self,
356        *,
357        session_id: str,
358        input_tokens: int = 0,
359        output_tokens: int = 0,
360        model: str = "",
361        provider: str = "",
362        framework: str = "",
363        extra: Optional[Mapping[str, AttributeValue]] = None,
364    ) -> None:
365        if not session_id:
366            return
367        attrs = self._base_attrs(
368            session_id=session_id,
369            model=model,
370            provider=provider,
371            framework=framework,
372            extra=extra,
373        )
374        if input_tokens > 0:
375            self._usage_llm_in.add(int(input_tokens), attributes=attrs)
376        if output_tokens > 0:
377            self._usage_llm_out.add(int(output_tokens), attributes=attrs)
378
379    def record_turn(
380        self,
381        *,
382        session_id: str,
383        turn_index: int,
384        participant_role: str = "agent",
385        e2e_latency_s: Optional[float] = None,
386        llm_ttft_s: Optional[float] = None,
387        tts_ttfb_s: Optional[float] = None,
388        transcription_delay_s: Optional[float] = None,
389        eou_delay_s: Optional[float] = None,
390        playback_latency_s: Optional[float] = None,
391        interrupted: bool = False,
392        framework: str = "",
393        extra: Optional[Mapping[str, AttributeValue]] = None,
394    ) -> None:
395        if not session_id:
396            return
397        attrs = self._base_attrs(
398            session_id=session_id,
399            framework=framework,
400            participant_role=participant_role,
401            extra=extra,
402        )
403        attrs[ATTR_TURN_INDEX] = int(turn_index)
404        if interrupted:
405            attrs[ATTR_TURN_INTERRUPTED] = True
406        if e2e_latency_s is not None:
407            self._turn_e2e.record(float(e2e_latency_s) * 1000.0, attributes=attrs)
408        if llm_ttft_s is not None:
409            self._turn_llm_ttft.record(float(llm_ttft_s) * 1000.0, attributes=attrs)
410        if tts_ttfb_s is not None:
411            self._turn_tts_ttfb.record(float(tts_ttfb_s) * 1000.0, attributes=attrs)
412        if transcription_delay_s is not None:
413            self._turn_transcription_delay.record(
414                float(transcription_delay_s) * 1000.0, attributes=attrs
415            )
416        if eou_delay_s is not None:
417            self._turn_eou_delay.record(float(eou_delay_s) * 1000.0, attributes=attrs)
418        if playback_latency_s is not None:
419            self._turn_playback_latency.record(
420                float(playback_latency_s) * 1000.0, attributes=attrs
421            )
422
423    def record_session_close(
424        self,
425        *,
426        session_id: str,
427        turn_count: int = 0,
428        total_input_tokens: int = 0,
429        total_output_tokens: int = 0,
430        framework: str = "",
431        extra: Optional[Mapping[str, AttributeValue]] = None,
432    ) -> None:
433        if not session_id:
434            return
435        attrs = self._base_attrs(
436            session_id=session_id, framework=framework, extra=extra
437        )
438        if turn_count:
439            self._session_turns.add(int(turn_count), attributes=attrs)
440        if total_input_tokens:
441            self._session_input_tokens.add(int(total_input_tokens), attributes=attrs)
442        if total_output_tokens:
443            self._session_output_tokens.add(int(total_output_tokens), attributes=attrs)
444
445    def force_flush(self, timeout_millis: int = 5_000) -> bool:
446        try:
447            return bool(self._meter_provider.force_flush(timeout_millis))
448        except Exception:
449            return False
450
451    def shutdown(self, timeout_millis: int = 5_000) -> None:
452        """Flush and stop periodic export. Safe to call more than once."""
453        try:
454            self.force_flush(timeout_millis)
455        except Exception:
456            pass
457        try:
458            self._meter_provider.shutdown(timeout_millis=timeout_millis)
459        except Exception:
460            logger.debug("MeterProvider shutdown failed", exc_info=True)

Records Parlot usage.*, turn.*, and session.* instruments.

ParlotMetricsRecorder( meter_provider: opentelemetry.sdk.metrics._internal.MeterProvider, *, meter_name: str = 'parlot.core', framework: str = '', tenant_id: str = '')
168    def __init__(
169        self,
170        meter_provider: MeterProvider,
171        *,
172        meter_name: str = "parlot.core",
173        framework: str = "",
174        tenant_id: str = "",
175    ) -> None:
176        self._meter_provider = meter_provider
177        self._framework = (framework or "").strip()
178        self._tenant_id = (tenant_id or "").strip()
179        meter = meter_provider.get_meter(meter_name)
180        self._meter = meter
181
182        self._usage_stt = meter.create_counter(
183            METRIC_USAGE_STT_AUDIO_DURATION,
184            unit="s",
185            description="STT audio duration per usage event",
186        )
187        self._usage_tts_audio = meter.create_counter(
188            METRIC_USAGE_TTS_AUDIO_DURATION,
189            unit="s",
190            description="TTS audio duration per usage event",
191        )
192        self._usage_tts_chars = meter.create_counter(
193            METRIC_USAGE_TTS_CHARACTERS,
194            description="TTS characters synthesized per usage event",
195        )
196        self._usage_llm_in = meter.create_counter(
197            METRIC_USAGE_LLM_INPUT_TOKENS,
198            description="LLM input tokens per usage event",
199        )
200        self._usage_llm_out = meter.create_counter(
201            METRIC_USAGE_LLM_OUTPUT_TOKENS,
202            description="LLM output tokens per usage event",
203        )
204
205        self._turn_e2e = meter.create_histogram(
206            "turn.e2e_latency_ms",
207            unit="ms",
208            description="End-to-end latency for one agent turn",
209        )
210        self._turn_llm_ttft = meter.create_histogram(
211            "turn.llm_ttft_ms",
212            unit="ms",
213            description="LLM time to first token for one agent turn",
214        )
215        self._turn_tts_ttfb = meter.create_histogram(
216            "turn.tts_ttfb_ms",
217            unit="ms",
218            description="TTS time to first byte for one agent turn",
219        )
220        self._turn_transcription_delay = meter.create_histogram(
221            "turn.transcription_delay_ms",
222            unit="ms",
223            description="Speech end to transcript available for one agent turn",
224        )
225        self._turn_eou_delay = meter.create_histogram(
226            "turn.eou_delay_ms",
227            unit="ms",
228            description="End-of-utterance detection delay for one agent turn",
229        )
230        self._turn_playback_latency = meter.create_histogram(
231            "turn.playback_latency_ms",
232            unit="ms",
233            description="Playback latency for one agent turn",
234        )
235
236        self._session_turns = meter.create_counter(
237            "session.turn_count",
238            description="Total turns in a session",
239        )
240        self._session_input_tokens = meter.create_counter(
241            "session.total_input_tokens",
242            description="Total input tokens in session",
243        )
244        self._session_output_tokens = meter.create_counter(
245            "session.total_output_tokens",
246            description="Total output tokens in session",
247        )
meter_provider: opentelemetry.sdk.metrics._internal.MeterProvider
249    @property
250    def meter_provider(self) -> MeterProvider:
251        return self._meter_provider
meter
253    @property
254    def meter(self):
255        return self._meter
def record_stt_audio( self, *, session_id: str, audio_seconds: float, model: str = '', provider: str = '', framework: str = '', extra: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None = None) -> None:
285    def record_stt_audio(
286        self,
287        *,
288        session_id: str,
289        audio_seconds: float,
290        model: str = "",
291        provider: str = "",
292        framework: str = "",
293        extra: Optional[Mapping[str, AttributeValue]] = None,
294    ) -> None:
295        if not session_id or audio_seconds <= 0:
296            return
297        self._usage_stt.add(
298            float(audio_seconds),
299            attributes=self._base_attrs(
300                session_id=session_id,
301                model=model,
302                provider=provider,
303                framework=framework,
304                extra=extra,
305            ),
306        )
def record_tts_audio( self, *, session_id: str, audio_seconds: float, model: str = '', provider: str = '', framework: str = '', extra: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None = None) -> None:
308    def record_tts_audio(
309        self,
310        *,
311        session_id: str,
312        audio_seconds: float,
313        model: str = "",
314        provider: str = "",
315        framework: str = "",
316        extra: Optional[Mapping[str, AttributeValue]] = None,
317    ) -> None:
318        if not session_id or audio_seconds <= 0:
319            return
320        self._usage_tts_audio.add(
321            float(audio_seconds),
322            attributes=self._base_attrs(
323                session_id=session_id,
324                model=model,
325                provider=provider,
326                framework=framework,
327                extra=extra,
328            ),
329        )
def record_tts_characters( self, *, session_id: str, characters: int, model: str = '', provider: str = '', framework: str = '', extra: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None = None) -> None:
331    def record_tts_characters(
332        self,
333        *,
334        session_id: str,
335        characters: int,
336        model: str = "",
337        provider: str = "",
338        framework: str = "",
339        extra: Optional[Mapping[str, AttributeValue]] = None,
340    ) -> None:
341        if not session_id or characters <= 0:
342            return
343        self._usage_tts_chars.add(
344            int(characters),
345            attributes=self._base_attrs(
346                session_id=session_id,
347                model=model,
348                provider=provider,
349                framework=framework,
350                extra=extra,
351            ),
352        )
def record_llm_tokens( self, *, session_id: str, input_tokens: int = 0, output_tokens: int = 0, model: str = '', provider: str = '', framework: str = '', extra: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None = None) -> None:
354    def record_llm_tokens(
355        self,
356        *,
357        session_id: str,
358        input_tokens: int = 0,
359        output_tokens: int = 0,
360        model: str = "",
361        provider: str = "",
362        framework: str = "",
363        extra: Optional[Mapping[str, AttributeValue]] = None,
364    ) -> None:
365        if not session_id:
366            return
367        attrs = self._base_attrs(
368            session_id=session_id,
369            model=model,
370            provider=provider,
371            framework=framework,
372            extra=extra,
373        )
374        if input_tokens > 0:
375            self._usage_llm_in.add(int(input_tokens), attributes=attrs)
376        if output_tokens > 0:
377            self._usage_llm_out.add(int(output_tokens), attributes=attrs)
def record_turn( self, *, session_id: str, turn_index: int, participant_role: str = 'agent', e2e_latency_s: float | None = None, llm_ttft_s: float | None = None, tts_ttfb_s: float | None = None, transcription_delay_s: float | None = None, eou_delay_s: float | None = None, playback_latency_s: float | None = None, interrupted: bool = False, framework: str = '', extra: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None = None) -> None:
379    def record_turn(
380        self,
381        *,
382        session_id: str,
383        turn_index: int,
384        participant_role: str = "agent",
385        e2e_latency_s: Optional[float] = None,
386        llm_ttft_s: Optional[float] = None,
387        tts_ttfb_s: Optional[float] = None,
388        transcription_delay_s: Optional[float] = None,
389        eou_delay_s: Optional[float] = None,
390        playback_latency_s: Optional[float] = None,
391        interrupted: bool = False,
392        framework: str = "",
393        extra: Optional[Mapping[str, AttributeValue]] = None,
394    ) -> None:
395        if not session_id:
396            return
397        attrs = self._base_attrs(
398            session_id=session_id,
399            framework=framework,
400            participant_role=participant_role,
401            extra=extra,
402        )
403        attrs[ATTR_TURN_INDEX] = int(turn_index)
404        if interrupted:
405            attrs[ATTR_TURN_INTERRUPTED] = True
406        if e2e_latency_s is not None:
407            self._turn_e2e.record(float(e2e_latency_s) * 1000.0, attributes=attrs)
408        if llm_ttft_s is not None:
409            self._turn_llm_ttft.record(float(llm_ttft_s) * 1000.0, attributes=attrs)
410        if tts_ttfb_s is not None:
411            self._turn_tts_ttfb.record(float(tts_ttfb_s) * 1000.0, attributes=attrs)
412        if transcription_delay_s is not None:
413            self._turn_transcription_delay.record(
414                float(transcription_delay_s) * 1000.0, attributes=attrs
415            )
416        if eou_delay_s is not None:
417            self._turn_eou_delay.record(float(eou_delay_s) * 1000.0, attributes=attrs)
418        if playback_latency_s is not None:
419            self._turn_playback_latency.record(
420                float(playback_latency_s) * 1000.0, attributes=attrs
421            )
def record_session_close( self, *, session_id: str, turn_count: int = 0, total_input_tokens: int = 0, total_output_tokens: int = 0, framework: str = '', extra: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None = None) -> None:
423    def record_session_close(
424        self,
425        *,
426        session_id: str,
427        turn_count: int = 0,
428        total_input_tokens: int = 0,
429        total_output_tokens: int = 0,
430        framework: str = "",
431        extra: Optional[Mapping[str, AttributeValue]] = None,
432    ) -> None:
433        if not session_id:
434            return
435        attrs = self._base_attrs(
436            session_id=session_id, framework=framework, extra=extra
437        )
438        if turn_count:
439            self._session_turns.add(int(turn_count), attributes=attrs)
440        if total_input_tokens:
441            self._session_input_tokens.add(int(total_input_tokens), attributes=attrs)
442        if total_output_tokens:
443            self._session_output_tokens.add(int(total_output_tokens), attributes=attrs)
def force_flush(self, timeout_millis: int = 5000) -> bool:
445    def force_flush(self, timeout_millis: int = 5_000) -> bool:
446        try:
447            return bool(self._meter_provider.force_flush(timeout_millis))
448        except Exception:
449            return False
def shutdown(self, timeout_millis: int = 5000) -> None:
451    def shutdown(self, timeout_millis: int = 5_000) -> None:
452        """Flush and stop periodic export. Safe to call more than once."""
453        try:
454            self.force_flush(timeout_millis)
455        except Exception:
456            pass
457        try:
458            self._meter_provider.shutdown(timeout_millis=timeout_millis)
459        except Exception:
460            logger.debug("MeterProvider shutdown failed", exc_info=True)

Flush and stop periodic export. Safe to call more than once.

class QuietOTLPMetricExporter(opentelemetry.sdk.metrics._internal.export.MetricExporter):
 59class QuietOTLPMetricExporter(MetricExporter):
 60    """Wraps an OTLP metric exporter; quota 429s are swallowed after one process log."""
 61
 62    def __init__(
 63        self,
 64        exporter: MetricExporter,
 65        *,
 66        endpoint_label: str,
 67    ) -> None:
 68        super().__init__(
 69            preferred_temporality=exporter._preferred_temporality,
 70            preferred_aggregation=exporter._preferred_aggregation,
 71        )
 72        self._exporter = exporter
 73        self._endpoint_label = endpoint_label
 74        self._quota_responses: list[tuple[int, str]] = []
 75        attach_otlp_http_quota_capture(
 76            exporter,
 77            on_quota=self._record_quota_response,
 78        )
 79
 80    def _record_quota_response(self, status_code: int, body_text: str) -> None:
 81        self._quota_responses.append((status_code, body_text))
 82
 83    def _swallow_quota_if_present(self) -> MetricExportResult | None:
 84        if not self._quota_responses:
 85            return None
 86        status_code, body_text = self._quota_responses[-1]
 87        if status_code == 429:
 88            log_parlot_ingest_quota_once(
 89                channel="metrics",
 90                status_code=status_code,
 91                body_text=body_text,
 92            )
 93            return MetricExportResult.SUCCESS
 94        return None
 95
 96    def export(
 97        self,
 98        metrics_data: MetricsData,
 99        timeout_millis: float = 10_000,
100        **kwargs,
101    ) -> MetricExportResult:
102        self._quota_responses.clear()
103        try:
104            result = self._exporter.export(
105                metrics_data, timeout_millis=timeout_millis, **kwargs
106            )
107        except Exception as exc:
108            swallowed = self._swallow_quota_if_present()
109            if swallowed is not None:
110                return swallowed
111            logger.warning(
112                "Parlot metrics export failed (%s): %s",
113                self._endpoint_label,
114                _short_metric_error(exc),
115            )
116            return MetricExportResult.FAILURE
117
118        swallowed = self._swallow_quota_if_present()
119        if swallowed is not None:
120            return swallowed
121        if result == MetricExportResult.FAILURE:
122            logger.warning(
123                "Parlot metrics export returned FAILURE (%s)",
124                self._endpoint_label,
125            )
126        return result
127
128    def force_flush(self, timeout_millis: float = 10_000) -> bool:
129        return self._exporter.force_flush(timeout_millis=timeout_millis)
130
131    def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
132        self._exporter.shutdown(timeout_millis=timeout_millis, **kwargs)

Wraps an OTLP metric exporter; quota 429s are swallowed after one process log.

QuietOTLPMetricExporter( exporter: opentelemetry.sdk.metrics._internal.export.MetricExporter, *, endpoint_label: str)
62    def __init__(
63        self,
64        exporter: MetricExporter,
65        *,
66        endpoint_label: str,
67    ) -> None:
68        super().__init__(
69            preferred_temporality=exporter._preferred_temporality,
70            preferred_aggregation=exporter._preferred_aggregation,
71        )
72        self._exporter = exporter
73        self._endpoint_label = endpoint_label
74        self._quota_responses: list[tuple[int, str]] = []
75        attach_otlp_http_quota_capture(
76            exporter,
77            on_quota=self._record_quota_response,
78        )
def export( self, metrics_data: opentelemetry.sdk.metrics._internal.point.MetricsData, timeout_millis: float = 10000, **kwargs) -> opentelemetry.sdk.metrics._internal.export.MetricExportResult:
 96    def export(
 97        self,
 98        metrics_data: MetricsData,
 99        timeout_millis: float = 10_000,
100        **kwargs,
101    ) -> MetricExportResult:
102        self._quota_responses.clear()
103        try:
104            result = self._exporter.export(
105                metrics_data, timeout_millis=timeout_millis, **kwargs
106            )
107        except Exception as exc:
108            swallowed = self._swallow_quota_if_present()
109            if swallowed is not None:
110                return swallowed
111            logger.warning(
112                "Parlot metrics export failed (%s): %s",
113                self._endpoint_label,
114                _short_metric_error(exc),
115            )
116            return MetricExportResult.FAILURE
117
118        swallowed = self._swallow_quota_if_present()
119        if swallowed is not None:
120            return swallowed
121        if result == MetricExportResult.FAILURE:
122            logger.warning(
123                "Parlot metrics export returned FAILURE (%s)",
124                self._endpoint_label,
125            )
126        return result

Exports a batch of telemetry data.

Arguments:
  • metrics: The list of opentelemetry.sdk.metrics.export.Metric objects to be exported
Returns:

The result of the export

def force_flush(self, timeout_millis: float = 10000) -> bool:
128    def force_flush(self, timeout_millis: float = 10_000) -> bool:
129        return self._exporter.force_flush(timeout_millis=timeout_millis)

Ensure that export of any metrics currently received by the exporter are completed as soon as possible.

def shutdown(self, timeout_millis: float = 30000, **kwargs) -> None:
131    def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
132        self._exporter.shutdown(timeout_millis=timeout_millis, **kwargs)

Shuts down the exporter.

Called when the SDK is shut down.

UsageMetricsRecorder = <class 'ParlotMetricsRecorder'>
def assert_sync_span_processors(provider) -> bool:
109def assert_sync_span_processors(provider) -> bool:
110    """Require ``SynchronousMultiSpanProcessor`` on the provider.
111
112    Parlot processors rely on synchronous ``on_start`` / ``on_end`` ordering and
113    often on ``contextvars`` copied at task creation. ``ConcurrentMultiSpanProcessor``
114    runs handlers on worker threads and breaks that model.
115
116    Returns ``False`` when ``ConcurrentMultiSpanProcessor`` is active (callers
117    should disable OTel ``context.attach`` and similar). Raises ``RuntimeError``
118    for unknown processor layouts.
119
120    Uses OTel SDK-private ``_active_span_processor`` — re-validate on SDK upgrades.
121    """
122    from opentelemetry.sdk.trace import (
123        ConcurrentMultiSpanProcessor,
124        SynchronousMultiSpanProcessor,
125    )
126
127    asp = getattr(provider, "_active_span_processor", None)
128    if asp is None:
129        raise RuntimeError(
130            "parlot: TracerProvider has no _active_span_processor; "
131            "cannot verify synchronous span processing"
132        )
133    if isinstance(asp, ConcurrentMultiSpanProcessor):
134        logger.error(
135            "parlot: ConcurrentMultiSpanProcessor detected — "
136            "unsupported for Parlot span processors (use synchronous layout)"
137        )
138        return False
139    if not isinstance(asp, SynchronousMultiSpanProcessor):
140        raise RuntimeError(
141            f"parlot: unsupported active span processor {type(asp).__name__!r}; "
142            "expected SynchronousMultiSpanProcessor"
143        )
144    return True

Require SynchronousMultiSpanProcessor on the provider.

Parlot processors rely on synchronous on_start / on_end ordering and often on contextvars copied at task creation. ConcurrentMultiSpanProcessor runs handlers on worker threads and breaks that model.

Returns False when ConcurrentMultiSpanProcessor is active (callers should disable OTel context.attach and similar). Raises RuntimeError for unknown processor layouts.

Uses OTel SDK-private _active_span_processor — re-validate on SDK upgrades.

@dataclass
class SessionState:
20@dataclass
21class SessionState:
22    """
23    Per-trace accumulator for cross-span aggregates.
24
25    Framework-specific instrumentation packages subclass this to add their own
26    fields (e.g. LiveKit adds agent_chain, pending_handoff_end_ns).
27    """
28
29    session_id: str = ""
30    room_name: str = ""
31    room_sid: str = ""
32    agent_label: str = ""
33
34    turn_count: int = 0
35    tool_call_count: int = 0
36    handoff_count: int = 0
37
38    total_input_tokens: int = 0
39    total_output_tokens: int = 0
40    total_cached_tokens: int = 0
41    total_cost_usd: float = 0.0
42
43    human_rep_participant_ids: set[str] = field(default_factory=set)
44    topology_agents: list[dict[str, Any]] = field(default_factory=list)
45    custom_metadata: dict[str, str] = field(default_factory=dict)
46    # Owning instrumentation package, e.g. "livekit" | "langgraph"
47    framework: str = ""
48    # Sample-0 wall ms for media_segment_* (first write wins via set_recording_anchor_wall_ms).
49    recording_anchor_wall_ms: int | None = None

Per-trace accumulator for cross-span aggregates.

Framework-specific instrumentation packages subclass this to add their own fields (e.g. LiveKit adds agent_chain, pending_handoff_end_ns).

SessionState( session_id: str = '', room_name: str = '', room_sid: str = '', agent_label: str = '', turn_count: int = 0, tool_call_count: int = 0, handoff_count: int = 0, total_input_tokens: int = 0, total_output_tokens: int = 0, total_cached_tokens: int = 0, total_cost_usd: float = 0.0, human_rep_participant_ids: set[str] = <factory>, topology_agents: list[dict[str, typing.Any]] = <factory>, custom_metadata: dict[str, str] = <factory>, framework: str = '', recording_anchor_wall_ms: int | None = None)
session_id: str = ''
room_name: str = ''
room_sid: str = ''
agent_label: str = ''
turn_count: int = 0
tool_call_count: int = 0
handoff_count: int = 0
total_input_tokens: int = 0
total_output_tokens: int = 0
total_cached_tokens: int = 0
total_cost_usd: float = 0.0
human_rep_participant_ids: set[str]
topology_agents: list[dict[str, typing.Any]]
custom_metadata: dict[str, str]
framework: str = ''
recording_anchor_wall_ms: int | None = None
@dataclass
class SessionTopology:
111@dataclass
112class SessionTopology:
113    """
114    Accumulates intent segments and agent instructions during a live session.
115
116    Framework adapters call ``record_instructions`` / ``open_segment_after_handoff``
117    during the session and ``stamp_session_span`` at close.
118    """
119
120    prompts_by_agent: dict[str, str] = field(default_factory=dict)
121    intent_by_agent: dict[str, dict[str, str]] = field(default_factory=dict)
122    # Expected to be a small fixed agent roster (not session-/user-suffixed ids).
123    agents_seen: dict[str, dict[str, Any]] = field(default_factory=dict)
124    intent_segments: list[_MutableSegment] = field(default_factory=list)
125    active_segment: _MutableSegment | None = None
126    pending_segment_from_turn: int | None = None
127    first_agent_label: str = ""
128    agent_chain: list[str] = field(default_factory=list)
129    handoff_count: int = 0
130    _handoff_keys: set[str] = field(default_factory=set)
131    _handoff_keys_turn: int | None = None
132
133    def upsert_agent(self, agent_id: str) -> None:
134        aid = agent_id.strip()
135        if not aid:
136            return
137        if not self.first_agent_label:
138            self.first_agent_label = aid
139        if aid not in self.agents_seen:
140            self.agents_seen[aid] = {"id": aid, "role": "agent"}
141
142    def record_instructions(self, agent_id: str, text: str) -> None:
143        # Keep the longer excerpt so a later shorter capture does not shrink the preview.
144        aid = agent_id.strip()
145        if not aid or not text.strip():
146            return
147        excerpt = _truncate(text.strip(), INSTRUCTIONS_PREVIEW)
148        prev = self.prompts_by_agent.get(aid, "")
149        if len(excerpt) >= len(prev):
150            self.prompts_by_agent[aid] = excerpt
151        self._sync_agent_intent_summary(aid)
152        self._refresh_open_segment_instructions(aid)
153
154    def push_agent_chain(self, agent_id: str) -> None:
155        aid = agent_id.strip()
156        if not aid:
157            return
158        if not self.agent_chain or self.agent_chain[-1] != aid:
159            self.agent_chain.append(aid)
160            trim_agent_chain(self.agent_chain)
161
162    def open_bootstrap_segment(self, agent_id: str, from_turn: int) -> None:
163        if self.active_segment is not None:
164            return
165        aid = agent_id.strip() or "unknown"
166        self.upsert_agent(aid)
167        self.active_segment = self._new_segment(aid, from_turn, 0)
168
169    def open_segment_after_handoff(
170        self,
171        agent_id: str,
172        *,
173        handoff_index: int | None = None,
174        turn_index: int,
175        from_agent: str = "",
176    ) -> None:
177        aid = agent_id.strip()
178        if not aid:
179            return
180        if self._handoff_keys_turn != turn_index:
181            self._handoff_keys.clear()
182            self._handoff_keys_turn = turn_index
183        dedupe_key = f"{from_agent.strip()}->{aid}@{turn_index}"
184        if dedupe_key in self._handoff_keys:
185            return
186        self._handoff_keys.add(dedupe_key)
187
188        self.close_active_segment(turn_index)
189        self.upsert_agent(aid)
190        self.handoff_count += 1
191        effective_handoff = handoff_index if handoff_index is not None else self.handoff_count
192        if not self.intent_segments and self.active_segment is None:
193            effective_handoff = 0
194        self.pending_segment_from_turn = max(1, turn_index + 1)
195        self.active_segment = self._new_segment(
196            aid,
197            self.pending_segment_from_turn,
198            effective_handoff,
199        )
200
201    def close_active_segment(self, to_turn: int) -> None:
202        if self.active_segment is None:
203            return
204        self.active_segment.to_turn = max(0, to_turn)
205        closed = self.active_segment
206        self.active_segment = None
207        if self.intent_segments and _merge_same_agent_into(self.intent_segments[-1], closed):
208            self._trim_closed_segments()
209            return
210        self.intent_segments.append(closed)
211        self._trim_closed_segments()
212
213    def _trim_closed_segments(self) -> None:
214        """Drop oldest closed segments only; never touches ``active_segment``."""
215        overflow = len(self.intent_segments) - MAX_INTENT_SEGMENTS
216        if overflow > 0:
217            del self.intent_segments[:overflow]
218
219    def apply_pending_from_turn_on_emit(self, turn_index: int) -> None:
220        if self.active_segment is None:
221            return
222        if self.pending_segment_from_turn is not None:
223            self.active_segment.from_turn = self.pending_segment_from_turn
224            self.pending_segment_from_turn = None
225        elif self.active_segment.from_turn <= 0:
226            self.active_segment.from_turn = turn_index
227
228    def finalize_intent_sequence(self, final_turn: int) -> list[dict[str, Any]]:
229        if self.active_segment is not None:
230            self.close_active_segment(final_turn)
231        return self._collapse_segments(
232            [
233                {
234                    "segment_index": idx,
235                    "agent_id": seg.agent_id,
236                    "intent_key": seg.intent_key,
237                    "intent_label": seg.intent_label,
238                    "from_turn": seg.from_turn,
239                    "handoff_index": seg.handoff_index,
240                    "source": seg.source,
241                    **(
242                        {"to_turn": seg.to_turn}
243                        if seg.to_turn is not None
244                        else {}
245                    ),
246                    **(
247                        {"instructions_excerpt": seg.instructions_excerpt}
248                        if seg.instructions_excerpt
249                        else {}
250                    ),
251                }
252                for idx, seg in enumerate(self.intent_segments)
253            ]
254        )
255
256    def bootstrap_instructions(self) -> str:
257        if self.first_agent_label:
258            text = self.prompts_by_agent.get(self.first_agent_label, "")
259            if text:
260                return text
261        if self.prompts_by_agent:
262            return max(self.prompts_by_agent.values(), key=len)
263        return ""
264
265    def agents_json(self) -> list[dict[str, Any]]:
266        out: list[dict[str, Any]] = []
267        for node in self.agents_seen.values():
268            summary = self.intent_by_agent.get(node["id"], {})
269            entry = dict(node)
270            if summary:
271                entry["intent_key"] = summary.get("intent_key", node["id"])
272                entry["intent_label"] = summary.get("intent_label", node["id"])
273                excerpt = summary.get("instructions_excerpt", "")
274                if excerpt:
275                    entry["instructions_excerpt"] = excerpt
276            out.append(entry)
277        return out
278
279    def stamp_session_span(self, span: Any, *, final_turn: int) -> None:
280        """Write topology attrs on the live ``parlot.session`` span."""
281        if span is None or not hasattr(span, "set_attribute"):
282            return
283        sequence = self.finalize_intent_sequence(final_turn)
284        agents = self.agents_json()
285        bootstrap = self.bootstrap_instructions()
286        if agents:
287            agents_json, agents_truncated = _json_dumps_cap(agents)
288            span.set_attribute(ATTR_SESSION_TOPOLOGY_AGENTS, agents_json)
289            if agents_truncated:
290                span.set_attribute(ATTR_SESSION_TOPOLOGY_AGENTS_TRUNCATED, True)
291        if bootstrap:
292            span.set_attribute(ATTR_SESSION_TOPOLOGY_BOOTSTRAP_INSTRUCTIONS, bootstrap)
293        if sequence:
294            seq_json, seq_truncated = _json_dumps_cap(sequence)
295            span.set_attribute(ATTR_SESSION_INTENT_SEQUENCE, seq_json)
296            if seq_truncated:
297                span.set_attribute(ATTR_SESSION_INTENT_SEQUENCE_TRUNCATED, True)
298        if self.agent_chain:
299            chain = " → ".join(self.agent_chain)
300            span.set_attribute(ATTR_SESSION_AGENT_CHAIN, _truncate(chain, MAX_JSON_CHARS))
301            if len(chain) > MAX_JSON_CHARS:
302                span.set_attribute(ATTR_SESSION_AGENT_CHAIN_TRUNCATED, True)
303
304    def _new_segment(self, agent_id: str, from_turn: int, handoff_index: int) -> _MutableSegment:
305        instructions = self.prompts_by_agent.get(agent_id, "")
306        derived = derive_intent(agent_id, instructions)
307        self._sync_agent_intent_summary(agent_id)
308        return _MutableSegment(
309            segment_index=0,
310            agent_id=agent_id,
311            intent_key=derived["intent_key"],
312            intent_label=derived["intent_label"],
313            from_turn=from_turn,
314            handoff_index=handoff_index,
315            instructions_excerpt=derived.get("instructions_excerpt", ""),
316        )
317
318    def _sync_agent_intent_summary(self, agent_id: str) -> None:
319        instructions = self.prompts_by_agent.get(agent_id, "")
320        derived = derive_intent(agent_id, instructions)
321        self.intent_by_agent[agent_id] = derived
322        node = self.agents_seen.get(agent_id)
323        if node is not None:
324            node["intent_key"] = derived["intent_key"]
325            node["intent_label"] = derived["intent_label"]
326            if derived.get("instructions_excerpt"):
327                node["instructions_excerpt"] = derived["instructions_excerpt"]
328
329    def _refresh_open_segment_instructions(self, agent_id: str) -> None:
330        if self.active_segment is None or self.active_segment.agent_id != agent_id.strip():
331            return
332        instructions = self.prompts_by_agent.get(agent_id, "")
333        derived = derive_intent(agent_id, instructions)
334        self.active_segment.intent_key = derived["intent_key"]
335        self.active_segment.intent_label = derived["intent_label"]
336        self.active_segment.instructions_excerpt = derived.get("instructions_excerpt", "")
337        self._sync_agent_intent_summary(agent_id)
338
339    @staticmethod
340    def _collapse_segments(segments: list[dict[str, Any]]) -> list[dict[str, Any]]:
341        if not segments:
342            return []
343        merged: list[dict[str, Any]] = []
344        for seg in segments:
345            if not merged:
346                merged.append(dict(seg))
347                continue
348            prev = merged[-1]
349            if _merge_same_agent_into(prev, seg):
350                continue
351            merged.append(dict(seg))
352        for idx, seg in enumerate(merged):
353            seg["segment_index"] = idx
354        return merged

Accumulates intent segments and agent instructions during a live session.

Framework adapters call record_instructions / open_segment_after_handoff during the session and stamp_session_span at close.

SessionTopology( prompts_by_agent: dict[str, str] = <factory>, intent_by_agent: dict[str, dict[str, str]] = <factory>, agents_seen: dict[str, dict[str, typing.Any]] = <factory>, intent_segments: list[parlot.core.topology._MutableSegment] = <factory>, active_segment: parlot.core.topology._MutableSegment | None = None, pending_segment_from_turn: int | None = None, first_agent_label: str = '', agent_chain: list[str] = <factory>, handoff_count: int = 0, _handoff_keys: set[str] = <factory>, _handoff_keys_turn: int | None = None)
prompts_by_agent: dict[str, str]
intent_by_agent: dict[str, dict[str, str]]
agents_seen: dict[str, dict[str, typing.Any]]
intent_segments: list[parlot.core.topology._MutableSegment]
active_segment: parlot.core.topology._MutableSegment | None = None
pending_segment_from_turn: int | None = None
first_agent_label: str = ''
agent_chain: list[str]
handoff_count: int = 0
def upsert_agent(self, agent_id: str) -> None:
133    def upsert_agent(self, agent_id: str) -> None:
134        aid = agent_id.strip()
135        if not aid:
136            return
137        if not self.first_agent_label:
138            self.first_agent_label = aid
139        if aid not in self.agents_seen:
140            self.agents_seen[aid] = {"id": aid, "role": "agent"}
def record_instructions(self, agent_id: str, text: str) -> None:
142    def record_instructions(self, agent_id: str, text: str) -> None:
143        # Keep the longer excerpt so a later shorter capture does not shrink the preview.
144        aid = agent_id.strip()
145        if not aid or not text.strip():
146            return
147        excerpt = _truncate(text.strip(), INSTRUCTIONS_PREVIEW)
148        prev = self.prompts_by_agent.get(aid, "")
149        if len(excerpt) >= len(prev):
150            self.prompts_by_agent[aid] = excerpt
151        self._sync_agent_intent_summary(aid)
152        self._refresh_open_segment_instructions(aid)
def push_agent_chain(self, agent_id: str) -> None:
154    def push_agent_chain(self, agent_id: str) -> None:
155        aid = agent_id.strip()
156        if not aid:
157            return
158        if not self.agent_chain or self.agent_chain[-1] != aid:
159            self.agent_chain.append(aid)
160            trim_agent_chain(self.agent_chain)
def open_bootstrap_segment(self, agent_id: str, from_turn: int) -> None:
162    def open_bootstrap_segment(self, agent_id: str, from_turn: int) -> None:
163        if self.active_segment is not None:
164            return
165        aid = agent_id.strip() or "unknown"
166        self.upsert_agent(aid)
167        self.active_segment = self._new_segment(aid, from_turn, 0)
def open_segment_after_handoff( self, agent_id: str, *, handoff_index: int | None = None, turn_index: int, from_agent: str = '') -> None:
169    def open_segment_after_handoff(
170        self,
171        agent_id: str,
172        *,
173        handoff_index: int | None = None,
174        turn_index: int,
175        from_agent: str = "",
176    ) -> None:
177        aid = agent_id.strip()
178        if not aid:
179            return
180        if self._handoff_keys_turn != turn_index:
181            self._handoff_keys.clear()
182            self._handoff_keys_turn = turn_index
183        dedupe_key = f"{from_agent.strip()}->{aid}@{turn_index}"
184        if dedupe_key in self._handoff_keys:
185            return
186        self._handoff_keys.add(dedupe_key)
187
188        self.close_active_segment(turn_index)
189        self.upsert_agent(aid)
190        self.handoff_count += 1
191        effective_handoff = handoff_index if handoff_index is not None else self.handoff_count
192        if not self.intent_segments and self.active_segment is None:
193            effective_handoff = 0
194        self.pending_segment_from_turn = max(1, turn_index + 1)
195        self.active_segment = self._new_segment(
196            aid,
197            self.pending_segment_from_turn,
198            effective_handoff,
199        )
def close_active_segment(self, to_turn: int) -> None:
201    def close_active_segment(self, to_turn: int) -> None:
202        if self.active_segment is None:
203            return
204        self.active_segment.to_turn = max(0, to_turn)
205        closed = self.active_segment
206        self.active_segment = None
207        if self.intent_segments and _merge_same_agent_into(self.intent_segments[-1], closed):
208            self._trim_closed_segments()
209            return
210        self.intent_segments.append(closed)
211        self._trim_closed_segments()
def apply_pending_from_turn_on_emit(self, turn_index: int) -> None:
219    def apply_pending_from_turn_on_emit(self, turn_index: int) -> None:
220        if self.active_segment is None:
221            return
222        if self.pending_segment_from_turn is not None:
223            self.active_segment.from_turn = self.pending_segment_from_turn
224            self.pending_segment_from_turn = None
225        elif self.active_segment.from_turn <= 0:
226            self.active_segment.from_turn = turn_index
def finalize_intent_sequence(self, final_turn: int) -> list[dict[str, typing.Any]]:
228    def finalize_intent_sequence(self, final_turn: int) -> list[dict[str, Any]]:
229        if self.active_segment is not None:
230            self.close_active_segment(final_turn)
231        return self._collapse_segments(
232            [
233                {
234                    "segment_index": idx,
235                    "agent_id": seg.agent_id,
236                    "intent_key": seg.intent_key,
237                    "intent_label": seg.intent_label,
238                    "from_turn": seg.from_turn,
239                    "handoff_index": seg.handoff_index,
240                    "source": seg.source,
241                    **(
242                        {"to_turn": seg.to_turn}
243                        if seg.to_turn is not None
244                        else {}
245                    ),
246                    **(
247                        {"instructions_excerpt": seg.instructions_excerpt}
248                        if seg.instructions_excerpt
249                        else {}
250                    ),
251                }
252                for idx, seg in enumerate(self.intent_segments)
253            ]
254        )
def bootstrap_instructions(self) -> str:
256    def bootstrap_instructions(self) -> str:
257        if self.first_agent_label:
258            text = self.prompts_by_agent.get(self.first_agent_label, "")
259            if text:
260                return text
261        if self.prompts_by_agent:
262            return max(self.prompts_by_agent.values(), key=len)
263        return ""
def agents_json(self) -> list[dict[str, typing.Any]]:
265    def agents_json(self) -> list[dict[str, Any]]:
266        out: list[dict[str, Any]] = []
267        for node in self.agents_seen.values():
268            summary = self.intent_by_agent.get(node["id"], {})
269            entry = dict(node)
270            if summary:
271                entry["intent_key"] = summary.get("intent_key", node["id"])
272                entry["intent_label"] = summary.get("intent_label", node["id"])
273                excerpt = summary.get("instructions_excerpt", "")
274                if excerpt:
275                    entry["instructions_excerpt"] = excerpt
276            out.append(entry)
277        return out
def stamp_session_span(self, span: Any, *, final_turn: int) -> None:
279    def stamp_session_span(self, span: Any, *, final_turn: int) -> None:
280        """Write topology attrs on the live ``parlot.session`` span."""
281        if span is None or not hasattr(span, "set_attribute"):
282            return
283        sequence = self.finalize_intent_sequence(final_turn)
284        agents = self.agents_json()
285        bootstrap = self.bootstrap_instructions()
286        if agents:
287            agents_json, agents_truncated = _json_dumps_cap(agents)
288            span.set_attribute(ATTR_SESSION_TOPOLOGY_AGENTS, agents_json)
289            if agents_truncated:
290                span.set_attribute(ATTR_SESSION_TOPOLOGY_AGENTS_TRUNCATED, True)
291        if bootstrap:
292            span.set_attribute(ATTR_SESSION_TOPOLOGY_BOOTSTRAP_INSTRUCTIONS, bootstrap)
293        if sequence:
294            seq_json, seq_truncated = _json_dumps_cap(sequence)
295            span.set_attribute(ATTR_SESSION_INTENT_SEQUENCE, seq_json)
296            if seq_truncated:
297                span.set_attribute(ATTR_SESSION_INTENT_SEQUENCE_TRUNCATED, True)
298        if self.agent_chain:
299            chain = " → ".join(self.agent_chain)
300            span.set_attribute(ATTR_SESSION_AGENT_CHAIN, _truncate(chain, MAX_JSON_CHARS))
301            if len(chain) > MAX_JSON_CHARS:
302                span.set_attribute(ATTR_SESSION_AGENT_CHAIN_TRUNCATED, True)

Write topology attrs on the live parlot.session span.

def add_platform_ref(kind: str, value: str, *, framework: str = 'custom') -> None:
 89def add_platform_ref(
 90    kind: str,
 91    value: str,
 92    *,
 93    framework: str = "custom",
 94) -> None:
 95    """
 96    Attach a searchable external ID to the active Parlot session.
 97
 98    Stamps ``platform.ref.{kind}`` (and the primary triple when this is the
 99    first ref) on the live session span so the session can be found by that
100    value in Parlot search / resolve.
101
102    Args:
103        kind: Identifier type (e.g. ``"crm_ticket"``, ``"order_number"``,
104            ``"call_sid"``).
105        value: Unique identifier value (e.g. ``"TKT-9921"``).
106        framework: Originating framework name. Defaults to ``"custom"``.
107
108    Example::
109
110        from parlot.instrumentation.livekit import add_platform_ref
111
112        add_platform_ref("crm_ticket", "TKT-9")
113    """
114    kind = str(kind or "").strip()
115    value = str(value or "").strip()
116    framework = str(framework or "custom").strip() or "custom"
117    if not kind or not value:
118        return
119    span = get_active_session_span()
120    if span is None:
121        return
122    stamp_platform_refs(span, [(framework, kind, value)])

Attach a searchable external ID to the active Parlot session.

Stamps platform.ref.{kind} (and the primary triple when this is the first ref) on the live session span so the session can be found by that value in Parlot search / resolve.

Arguments:
  • kind: Identifier type (e.g. "crm_ticket", "order_number", "call_sid").
  • value: Unique identifier value (e.g. "TKT-9921").
  • framework: Originating framework name. Defaults to "custom".

Example::

from parlot.instrumentation.livekit import add_platform_ref

add_platform_ref("crm_ticket", "TKT-9")
def adopt_existing_tracer_provider() -> Any | None:
144def adopt_existing_tracer_provider() -> Any | None:
145    """Return the global tracer provider when already set by another Parlot package.
146
147    Returns ``None`` when only the default no-op / proxy provider is installed.
148    """
149    from opentelemetry import trace
150    from opentelemetry.sdk.trace import TracerProvider as SdkTracerProvider
151
152    provider = trace.get_tracer_provider()
153    if isinstance(provider, SdkTracerProvider):
154        return provider
155    return None

Return the global tracer provider when already set by another Parlot package.

Returns None when only the default no-op / proxy provider is installed.

def build_otlp_http_exporter( *, endpoint: str, api_key: str = '') -> opentelemetry.sdk.trace.export.SpanExporter:
 91def build_otlp_http_exporter(
 92    *,
 93    endpoint: str,
 94    api_key: str = "",
 95) -> SpanExporter:
 96    """Build an OTLP/HTTP span exporter targeting ``{endpoint}/v1/traces``."""
 97    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
 98
 99    if not endpoint:
100        raise ValueError(
101            "No OTLP endpoint configured. Pass endpoint= or set the "
102            "PARLOT_ENDPOINT environment variable."
103        )
104    headers = build_parlot_client_headers(api_key)
105    trace_endpoint = endpoint.rstrip("/") + "/v1/traces"
106    return OTLPSpanExporter(endpoint=trace_endpoint, headers=headers)

Build an OTLP/HTTP span exporter targeting {endpoint}/v1/traces.

def build_parlot_client_headers(api_key: str = '') -> dict[str, str]:
76def build_parlot_client_headers(api_key: str = "") -> dict[str, str]:
77    """Build standard transport headers sent with Parlot telemetry exports."""
78    from parlot.core.sdk_version import resolve_parlot_sdk_version
79
80    version = resolve_parlot_sdk_version() or "unknown"
81    headers: dict[str, str] = {
82        HEADER_SDK_NAME: "parlot-python",
83        HEADER_SDK_VERSION: version,
84        HEADER_INGESTION_VERSION: INGESTION_PROTOCOL_VERSION,
85    }
86    if api_key:
87        headers["Authorization"] = f"Bearer {api_key}"
88    return headers

Build standard transport headers sent with Parlot telemetry exports.

def build_parlot_meter_provider( endpoint: str, resource: opentelemetry.sdk.resources.Resource, *, api_key: str = '', headers: Mapping[str, str] | None = None, export_interval_millis: int = 5000, quiet: bool = True) -> opentelemetry.sdk.metrics._internal.MeterProvider:
135def build_parlot_meter_provider(
136    endpoint: str,
137    resource: Resource,
138    *,
139    api_key: str = "",
140    headers: Optional[Mapping[str, str]] = None,
141    export_interval_millis: int = DEFAULT_METRICS_EXPORT_INTERVAL_MS,
142    quiet: bool = True,
143) -> MeterProvider:
144    """Build a dedicated MeterProvider exporting Parlot metrics.
145
146    Do **not** register this as the global meter provider when the host
147    framework also emits its own meters (LiveKit ``lk.agents.usage.*``).
148    """
149    metrics_endpoint = endpoint.rstrip("/") + "/v1/metrics"
150    resolved_headers = (
151        dict(headers) if headers is not None else build_parlot_client_headers(api_key)
152    )
153    raw = OTLPMetricExporter(endpoint=metrics_endpoint, headers=resolved_headers)
154    exporter: MetricExporter = (
155        QuietOTLPMetricExporter(raw, endpoint_label=metrics_endpoint)
156        if quiet
157        else raw
158    )
159    reader = PeriodicExportingMetricReader(
160        exporter, export_interval_millis=int(export_interval_millis)
161    )
162    return MeterProvider(metric_readers=[reader], resource=resource)

Build a dedicated MeterProvider exporting Parlot metrics.

Do not register this as the global meter provider when the host framework also emits its own meters (LiveKit lk.agents.usage.*).

def build_resource( *, service_name: str | None = None, service_version: str | None = None, agent_id: str = '') -> opentelemetry.sdk.resources.Resource:
53def build_resource(
54    *,
55    service_name: Optional[str] = None,
56    service_version: Optional[str] = None,
57    agent_id: str = "",
58) -> Resource:
59    attrs = {
60        service_attributes.SERVICE_NAME: resolve_service_name(
61            service_name,
62            agent_id=agent_id,
63        ),
64    }
65    if service_version:
66        attrs[service_attributes.SERVICE_VERSION] = service_version
67    return Resource.create(attrs)
def build_tracer_provider( *, endpoint: str | None = None, api_key: str | None = None, service_name: str | None = None, service_version: str | None = None, span_processors: Sequence[opentelemetry.sdk.trace.SpanProcessor] = (), span_exporter: opentelemetry.sdk.trace.export.SpanExporter | None = None) -> opentelemetry.sdk.trace.TracerProvider:
109def build_tracer_provider(
110    *,
111    endpoint: Optional[str] = None,
112    api_key: Optional[str] = None,
113    service_name: Optional[str] = None,
114    service_version: Optional[str] = None,
115    span_processors: Sequence[SpanProcessor] = (),
116    span_exporter: SpanExporter | None = None,
117) -> TracerProvider:
118    """Build a ``TracerProvider`` with optional processors and OTLP export.
119
120    When ``span_exporter`` is omitted, builds a default OTLP/HTTP exporter to
121    ``{endpoint}/v1/traces``. Callers typically wrap that exporter (filter,
122    sanitize, quiet) before passing it here.
123    """
124    resolved_endpoint = resolve_endpoint(endpoint)
125    resolved_api_key = resolve_api_key(api_key)
126    resource = build_resource(
127        service_name=service_name,
128        service_version=service_version,
129    )
130    exporter = span_exporter
131    if exporter is None:
132        exporter = build_otlp_http_exporter(
133            endpoint=resolved_endpoint,
134            api_key=resolved_api_key,
135        )
136
137    provider = TracerProvider(resource=resource)
138    for processor in span_processors:
139        provider.add_span_processor(processor)
140    provider.add_span_processor(BatchSpanProcessor(exporter))
141    return provider

Build a TracerProvider with optional processors and OTLP export.

When span_exporter is omitted, builds a default OTLP/HTTP exporter to {endpoint}/v1/traces. Callers typically wrap that exporter (filter, sanitize, quiet) before passing it here.

HEADER_SDK_NAME = 'x-parlot-sdk-name'
HEADER_SDK_VERSION = 'x-parlot-sdk-version'
HEADER_INGESTION_VERSION = 'x-parlot-ingestion-version'
INGESTION_PROTOCOL_VERSION = '1'
def clear_active_session() -> None:
71def clear_active_session() -> None:
72    """Clear the active session bindings for this context."""
73    set_active_session(None, None)

Clear the active session bindings for this context.

def derive_intent(agent_id: str, instructions: str = '') -> dict[str, str]:
36def derive_intent(agent_id: str, instructions: str = "") -> dict[str, str]:
37    """Port of platform ingest ``deriveIntent``."""
38    aid = agent_id.strip()
39    intent_key = _normalize_key(aid) if aid else "unknown"
40    excerpt = _truncate(instructions.strip(), INSTRUCTIONS_EXCERPT_MAX) if instructions.strip() else ""
41
42    if excerpt:
43        first = _first_line(excerpt)
44        intent_label = _truncate(first, INTENT_LABEL_MAX) if first else _title_from_id(intent_key)
45    else:
46        intent_label = _title_from_id(intent_key)
47
48    return {
49        "intent_key": intent_key,
50        "intent_label": intent_label,
51        "instructions_excerpt": excerpt,
52    }

Port of platform ingest deriveIntent.

def fetch_telemetry_bootstrap( endpoint: str, api_key: str, context: ParlotContext, *, timeout: float = 15.0) -> dict[str, Any] | None:
15def fetch_telemetry_bootstrap(
16    endpoint: str,
17    api_key: str,
18    context: ParlotContext,
19    *,
20    timeout: float = 15.0,
21) -> Optional[dict[str, Any]]:
22    """GET ``/v1/telemetry/bootstrap`` and store runtime on ``context``, or None."""
23    if not endpoint or not api_key:
24        return None
25
26    import httpx
27
28    from parlot.core.runtime import runtime_from_bootstrap
29
30    url = f"{endpoint.rstrip('/')}/v1/telemetry/bootstrap"
31    headers = {"Authorization": f"Bearer {api_key}"}
32    try:
33        with httpx.Client(timeout=timeout) as client:
34            resp = client.get(url, headers=headers)
35        if resp.status_code >= 400:
36            logger.error(
37                "parlot: telemetry bootstrap failed status=%s endpoint=%s",
38                resp.status_code,
39                endpoint.rstrip("/"),
40            )
41            return None
42        payload = resp.json()
43        if not isinstance(payload, dict):
44            logger.error(
45                "parlot: telemetry bootstrap returned non-object JSON endpoint=%s",
46                endpoint.rstrip("/"),
47            )
48            return None
49        context.runtime = runtime_from_bootstrap(endpoint, api_key, payload)
50        logger.info(
51            "parlot: telemetry bootstrap ok (endpoint=%s)",
52            endpoint.rstrip("/"),
53        )
54        return payload
55    except Exception as exc:
56        logger.error(
57            "parlot: telemetry bootstrap request failed — %s (endpoint=%s)",
58            exc,
59            endpoint.rstrip("/"),
60        )
61        logger.debug("parlot: telemetry bootstrap request failed", exc_info=True)
62        return None

GET /v1/telemetry/bootstrap and store runtime on context, or None.

def get_active_session() -> SessionState | None:
52def get_active_session() -> SessionState | None:
53    """Return the session state bound to the current context, if any."""
54    return _active_session_state.get()

Return the session state bound to the current context, if any.

def get_active_session_span() -> opentelemetry.trace.span.Span | None:
57def get_active_session_span() -> Span | None:
58    """Return the active ``parlot.session`` span for the current context, if any."""
59    return _active_session_span.get()

Return the active parlot.session span for the current context, if any.

@contextmanager
def human_escalation(label: str | None = None) -> Iterator[NoneType]:
50@contextmanager
51def human_escalation(label: str | None = None) -> Iterator[None]:
52    """Mark the next participant who joins the active session as a human rep.
53
54    Args:
55        label: Optional role or team label for the incoming human representative.
56    """
57    token = _pending_escalation_label.set(label)
58    try:
59        yield
60    finally:
61        _pending_escalation_label.reset(token)

Mark the next participant who joins the active session as a human rep.

Arguments:
  • label: Optional role or team label for the incoming human representative.
def record_human_rep(participant_id: str, *, label: str | None = None) -> None:
19def record_human_rep(participant_id: str, *, label: str | None = None) -> None:
20    """
21    Mark a participant as a human representative.
22
23    Stamps ``session.topology.agents`` on the active session span and registers
24    the participant for ``turn.participant_role=human_rep`` on future turns.
25
26    Args:
27        participant_id: Participant identifier within the room/session.
28        label: Optional human-readable name (e.g. ``"Tier 2 Escalation Desk"``).
29    """
30    participant_id = str(participant_id or "").strip()
31    if not participant_id:
32        return
33
34    state = get_active_session()
35    if state is not None:
36        state.human_rep_participant_ids.add(participant_id)
37        entry: dict[str, str] = {"id": participant_id, "role": "human_rep"}
38        if label:
39            entry["label"] = label
40        if not any(a.get("id") == participant_id for a in state.topology_agents):
41            state.topology_agents.append(entry)
42
43    span = get_active_session_span()
44    if span is not None and state is not None and state.topology_agents:
45        span.set_attribute(
46            ATTR_SESSION_TOPOLOGY_AGENTS, json.dumps(state.topology_agents)
47        )

Mark a participant as a human representative.

Stamps session.topology.agents on the active session span and registers the participant for turn.participant_role=human_rep on future turns.

Arguments:
  • participant_id: Participant identifier within the room/session.
  • label: Optional human-readable name (e.g. "Tier 2 Escalation Desk").
def remap_native_span_name( native: str, attrs: Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]] | None = None, *, llm_names: Collection[str] = (), tool_names: Collection[str] = (), tts_names: Collection[str] = (), stt_names: Collection[str] = (), drop_names: Collection[str] = (), tool_name_attrs: Collection[str] = ('gen_ai.tool.name',), unknown: Literal['drop', 'passthrough'] = 'drop', custom: Callable[[str, Mapping[str, str | bool | int | float | Sequence[str] | Sequence[bool] | Sequence[int] | Sequence[float]]], str | None | object] | None = None) -> str | None:
30def remap_native_span_name(
31    native: str,
32    attrs: Mapping[str, AttributeValue] | None = None,
33    *,
34    llm_names: Collection[str] = (),
35    tool_names: Collection[str] = (),
36    tts_names: Collection[str] = (),
37    stt_names: Collection[str] = (),
38    drop_names: Collection[str] = (),
39    tool_name_attrs: Collection[str] = (ATTR_GEN_AI_TOOL_NAME,),
40    unknown: UnknownPolicy = "drop",
41    custom: Callable[
42        [str, Mapping[str, AttributeValue]], str | None | object
43    ]
44    | None = None,
45) -> str | None:
46    """Map a vendor-native span name to a GenAI / voice export name.
47
48    Returns ``None`` to drop the span from the export name path.
49
50    ``custom`` may return a string, ``None`` (drop), or the sentinel
51    ``NotImplemented`` to fall through to the table-driven rules.
52    """
53    attrs = attrs or {}
54    native_name = native or ""
55
56    if custom is not None:
57        result = custom(native_name, attrs)
58        if result is not NotImplemented:
59            return result  # type: ignore[return-value]
60
61    if native_name in drop_names:
62        return None
63    if native_name in llm_names:
64        model = str(attrs.get(ATTR_GEN_AI_MODEL) or "").strip() or None
65        return span_name_chat(model)
66    if native_name in tool_names:
67        tool = ""
68        for key in tool_name_attrs:
69            tool = str(attrs.get(key) or "").strip()
70            if tool:
71                break
72        return span_name_execute_tool(tool)
73    if native_name in tts_names:
74        return SPAN_VOICE_TTS
75    if native_name in stt_names:
76        return SPAN_VOICE_STT
77    if unknown == "passthrough":
78        return native_name
79    return None

Map a vendor-native span name to a GenAI / voice export name.

Returns None to drop the span from the export name path.

custom may return a string, None (drop), or the sentinel NotImplemented to fall through to the table-driven rules.

def resolve_api_key(api_key: str | None = None) -> str:
23def resolve_api_key(api_key: Optional[str] = None) -> str:
24    """Resolve API key from kwarg or ``PARLOT_API_KEY``."""
25    return api_key or os.environ.get("PARLOT_API_KEY", "") or ""

Resolve API key from kwarg or PARLOT_API_KEY.

def resolve_capture_genai_content(capture_genai_content: bool | None = None) -> bool | None:
28def resolve_capture_genai_content(capture_genai_content: Optional[bool] = None) -> Optional[bool]:
29    """Return the explicit ``parlotize(capture_genai_content=)`` override, or None.
30
31    When None, callers should resolve via ``should_capture_genai_content`` against
32    bootstrap policy (default on).
33    """
34    if capture_genai_content is not None:
35        return bool(capture_genai_content)
36    return None

Return the explicit parlotize(capture_genai_content=) override, or None.

When None, callers should resolve via should_capture_genai_content against bootstrap policy (default on).

def resolve_endpoint(endpoint: str | None = None) -> str:
18def resolve_endpoint(endpoint: Optional[str] = None) -> str:
19    """Resolve OTLP base endpoint from kwarg or ``PARLOT_ENDPOINT``."""
20    return (endpoint or os.environ.get("PARLOT_ENDPOINT", "") or "").rstrip("/")

Resolve OTLP base endpoint from kwarg or PARLOT_ENDPOINT.

def resolve_service_name(service_name: str | None = None, *, agent_id: str = '') -> str:
39def resolve_service_name(
40    service_name: Optional[str] = None,
41    *,
42    agent_id: str = "",
43) -> str:
44    """Resolve OTel ``service.name``: explicit kwarg, else ``agent_id``, else ``unknown``.
45
46    Matches ``ParlotizeProtocol`` / adapter docs so every instrumentation uses the
47    same default without repeating framework fallbacks.
48    """
49    name = (service_name or "").strip() or (agent_id or "").strip()
50    return name or "unknown"

Resolve OTel service.name: explicit kwarg, else agent_id, else unknown.

Matches ParlotizeProtocol / adapter docs so every instrumentation uses the same default without repeating framework fallbacks.

def resolve_parlot_sdk_version() -> str:
26def resolve_parlot_sdk_version() -> str:
27    """Return installed parlot-core version, cached after first call."""
28    global _cached_version
29    if _cached_version is not None:
30        return _cached_version
31
32    try:
33        _cached_version = _normalize_version(metadata.version(_PARLOT_CORE_DIST))
34    except metadata.PackageNotFoundError:
35        _cached_version = ""
36
37    return _cached_version

Return installed parlot-core version, cached after first call.

def runtime_from_bootstrap( endpoint: str, api_key: str, payload: dict[str, typing.Any]) -> ParlotRuntimeContext:
136def runtime_from_bootstrap(
137    endpoint: str, api_key: str, payload: dict[str, Any]
138) -> ParlotRuntimeContext:
139    globs, agents = _parse_recording_policy(payload)
140    (
141        logs_globs,
142        logs_agents,
143        logs_agent_min_levels,
144        logs_min_level,
145        logs_present,
146    ) = _parse_logs_policy(payload)
147    (
148        capture_globs,
149        capture_agents,
150        capture_present,
151    ) = _parse_capture_genai_content_policy(payload)
152    return ParlotRuntimeContext(
153        endpoint=endpoint.rstrip("/"),
154        api_key=api_key,
155        tenant_id=str(payload.get("tenant_id") or ""),
156        content_bucket=str(payload.get("content_bucket") or ""),
157        r2_endpoint=str(payload.get("r2_endpoint") or ""),
158        recording_globs=globs,
159        recording_agents=agents,
160        logs_globs=logs_globs,
161        logs_agents=logs_agents,
162        logs_agent_min_levels=logs_agent_min_levels,
163        logs_min_level=logs_min_level,
164        logs_policy_present=logs_present,
165        capture_genai_content_globs=capture_globs,
166        capture_genai_content_agents=capture_agents,
167        capture_genai_content_policy_present=capture_present,
168    )
def session_owned(*, framework: str | None = None) -> bool:
76def session_owned(*, framework: str | None = None) -> bool:
77    """True when an active Parlot session with a non-empty session_id is bound.
78
79    Args:
80        framework: If provided, also require ``state.framework`` to match
81            (e.g. ``session_owned(framework="livekit")`` for coexistence checks).
82    """
83    state = get_active_session()
84    if state is None or not state.session_id:
85        return False
86    if framework is not None and state.framework != framework:
87        return False
88    return True

True when an active Parlot session with a non-empty session_id is bound.

Arguments:
  • framework: If provided, also require state.framework to match (e.g. session_owned(framework="livekit") for coexistence checks).
def set_active_session( span: opentelemetry.trace.span.Span | None, state: SessionState | None) -> None:
62def set_active_session(
63    span: Span | None,
64    state: SessionState | None,
65) -> None:
66    """Bind (or clear) the active session span + state for this context."""
67    _active_session_span.set(span)
68    _active_session_state.set(state)

Bind (or clear) the active session span + state for this context.

def set_session_attribute(key: str, value: str | int | float | bool) -> None:
20def set_session_attribute(key: str, value: str | int | float | bool) -> None:
21    """
22    Stamp one custom attribute on the active session under ``session.metadata.*``.
23
24    Keys are normalized to ``session.metadata.<key>``. Values are stored as
25    strings on the live session span and remembered on session state so they
26    are also present on ``parlot.session.close``.
27
28    Args:
29        key: Attribute name. If not prefixed with ``session.metadata.``, the
30            prefix is added automatically.
31        value: Value to record (``str``, ``int``, ``float``, or ``bool``).
32    """
33    full_key = session_metadata_key(key)
34    if not full_key or full_key == ATTR_SESSION_METADATA_PREFIX:
35        return
36    str_value = value if isinstance(value, str) else str(value)
37
38    state = get_active_session()
39    if state is not None:
40        state.custom_metadata[full_key] = str_value
41
42    span = get_active_session_span()
43    if span is not None and hasattr(span, "set_attribute"):
44        span.set_attribute(full_key, str_value)

Stamp one custom attribute on the active session under session.metadata.*.

Keys are normalized to session.metadata.<key>. Values are stored as strings on the live session span and remembered on session state so they are also present on parlot.session.close.

Arguments:
  • key: Attribute name. If not prefixed with session.metadata., the prefix is added automatically.
  • value: Value to record (str, int, float, or bool).
def set_session_metadata(**pairs: str | int | float | bool) -> None:
47def set_session_metadata(**pairs: str | int | float | bool) -> None:
48    """
49    Attach custom key/value metadata to the active Parlot session.
50
51    Each keyword becomes ``session.metadata.<name>`` on the session span and
52    appears in session detail in the Parlot UI.
53
54    Args:
55        **pairs: Keyword metadata pairs (values ``str``, ``int``, ``float``,
56            or ``bool``).
57
58    Example::
59
60        from parlot.instrumentation.livekit import set_session_metadata
61
62        set_session_metadata(order_id="12345", crm_ticket="TKT-9")
63    """
64    for key, value in pairs.items():
65        set_session_attribute(key, value)

Attach custom key/value metadata to the active Parlot session.

Each keyword becomes session.metadata.<name> on the session span and appears in session detail in the Parlot UI.

Arguments:
  • **pairs: Keyword metadata pairs (values str, int, float, or bool).

Example::

from parlot.instrumentation.livekit import set_session_metadata

set_session_metadata(order_id="12345", crm_ticket="TKT-9")
def should_capture_genai_content( agent_name: str, *, metadata_capture_genai_content: bool | None = None, capture_genai_content_config: bool | None = None, bootstrap_globs: Sequence[str] | None = None, bootstrap_agents: Mapping[str, bool] | None = None, bootstrap_present: bool = False) -> bool:
30def should_capture_genai_content(
31    agent_name: str,
32    *,
33    metadata_capture_genai_content: Optional[bool] = None,
34    capture_genai_content_config: Optional[bool] = None,
35    bootstrap_globs: Optional[Sequence[str]] = None,
36    bootstrap_agents: Optional[Mapping[str, bool]] = None,
37    bootstrap_present: bool = False,
38) -> bool:
39    """Return True when generative AI / tool content bodies should be captured.
40
41    Precedence: job metadata > ``parlotize(capture_genai_content=…)`` > bootstrap (UI).
42    When bootstrap is absent (``bootstrap_present=False``) or globs are unset,
43    default is **on**. Explicit empty globs means off.
44    """
45    if metadata_capture_genai_content is False:
46        return False
47    if metadata_capture_genai_content is True:
48        return True
49
50    if capture_genai_content_config is not None:
51        return bool(capture_genai_content_config)
52
53    agents = bootstrap_agents or {}
54    if agent_name in agents:
55        return bool(agents[agent_name])
56
57    if not bootstrap_present:
58        return True
59
60    if bootstrap_globs is None:
61        return True
62
63    globs = list(bootstrap_globs)
64    if not globs:
65        return False
66    return matches_allowlist(agent_name, globs)

Return True when generative AI / tool content bodies should be captured.

Precedence: job metadata > parlotize(capture_genai_content=…) > bootstrap (UI). When bootstrap is absent (bootstrap_present=False) or globs are unset, default is on. Explicit empty globs means off.

def should_record( agent_name: str, *, metadata_record: bool | None = None, record_config: bool | Sequence[str] | None = None, bootstrap_globs: Sequence[str] | None = None, bootstrap_agents: Mapping[str, bool] | None = None) -> bool:
28def should_record(
29    agent_name: str,
30    *,
31    metadata_record: Optional[bool] = None,
32    record_config: RecordConfig = None,
33    bootstrap_globs: Optional[Sequence[str]] = None,
34    bootstrap_agents: Optional[Mapping[str, bool]] = None,
35) -> bool:
36    """Return True when session audio recording should start for this job.
37
38    Precedence: job metadata > ``parlotize(record=…)`` > bootstrap (UI) policy.
39    """
40    if metadata_record is False:
41        return False
42    if metadata_record is True:
43        return True
44
45    if record_config is not None:
46        if isinstance(record_config, bool):
47            return record_config
48        return matches_allowlist(agent_name, list(record_config))
49
50    agents = bootstrap_agents or {}
51    if agent_name in agents:
52        return bool(agents[agent_name])
53
54    globs = list(bootstrap_globs or ())
55    if not globs:
56        return False
57    return matches_allowlist(agent_name, globs)

Return True when session audio recording should start for this job.

Precedence: job metadata > parlotize(record=…) > bootstrap (UI) policy.

@runtime_checkable
class RecordingSource(typing.Protocol):
19@runtime_checkable
20class RecordingSource(Protocol):
21    """Adapter-facing recording capture / stamp contract."""
22
23    async def start(self) -> None: ...
24
25    async def finish(self) -> None: ...
26
27    async def fail(self, reason: str) -> None: ...

Adapter-facing recording capture / stamp contract.

RecordingSource(*args, **kwargs)
1866def _no_init_or_replace_init(self, *args, **kwargs):
1867    cls = type(self)
1868
1869    if cls._is_protocol:
1870        raise TypeError('Protocols cannot be instantiated')
1871
1872    # Already using a custom `__init__`. No need to calculate correct
1873    # `__init__` to call. This can lead to RecursionError. See bpo-45121.
1874    if cls.__init__ is not _no_init_or_replace_init:
1875        return
1876
1877    # Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`.
1878    # The first instantiation of the subclass will call `_no_init_or_replace_init` which
1879    # searches for a proper new `__init__` in the MRO. The new `__init__`
1880    # replaces the subclass' old `__init__` (ie `_no_init_or_replace_init`). Subsequent
1881    # instantiation of the protocol subclass will thus use the new
1882    # `__init__` and no longer call `_no_init_or_replace_init`.
1883    for base in cls.__mro__:
1884        init = base.__dict__.get('__init__', _no_init_or_replace_init)
1885        if init is not _no_init_or_replace_init:
1886            cls.__init__ = init
1887            break
1888    else:
1889        # should not happen
1890        cls.__init__ = object.__init__
1891
1892    cls.__init__(self, *args, **kwargs)
async def start(self) -> None:
23    async def start(self) -> None: ...
async def finish(self) -> None:
25    async def finish(self) -> None: ...
async def fail(self, reason: str) -> None:
27    async def fail(self, reason: str) -> None: ...
def stamp_external_recording( *, audio_uri: str, anchor_wall_ms: int, mode: str = 'link', format: str = 'opus_ogg', sample_count: int | None = None, wall_end_ms: int | None = None, pii_redaction_required: bool = False, span: Any = None) -> bool:
30def stamp_external_recording(
31    *,
32    audio_uri: str,
33    anchor_wall_ms: int,
34    mode: str = "link",
35    format: str = "opus_ogg",
36    sample_count: Optional[int] = None,
37    wall_end_ms: Optional[int] = None,
38    pii_redaction_required: bool = False,
39    span: Any = None,
40) -> bool:
41    """BYO recording stamp.
42
43    * ``mode="link"`` — external URI only. Rejected when *pii_redaction_required*.
44    * ``mode="ingest"`` — caller already uploaded via grant; stamp as uploaded.
45
46    Returns False when rejected or no session span.
47    """
48    target = span if span is not None else get_active_session_span()
49    if target is None:
50        logger.warning("parlot: stamp_external_recording skipped — no session span")
51        return False
52
53    if mode == "link" and pii_redaction_required:
54        stamp_recording_error(
55            target,
56            "byo_link_rejected:tenant_pii_redaction_enabled",
57        )
58        return False
59
60    source = "byo_link" if mode == "link" else "byo_ingest"
61    stamp_recording_started(
62        target,
63        anchor_wall_ms=anchor_wall_ms,
64        source=source,
65        format=format,
66    )
67    stamp_recording_uploaded(
68        target,
69        audio_uri=audio_uri,
70        partial=False,
71        sample_count=sample_count,
72        wall_end_ms=wall_end_ms,
73        format=format,
74    )
75    return True

BYO recording stamp.

  • mode="link" — external URI only. Rejected when pii_redaction_required.
  • mode="ingest" — caller already uploaded via grant; stamp as uploaded.

Returns False when rejected or no session span.

def stamp_recording_disabled(span: Any, reason: str) -> None:
43def stamp_recording_disabled(span: Any, reason: str) -> None:
44    stamp_recording_attr(span, ATTR_SESSION_RECORDING_DISABLED_REASON, reason)
45    stamp_recording_attr(span, ATTR_SESSION_RECORDING_STATUS, "disabled")
46    logger.debug("parlot: recording disabled — %s", reason)
def stamp_recording_error(span: Any, error: str) -> None:
49def stamp_recording_error(span: Any, error: str) -> None:
50    msg = error[:500]
51    stamp_recording_attr(span, ATTR_SESSION_RECORDING_ERROR, msg)
52    stamp_recording_attr(span, ATTR_SESSION_RECORDING_STATUS, "failed")
53    if _span_recording(span):
54        try:
55            from opentelemetry.trace import Status, StatusCode
56
57            span.set_status(Status(StatusCode.ERROR, msg))
58        except Exception:
59            logger.debug("parlot: failed to set recording error span status", exc_info=True)
60    # Grant denials are operational errors (Honeycomb + agent logs); keep ERROR.
61    if msg.startswith(("quota_exceeded", "feature_not_granted")):
62        logger.error("parlot: recording failed — %s", msg)
63    else:
64        logger.warning("parlot: recording failed — %s", msg)
def stamp_recording_started( span: Any, *, anchor_wall_ms: int, source: str, format: str = 'pcm_wav') -> None:
67def stamp_recording_started(
68    span: Any,
69    *,
70    anchor_wall_ms: int,
71    source: str,
72    format: str = "pcm_wav",
73) -> None:
74    stamp_recording_attr(span, ATTR_SESSION_RECORDING_ANCHOR_WALL_MS, int(anchor_wall_ms))
75    stamp_recording_attr(span, ATTR_SESSION_RECORDING_STATUS, "recording")
76    stamp_recording_attr(span, ATTR_SESSION_RECORDING_SOURCE, source)
77    stamp_recording_attr(span, ATTR_SESSION_RECORDING_FORMAT, format)
def stamp_recording_uploaded( span: Any, *, audio_uri: str, partial: bool = False, sample_count: int | None = None, wall_end_ms: int | None = None, format: str | None = None, egress_id: str | None = None) -> None:
 80def stamp_recording_uploaded(
 81    span: Any,
 82    *,
 83    audio_uri: str,
 84    partial: bool = False,
 85    sample_count: Optional[int] = None,
 86    wall_end_ms: Optional[int] = None,
 87    format: Optional[str] = None,
 88    egress_id: Optional[str] = None,
 89) -> None:
 90    stamp_recording_attr(span, ATTR_SESSION_RECORDING_AUDIO_URI, audio_uri)
 91    stamp_recording_attr(
 92        span,
 93        ATTR_SESSION_RECORDING_STATUS,
 94        "partial" if partial else "uploaded",
 95    )
 96    stamp_recording_attr(span, ATTR_SESSION_RECORDING_PARTIAL, bool(partial))
 97    if sample_count is not None:
 98        stamp_recording_attr(span, ATTR_SESSION_RECORDING_SAMPLE_COUNT, int(sample_count))
 99    if wall_end_ms is not None:
100        stamp_recording_attr(span, ATTR_SESSION_RECORDING_WALL_END_MS, int(wall_end_ms))
101    if format:
102        stamp_recording_attr(span, ATTR_SESSION_RECORDING_FORMAT, format)
103    if egress_id:
104        stamp_recording_attr(span, ATTR_SESSION_RECORDING_EGRESS_ID, egress_id)
def set_recording_anchor_wall_ms(state: Any, anchor_wall_ms: int) -> None:
13def set_recording_anchor_wall_ms(state: Any, anchor_wall_ms: int) -> None:
14    """Freeze the sample-0 wall clock on *state* (first write wins)."""
15    current = getattr(state, "recording_anchor_wall_ms", None)
16    if current is not None:
17        return
18    state.recording_anchor_wall_ms = int(anchor_wall_ms)

Freeze the sample-0 wall clock on state (first write wins).

def media_segments_from_speech( state: Any, speech_start_wall_ms: int, speech_end_wall_ms: int) -> tuple[int, int]:
21def media_segments_from_speech(
22    state: Any,
23    speech_start_wall_ms: int,
24    speech_end_wall_ms: int,
25) -> tuple[int, int]:
26    """Return ``(start_ms, end_ms)`` offsets from the frozen recording anchor."""
27    anchor = getattr(state, "recording_anchor_wall_ms", None)
28    if anchor is None:
29        return (0, max(0, int(speech_end_wall_ms) - int(speech_start_wall_ms)))
30    start = max(0, int(speech_start_wall_ms) - int(anchor))
31    end = max(start, int(speech_end_wall_ms) - int(anchor))
32    return (start, end)

Return (start_ms, end_ms) offsets from the frozen recording anchor.

def speech_wall_ms_from_span(span: Any) -> tuple[int, int] | None:
35def speech_wall_ms_from_span(span: Any) -> tuple[int, int] | None:
36    """Derive speech wall bounds from an OTel ReadableSpan start/end."""
37    start_ns = getattr(span, "start_time", None)
38    end_ns = getattr(span, "end_time", None)
39    if start_ns is None or end_ns is None:
40        return None
41    try:
42        start_ms = int(start_ns) // 1_000_000
43        end_ms = int(end_ns) // 1_000_000
44    except (TypeError, ValueError):
45        return None
46    if end_ms < start_ms:
47        return None
48    return (start_ms, end_ms)

Derive speech wall bounds from an OTel ReadableSpan start/end.

@dataclass(frozen=True)
class UploadGrantOutcome:
21@dataclass(frozen=True)
22class UploadGrantOutcome:
23    """Result of ``/v1/recordings/upload-grant``.
24
25    On success ``grant`` is the JSON body. On failure ``error`` is a durable
26    ``recording_error`` string (e.g. ``quota_exceeded:session_recordings``).
27    """
28
29    grant: dict[str, Any] | None = None
30    error: str | None = None
31
32    @property
33    def ok(self) -> bool:
34        return self.grant is not None

Result of /v1/recordings/upload-grant.

On success grant is the JSON body. On failure error is a durable recording_error string (e.g. quota_exceeded:session_recordings).

UploadGrantOutcome(grant: dict[str, Any] | None = None, error: str | None = None)
grant: dict[str, Any] | None = None
error: str | None = None
ok: bool
32    @property
33    def ok(self) -> bool:
34        return self.grant is not None
async def fetch_upload_grant( runtime: ParlotRuntimeContext, *, session_id: str, room_name: str | None = None, segment_index: int | None = None, sample_offset: int | None = None, content_type: str | None = None) -> UploadGrantOutcome:
 85async def fetch_upload_grant(
 86    runtime: ParlotRuntimeContext,
 87    *,
 88    session_id: str,
 89    room_name: Optional[str] = None,
 90    segment_index: Optional[int] = None,
 91    sample_offset: Optional[int] = None,
 92    content_type: Optional[str] = None,
 93) -> UploadGrantOutcome:
 94    """POST ``/v1/recordings/upload-grant``; return grant body or structured error."""
 95    url = f"{runtime.endpoint.rstrip('/')}/v1/recordings/upload-grant"
 96    headers = {"Authorization": f"Bearer {runtime.api_key}"}
 97    payload: dict[str, Any] = {"session_id": session_id}
 98    if room_name:
 99        payload["room_name"] = room_name
100    if segment_index is not None:
101        payload["segment_index"] = int(segment_index)
102    if sample_offset is not None:
103        payload["sample_offset"] = int(sample_offset)
104    if content_type:
105        payload["content_type"] = content_type
106    try:
107        async with httpx.AsyncClient(timeout=30.0) as client:
108            resp = await client.post(url, json=payload, headers=headers)
109        if resp.status_code >= 400:
110            error = _parse_upload_grant_error(resp.status_code, resp.text[:500])
111            extras = _quota_log_extras(resp.text[:500])
112            logger.error(
113                "parlot: upload grant failed status=%s error=%s body=%s%s",
114                resp.status_code,
115                error,
116                resp.text[:500],
117                f" ({extras})" if extras else "",
118            )
119            return UploadGrantOutcome(error=error)
120        data = resp.json()
121        if isinstance(data, dict):
122            return UploadGrantOutcome(grant=data)
123        return UploadGrantOutcome(error="upload_grant_failed")
124    except Exception as exc:
125        logger.error("parlot: upload grant request failed — %s", exc)
126        logger.debug("parlot: upload grant request failed", exc_info=True)
127        return UploadGrantOutcome(error="upload_grant_failed")

POST /v1/recordings/upload-grant; return grant body or structured error.

def recording_grant_error(code: str, *, grant_type: str = 'session_recordings') -> str:
37def recording_grant_error(
38    code: str, *, grant_type: str = _SESSION_RECORDINGS_GRANT
39) -> str:
40    """Canonical recording_error for session_recordings grant denials."""
41    return f"{code}:{grant_type}"

Canonical recording_error for session_recordings grant denials.

async def complete_recording( runtime: ParlotRuntimeContext, *, session_id: str, format: str, conversation_id: str | None = None, segment_count: int | None = None, segments: Sequence[Mapping[str, int]] | None = None, sample_rate: int | None = None, anchor_wall_ms: int | None = None, sample_count: int | None = None, wall_end_ms: int | None = None, duration_ms: int | None = None, audio_recording_uri: str | None = None) -> dict[str, Any] | None:
130async def complete_recording(
131    runtime: ParlotRuntimeContext,
132    *,
133    session_id: str,
134    format: str,
135    conversation_id: Optional[str] = None,
136    segment_count: Optional[int] = None,
137    segments: Optional[Sequence[Mapping[str, int]]] = None,
138    sample_rate: Optional[int] = None,
139    anchor_wall_ms: Optional[int] = None,
140    sample_count: Optional[int] = None,
141    wall_end_ms: Optional[int] = None,
142    duration_ms: Optional[int] = None,
143    audio_recording_uri: Optional[str] = None,
144) -> Optional[dict[str, Any]]:
145    """POST ``/v1/recordings/complete``; return JSON body or ``None`` on failure."""
146    url = f"{runtime.endpoint.rstrip('/')}/v1/recordings/complete"
147    headers = {"Authorization": f"Bearer {runtime.api_key}"}
148    payload: dict[str, Any] = {
149        "session_id": session_id,
150        "format": format,
151    }
152    if conversation_id:
153        payload["conversation_id"] = conversation_id
154    if segment_count is not None:
155        payload["segment_count"] = int(segment_count)
156    if segments:
157        payload["segments"] = [
158            {
159                "segment_index": int(s["segment_index"]),
160                "sample_offset": int(s["sample_offset"]),
161            }
162            for s in segments
163        ]
164    if sample_rate is not None:
165        payload["sample_rate"] = int(sample_rate)
166    if anchor_wall_ms is not None:
167        payload["anchor_wall_ms"] = int(anchor_wall_ms)
168    if sample_count is not None:
169        payload["sample_count"] = int(sample_count)
170    if wall_end_ms is not None:
171        payload["wall_end_ms"] = int(wall_end_ms)
172    if duration_ms is not None:
173        payload["duration_ms"] = int(duration_ms)
174    if audio_recording_uri:
175        payload["audio_recording_uri"] = audio_recording_uri
176    try:
177        async with httpx.AsyncClient(timeout=120.0) as client:
178            resp = await client.post(url, json=payload, headers=headers)
179        if resp.status_code >= 400:
180            logger.error(
181                "parlot: recording complete failed status=%s body=%s",
182                resp.status_code,
183                resp.text[:500],
184            )
185            return None
186        data = resp.json()
187        return data if isinstance(data, dict) else None
188    except Exception as exc:
189        logger.error("parlot: recording complete failed — %s", exc)
190        logger.debug("parlot: recording complete failed", exc_info=True)
191        return None

POST /v1/recordings/complete; return JSON body or None on failure.

async def put_bytes_to_presigned_url( put_url: str, body: bytes, *, content_type: str = 'audio/wav', timeout: float = 120.0) -> bool:
194async def put_bytes_to_presigned_url(
195    put_url: str,
196    body: bytes,
197    *,
198    content_type: str = "audio/wav",
199    timeout: float = 120.0,
200) -> bool:
201    """PUT object bytes to a presigned URL. Returns True on 2xx."""
202    try:
203        async with httpx.AsyncClient(timeout=timeout) as client:
204            resp = await client.put(
205                put_url,
206                content=body,
207                headers={"Content-Type": content_type},
208            )
209        if resp.status_code >= 400:
210            logger.error(
211                "parlot: presigned PUT failed status=%s body=%s",
212                resp.status_code,
213                resp.text[:300],
214            )
215            return False
216        return True
217    except Exception as exc:
218        logger.error("parlot: presigned PUT failed — %s", exc)
219        logger.debug("parlot: presigned PUT failed", exc_info=True)
220        return False

PUT object bytes to a presigned URL. Returns True on 2xx.

def should_capture_logs( agent_name: str, *, metadata_capture_logs: bool | None = None, capture_logs_config: bool | Sequence[str] | None = None, bootstrap_globs: Sequence[str] | None = None, bootstrap_agents: Mapping[str, bool] | None = None, bootstrap_present: bool = False) -> bool:
47def should_capture_logs(
48    agent_name: str,
49    *,
50    metadata_capture_logs: Optional[bool] = None,
51    capture_logs_config: CaptureLogsConfig = None,
52    bootstrap_globs: Optional[Sequence[str]] = None,
53    bootstrap_agents: Optional[Mapping[str, bool]] = None,
54    bootstrap_present: bool = False,
55) -> bool:
56    """Return True when application logs should be captured for this job.
57
58    Precedence: job metadata > ``parlotize(capture_logs=…)`` > bootstrap (UI).
59    When bootstrap is absent (``bootstrap_present=False``) or globs are unset,
60    default is **on**. Explicit empty globs means off.
61    """
62    if metadata_capture_logs is False:
63        return False
64    if metadata_capture_logs is True:
65        return True
66
67    if capture_logs_config is not None:
68        if isinstance(capture_logs_config, bool):
69            return capture_logs_config
70        return matches_allowlist(agent_name, list(capture_logs_config))
71
72    agents = bootstrap_agents or {}
73    if agent_name in agents:
74        return bool(agents[agent_name])
75
76    if not bootstrap_present:
77        return True
78
79    if bootstrap_globs is None:
80        return True
81
82    globs = list(bootstrap_globs)
83    if not globs:
84        return False
85    return matches_allowlist(agent_name, globs)

Return True when application logs should be captured for this job.

Precedence: job metadata > parlotize(capture_logs=…) > bootstrap (UI). When bootstrap is absent (bootstrap_present=False) or globs are unset, default is on. Explicit empty globs means off.

def stamp_framework_refs(span: Any, framework: str, **kinds: str) -> None:
28def stamp_framework_refs(span: Any, framework: str, **kinds: str) -> None:
29    """Stamp non-empty ``platform.ref.*`` kinds for ``framework``.
30
31    Convenience wrapper over :func:`stamp_platform_refs` for adapters that
32    build refs from keyword ids (e.g. ``conversation_id=``, ``room_sid=``).
33    """
34    fw = str(framework or "").strip()
35    if not fw:
36        return
37    triples = [
38        (fw, str(kind).strip(), str(value).strip())
39        for kind, value in kinds.items()
40        if str(kind or "").strip() and str(value or "").strip()
41    ]
42    if triples:
43        stamp_platform_refs(span, triples)

Stamp non-empty platform.ref.* kinds for framework.

Convenience wrapper over stamp_platform_refs() for adapters that build refs from keyword ids (e.g. conversation_id=, room_sid=).

def stamp_platform_refs(span: Any, refs: list[tuple[str, str, str]]) -> None:
46def stamp_platform_refs(
47    span: Any,
48    refs: list[tuple[str, str, str]],
49) -> None:
50    """Stamp ``platform.ref.*`` triples onto a span.
51
52    ``refs`` is a list of ``(framework, kind, value)`` tuples, e.g.
53    ``("livekit", "room_sid", "RM_abc")``. The first tuple is also written
54    to the canonical triple attributes (``platform.ref.framework/kind/value``)
55    so backends can pivot on a single primary ref.
56
57    Every tuple is also written as a flat ``platform.ref.{kind} = value`` key
58    for ingestion fallbacks.
59
60    Args:
61        span: A live OTel span (``set_attribute``) or a ``ReadableSpan`` with a
62            mutable ``_attributes`` dict.
63        refs: Non-empty list of reference triples.
64    """
65    if not refs:
66        return
67
68    def _write(key: str, value: str) -> None:
69        if hasattr(span, "set_attribute") and callable(span.set_attribute):
70            try:
71                span.set_attribute(key, value)
72                return
73            except Exception:
74                pass
75        try:
76            ParlotBaseProcessor._set(span, key, value)
77        except Exception:
78            return
79
80    fw, kind, val = refs[0]
81    _write(ATTR_PLATFORM_FRAMEWORK, fw)
82    _write(ATTR_PLATFORM_KIND, kind)
83    _write(ATTR_PLATFORM_VALUE, val)
84
85    for _fw_i, kind_i, val_i in refs:
86        _write(platform_ref_flat_key(kind_i), val_i)

Stamp platform.ref.* triples onto a span.

refs is a list of (framework, kind, value) tuples, e.g. ("livekit", "room_sid", "RM_abc"). The first tuple is also written to the canonical triple attributes (platform.ref.framework/kind/value) so backends can pivot on a single primary ref.

Every tuple is also written as a flat platform.ref.{kind} = value key for ingestion fallbacks.

Arguments:
  • span: A live OTel span (set_attribute) or a ReadableSpan with a mutable _attributes dict.
  • refs: Non-empty list of reference triples.
def stamp_session_sdk_version(session_span: Any, state: Any | None = None) -> None:
40def stamp_session_sdk_version(session_span: Any, state: Any | None = None) -> None:
41    """Stamp ``parlot.sdk.version`` and ``session.metadata.sdk_version`` on the session span and state."""
42    version = resolve_parlot_sdk_version()
43    if not version:
44        return
45
46    if isinstance(session_span, dict):
47        session_span[ATTR_PARLOT_SDK_VERSION] = version
48        session_span[ATTR_SESSION_METADATA_SDK_VERSION] = version
49    elif session_span is not None and hasattr(session_span, "set_attribute"):
50        session_span.set_attribute(ATTR_PARLOT_SDK_VERSION, version)
51        session_span.set_attribute(ATTR_SESSION_METADATA_SDK_VERSION, version)
52
53    if state is None:
54        try:
55            from parlot.core.session import get_active_session
56
57            state = get_active_session()
58        except Exception:
59            state = None
60
61    if state is not None and hasattr(state, "custom_metadata") and isinstance(state.custom_metadata, dict):
62        state.custom_metadata[ATTR_SESSION_METADATA_SDK_VERSION] = version

Stamp parlot.sdk.version and session.metadata.sdk_version on the session span and state.

def stamp_turn_utterance_text(span: Any, *, participant_role: str, utterance_text: str) -> None:
29def stamp_turn_utterance_text(
30    span: Any,
31    *,
32    participant_role: str,
33    utterance_text: str,
34) -> None:
35    """
36    Stamp ``turn.user_text`` or ``turn.agent_text`` on a turn root span.
37
38    Adapters call this after committing an utterance so the OTLP contract stays
39    consistent regardless of source.
40    """
41    text = (utterance_text or "").strip()
42    if not text:
43        return
44    role = (participant_role or "").strip().lower()
45    if role == "user":
46        span.set_attribute(ATTR_TURN_USER_TEXT, text)
47    elif role == "agent":
48        span.set_attribute(ATTR_TURN_AGENT_TEXT, text)

Stamp turn.user_text or turn.agent_text on a turn root span.

Adapters call this after committing an utterance so the OTLP contract stays consistent regardless of source.

def start_parlot_session_span( tracer: Any, *, session_id: str | None = None, conversation_id: str, stamp_fn: Callable[[Any], NoneType] | None = None, extra_attrs: Mapping[str, Any] | None = None) -> tuple[typing.Any, str]:
207def start_parlot_session_span(
208    tracer: Any,
209    *,
210    session_id: str | None = None,
211    conversation_id: str,
212    stamp_fn: Callable[[Any], None] | None = None,
213    extra_attrs: Mapping[str, Any] | None = None,
214) -> tuple[Any, str]:
215    """Start a ``parlot.session`` span and stamp identity.
216
217    Returns ``(span, session_id)``. Generates a session id when omitted.
218    """
219    sid = session_id or new_session_id()
220    span = tracer.start_span(SPAN_CONVERSATION_SESSION)
221    span.set_attribute(ATTR_SESSION_ID, sid)
222    span.set_attribute(ATTR_SESSION_CONVERSATION_ID, conversation_id)
223    span.set_attribute(ATTR_GEN_AI_CONVERSATION_ID, conversation_id)
224    if extra_attrs:
225        for key, value in extra_attrs.items():
226            if value is None or value == "":
227                continue
228            span.set_attribute(key, value)
229    if stamp_fn is not None:
230        stamp_fn(span)
231    return span, sid

Start a parlot.session span and stamp identity.

Returns (span, session_id). Generates a session id when omitted.

def try_set_global_tracer_provider(provider: Any) -> bool:
158def try_set_global_tracer_provider(provider: Any) -> bool:
159    """Set the global tracer provider, swallowing failures (already set, etc.)."""
160    from opentelemetry import trace
161
162    try:
163        trace.set_tracer_provider(provider)
164        return True
165    except Exception:
166        logger.debug("Could not set global tracer provider", exc_info=True)
167        return False

Set the global tracer provider, swallowing failures (already set, etc.).