adk_observability (erlang_adk v0.7.0)

View Source

Correlated telemetry and safe structured event/log envelopes.

The module has no mandatory OpenTelemetry dependency. It emits ordinary telemetry events carrying stable correlation metadata and can deliver the same JSON-safe envelope to optional bounded exporters. Prompt, response, tool arguments, and other content are omitted unless capture is explicitly enabled; authentication material is pruned in either mode.

Summary

Functions

Create a correlated child span context.

Decode a persisted envelope. Unknown schema versions are rejected.

Deliver through either the compatibility synchronous path or the bounded supervised asynchronous bus.

Emit a connected telemetry event and return its structured envelope.

Verify and return the canonical JSON-safe envelope.

Deliver one already-safe envelope to bounded exporters in list order.

Finish an operation span and update bounded low-cardinality metrics.

Continue a valid inbound W3C trace with a fresh local span.

Inject this context into outbound HTTP-style headers.

Build a root correlation context.

Surface an exporter failure which happened after an operation had already completed. Only structural tags and correlation IDs are emitted; the untrusted exporter reason is never forwarded. This diagnostic is deliberately separate from the operation result so callers cannot replay a successful model/tool side effect merely because span delivery failed.

Start a real operation span at the execution boundary.

Validate exporter descriptors at Runner construction time. Callback execution still revalidates the immutable descriptors so a forged Runner term cannot bypass the boundary.

Types

context/0

-type context() :: map().

envelope/0

-type envelope() :: map().

Functions

child_context(Parent, Attrs)

-spec child_context(context(), map()) -> {ok, context()} | {error, term()}.

Create a correlated child span context.

Trace/run/session identity is inherited. The parent's span ID becomes the child parent ID. Additional agent/model/tool/call fields may be overridden.

decode(Envelope)

-spec decode(map()) -> {ok, envelope()} | {error, term()}.

Decode a persisted envelope. Unknown schema versions are rejected.

deliver(Envelope, Config)

-spec deliver(envelope(), map()) -> {ok, term()} | {error, term()}.

Deliver through either the compatibility synchronous path or the bounded supervised asynchronous bus.

Async admission is explicit: a full queue is returned to the caller and is also visible in bus counters. failure_policy controls whether admission failure is reported (closed) or converted into a structural dropped result (open).

emit(EventName, Measurements, Context)

-spec emit([atom(), ...], map(), context()) -> {ok, envelope()} | {error, term()}.

emit(EventName, Measurements0, Context0, Opts)

-spec emit([atom(), ...], map(), context(), map()) -> {ok, envelope()} | {error, term()}.

Emit a connected telemetry event and return its structured envelope.

Options are attributes, capture_content (default false), and content. Even when capture is enabled, secret-bearing keys are removed before either telemetry handlers or exporters can see them.

encode(Envelope)

-spec encode(envelope()) -> {ok, envelope()} | {error, term()}.

Verify and return the canonical JSON-safe envelope.

export(Envelope0, Exporters)

-spec export(envelope(), [map()]) -> {ok, [map()]} | {error, term()} | {error, term(), [map()]}.

Deliver one already-safe envelope to bounded exporters in list order.

Exporter descriptors use id, module, optional config, timeout_ms, max_heap_words, and failure_policy (open by default). Fail-open exporters report failure and allow later exporters to run. Fail-closed stops immediately with a typed error.

finish_span(_, Status, Details)

-spec finish_span(map(), term(), map()) -> {ok, map()} | {error, term()}.

Finish an operation span and update bounded low-cardinality metrics.

from_headers(Headers, Attrs)

-spec from_headers(map() | list(), map()) -> {ok, context()} | {error, term()} | not_found.

Continue a valid inbound W3C trace with a fresh local span.

This function validates wire syntax but deliberately does not decide whether the remote sampling bit should be trusted. Network boundaries may clear trace_flags or start a new trace before calling it.

inject_headers(Context, Headers)

-spec inject_headers(context(), map() | list()) -> {ok, map() | list()} | {error, term()}.

Inject this context into outbound HTTP-style headers.

new_context()

-spec new_context() -> {ok, context()}.

new_context(Attrs)

-spec new_context(map()) -> {ok, context()} | {error, term()}.

Build a root correlation context.

Known identity fields are copied explicitly; unknown safe fields are placed below attributes. Binary input is never converted into an atom.

report_delivery_failure(Phase, Reason, Span)

-spec report_delivery_failure(atom(), term(), map()) -> ok.

Surface an exporter failure which happened after an operation had already completed. Only structural tags and correlation IDs are emitted; the untrusted exporter reason is never forwarded. This diagnostic is deliberately separate from the operation result so callers cannot replay a successful model/tool side effect merely because span delivery failed.

schema_version()

-spec schema_version() -> pos_integer().

start_span(Operation, Kind, Parent, Details, Delivery)

-spec start_span(atom() | binary(), atom() | binary(), context(), map(), map()) ->
                    {ok, map()} | {error, term()}.

Start a real operation span at the execution boundary.

Details is mapped through the pinned metadata-only GenAI convention; it cannot inject prompt/output content. The returned handle carries monotonic timing and must be finished exactly once by the operation owner.

validate_exporters(Exporters)

-spec validate_exporters([map()]) -> ok | {error, term()}.

Validate exporter descriptors at Runner construction time. Callback execution still revalidates the immutable descriptors so a forged Runner term cannot bypass the boundary.