Skip to main content

Crate klieo_otel

Crate klieo_otel 

Source
Expand description

klieo-otel — OpenTelemetry OTLP exporter wiring for klieo agent runtimes.

The klieo runtime is instrumented with tracing spans on every Agent::run_steps call, every LLM invocation, and every tool dispatch. Field names follow the OpenTelemetry GenAI semantic conventions, so any OTel-aware backend (Tempo, Jaeger, Honeycomb, Datadog, …) ingests them once you wire up an exporter.

This crate is that exporter. One function call:

use klieo_otel::{install, OtelConfig, Protocol};
use std::time::Duration;

let _guard = install(
    OtelConfig::new("my-agent")
        .service_version(env!("CARGO_PKG_VERSION"))
        .gen_ai_system("ollama")
        .endpoint("http://localhost:4317")
        .protocol(Protocol::Grpc)
        .timeout(Duration::from_secs(5)),
)?;
// run agents here — spans flow to the configured collector
// until `_guard` is dropped.

§Defaults

  • EndpointOTEL_EXPORTER_OTLP_ENDPOINT env var (the standard OTel SDK convention) is honoured if set; otherwise the value passed to OtelConfig::endpoint is used; otherwise http://localhost:4317.
  • Protocol — gRPC (Protocol::Grpc). Switch with OtelConfig::protocol.
  • Resource attributesservice.name, service.version, gen_ai.system are emitted on every span, per OTel semantic conventions.

§Guard semantics

OtelGuard’s Drop impl force-flushes any in-flight batch and shuts the provider down cleanly. Bind it to a binding that lives for the lifetime of your process (e.g. in main):

let _otel = install(OtelConfig::new("my-agent"))?;
// … run server … the guard drops at end of main, flushing spans.

Prefix the binding with _ (not _otel = … then unused) to suppress the unused-variable warning while keeping the binding alive — let _ = … would drop the guard immediately and discard every export.

§Composition with existing subscribers

install sets a global tracing subscriber that combines:

  • an env-filter layer respecting RUST_LOG,
  • the OpenTelemetry layer that exports spans over OTLP,
  • a human-readable fmt layer on stderr.

If you already call tracing_subscriber::fmt().init() (or any other subscriber installer) earlier in main, install will return OtelError::AlreadyInitialized. Call this crate first (or skip the other call) to avoid a double-install.

Modules§

test_utils
Test utilities for asserting against emitted tracing spans without an OTLP collector.

Structs§

OtelConfig
Configuration for crate::install.
OtelGuard
Guard that flushes and shuts down the OTel tracer provider on Drop.

Enums§

OtelError
Failure modes for crate::install.
Protocol
OTLP wire protocol selector.

Constants§

OTEL_EXPORTER_OTLP_ENDPOINT_ENV
Standard OTel SDK environment variable: when set, overrides OtelConfig::endpoint.

Traits§

OpenTelemetrySpanExt
Utility functions to allow tracing Spans to accept and return OpenTelemetry Contexts.

Functions§

install
Install a global OpenTelemetry tracing subscriber.
install_or_replace
Install klieo’s OpenTelemetry exporter, tolerating an already-installed tracing subscriber.