Skip to content

API Reference parrot.observability.setup

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.observability.setup

View source

setup_telemetry() and shutdown_telemetry() — one-call observability boot helpers.

FEAT-177 TASK-1235.

setup_telemetry(config) is the single public entrypoint that:

  1. Builds an OTel Resource (service.name, service.version, service.instance.id, parrot.version).
  2. Configures a TracerProvider with one BatchSpanProcessor per OTLP target (FEAT-462 — config.otlp_targets, or a single implicit target wrapping the legacy config.otlp_endpoint) + TraceIdRatioBased sampler, and registers it globally.
  3. Configures a MeterProvider with one PeriodicExportingMetricReader per OTLP target (FEAT-705) + histogram View objects for LLM-optimised bucket boundaries, and registers it globally.
  4. Optionally builds a CostCalculator (respects PARROT_PRICING_PATH env var).
  5. Constructs GenAIOpenTelemetrySubscriber and/or MetricsSubscriber.
  6. Bundles them into ParrotTelemetryProvider and calls get_global_registry().add_provider(provider).

Idempotent: same config → same provider returned; different config → ConfigurationError. config.enabled=False → immediate None return with zero OTel SDK imports.

Spec §3 Module 8, §2 Initialization flow.

Alias ConfigurationError

Re-exported from parrot.observability.errors.ConfigurationError.

Alias ObservabilityConfig

Re-exported from parrot.observability.config.ObservabilityConfig.

Alias ParrotTelemetryProvider

Re-exported from parrot.observability.provider.ParrotTelemetryProvider.

Function drain_lifecycle_events

Await all in-flight lifecycle forwarding tasks.

Short-lived scripts or lambda-style callers should await drain_lifecycle_events() before shutdown_telemetry() to ensure fire-and-forget events (from _emit_before_call's emit_nowait / forward_to_global) have reached their subscribers.

This is no longer strictly required for _emit_after_call / _emit_failed_call events (those are now awaited directly), but it still drains any BeforeClientCallEvent tasks scheduled via emit_nowait / create_task.

Idempotent and safe to call when no loop is running (returns immediately in that case).

Example::

result = await client.ask("hello", model="gpt-4o")
await drain_lifecycle_events()
shutdown_telemetry()

drain_lifecycle_events() -> None

Attribute logger

Function setup_telemetry

Configure OpenTelemetry + cost observability and wire to the global registry.

setup_telemetry(config: ObservabilityConfig) -> Optional[ParrotTelemetryProvider]

Parameter Type Default Description
config ObservabilityConfig ObservabilityConfig controlling every aspect of the stack.

Returns: Optional[ParrotTelemetryProvider] — The ParrotTelemetryProvider registered with the global event registry, or None when config.enabled is False.

Raises:

  • ConfigurationError — When called a second time with a different config than the first call (hash mismatch). Also raised if a SimpleSpanProcessor is detected in the constructed TracerProvider's processor chain.

Function shutdown_telemetry

Flush all exporters and clear the setup state. Idempotent.

Calls TracerProvider.shutdown() (which flushes BatchSpanProcessor) and MeterProvider.shutdown() (which flushes PeriodicExportingMetricReader). Then unregisters all subscriptions from the global event registry and clears the module-level cache so setup_telemetry can be called again.

This function is safe to call when setup_telemetry was never called, or after a previous shutdown_telemetry — it is fully idempotent.

shutdown_telemetry() -> None

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally