Skip to content

credencestack.otel

OpenTelemetry for runs and agents. See Observability.

credencestack.otel

OpenTelemetry traces for CredenceStack runs (the [otel] extra).

Off by default. With --otel (or CREDENCESTACK_OTEL=1) every run emits a trace per scenario epoch, following the OpenTelemetry GenAI semantic conventions: a scenario span, an invoke_agent span, execute_tool and chat spans, findings, faults, control signals and approvals as span events, and a grade span. See docs/guides/telemetry/.

This module imports nothing from OpenTelemetry: the core package imports and runs without the extra. :func:start loads the OpenTelemetry modules and gives a clear error when the extra is missing.

TelemetryConfig dataclass

TelemetryConfig(
    enabled: bool = False,
    exporter: Exporter = "otlp",
    endpoint: str | None = None,
    protocol: Protocol = "http/protobuf",
    headers: dict[str, str] | None = None,
    file: Path | None = None,
    capture_content: bool = False,
    granularity: Granularity = "scenario",
    trace_url_template: str | None = None,
    agent_spans: AgentSpans = "nest",
    max_content_bytes: int = DEFAULT_MAX_CONTENT_BYTES,
    service_name: str | None = None,
    metrics: bool = False,
    sample_rate: float = 1.0,
    warnings: tuple[str, ...] = (),
)

Resolved telemetry settings for one command.

endpoint class-attribute instance-attribute

endpoint: str | None = None

Traces endpoint from --otel-endpoint (else the exporter reads OTEL_* itself).

headers class-attribute instance-attribute

headers: dict[str, str] | None = None

Headers from --otel-headers (else the exporter reads OTEL_* itself).

metrics class-attribute instance-attribute

metrics: bool = False

Also emit the registered metrics (--otel-metrics).

sample_rate class-attribute instance-attribute

sample_rate: float = 1.0

Share of environment episodes traced (make_env_batch lowers the default).

warnings class-attribute instance-attribute

warnings: tuple[str, ...] = ()

Messages to print once (headers on the command line, content capture on).

with_

with_(**changes: object) -> TelemetryConfig

A copy with changes applied.

TelemetryConfigError

TelemetryConfigError(
    message: str,
    *,
    hint: str | None = None,
    docs: str | None = None,
)

Bases: ConfigError, ValueError

A telemetry setting is invalid (the message says which and why).

TelemetryNotInstalled

TelemetryNotInstalled(
    message: str,
    *,
    hint: str | None = None,
    docs: str | None = None,
)

Bases: DependencyError

Telemetry was enabled but the [otel] extra is not installed.

active

active() -> Telemetry | None

The telemetry of the current command, or None when it is off.

available

available() -> bool

True if the OpenTelemetry SDK can be imported (without importing it).

otlp_hint

otlp_hint(
    config: TelemetryConfig,
    environ: Mapping[str, str] | None = None,
) -> str | None

A one-line hint when OTLP variables are set but telemetry is off, else None.

propagation_env

propagation_env() -> dict[str, str]

TRACEPARENT/TRACESTATE for a launched agent process ({} when off).

propagation_headers

propagation_headers() -> dict[str, str]

traceparent/tracestate headers for an outgoing agent request ({} when off).

resolve

resolve(
    settings: OtelSettings | None = None,
    *,
    environ: Mapping[str, str] | None = None,
    max_content_bytes: int | None = None,
) -> TelemetryConfig

The telemetry configuration of one command, from its resolved [otel] settings.

Raises:

Type Description
TelemetryConfigError

if a value is invalid.

start

start(
    config: TelemetryConfig,
    *,
    run_id: str | None = None,
    out: IO[str] | None = None,
    simple: bool = False,
    exporter: Any = None,
    install_global: bool = True,
    on_failure: Callable[[str], None] | None = None,
) -> Telemetry

Start telemetry for this process (the CLI calls it once per command).

Raises:

Type Description
TelemetryNotInstalled

if the [otel] extra is missing.

stop

stop(tel: Telemetry | None = None) -> str | None

Shut down tel (default: the active one); return its summary line.

suspended

suspended() -> Generator[None]

No telemetry in this context, even if a command-wide one is active.

use

use(tel: Telemetry) -> Generator[Telemetry]

Make tel the active telemetry in this context (tests, the conformance check).