Skip to content

Building AI agents, atomically

Make the manual OpenTelemetry setup in the observability guide runnable (#309)

Kenny Vaneetvelde812b9dad69e6
diff --git a/atomic-agents/tests/observability/test_openinference_tracing.py b/atomic-agents/tests/observability/test_openinference_tracing.py
index 8bb56c4..614ea26 100644
--- a/atomic-agents/tests/observability/test_openinference_tracing.py
+++ b/atomic-agents/tests/observability/test_openinference_tracing.py
@@ -12,7 +12,7 @@ from unittest.mock import Mock
import instructor
import pytest
-from openinference.instrumentation import TraceConfig, using_metadata, using_session
+from openinference.instrumentation import OITracer, TraceConfig, using_metadata, using_session
from openinference.semconv.trace import OpenInferenceSpanKindValues, SpanAttributes
from opentelemetry import trace as trace_api
from opentelemetry.sdk.trace import ReadableSpan, TracerProvider
@@ -280,6 +280,36 @@ def test_agent_span_nests_under_a_user_chain_span(exporter, provider_tracer):
assert turn_span.name == "turn"
+def test_manual_setup_from_the_guide_groups_agents_and_tools_under_one_chain_span():
+ exporter = InMemorySpanExporter()
+ tracer_provider = TracerProvider()
+ tracer_provider.add_span_processor(SimpleSpanProcessor(exporter))
+ trace_config = TraceConfig()
+ instrumentor = AtomicAgentsInstrumentor()
+ instrumentor.instrument(tracer_provider=tracer_provider, config=trace_config)
+ tracer = OITracer(tracer_provider.get_tracer(__name__), config=trace_config)
+ planner = make_agent(sync_client())
+ answerer = make_agent(sync_client())
+ calculator = CalculatorTool()
+ try:
+ with tracer.start_as_current_span("answer_question", openinference_span_kind="chain") as turn:
+ turn.set_input("what is 2 + 2?")
+ plan = planner.run(QuestionSchema(question="what is 2 + 2?"))
+ calculation = calculator.run(CalculationInputSchema(expression=plan.answer))
+ answer = answerer.run(QuestionSchema(question=str(calculation.result)))
+ turn.set_output(answer.answer)
+ finally:
+ instrumentor.uninstrument()
+
+ spans = spans_of(exporter)
+ chain_span = next(span for span in spans if kind_of(span) == OpenInferenceSpanKindValues.CHAIN.value)
+ children = [span for span in spans if span is not chain_span]
+ assert [kind_of(span) for span in children] == ["AGENT", "TOOL", "AGENT"]
+ assert all(span.parent.span_id == chain_span.get_span_context().span_id for span in children)
+ assert chain_span.attributes[SpanAttributes.INPUT_VALUE] == "what is 2 + 2?"
+ assert chain_span.attributes[SpanAttributes.OUTPUT_VALUE] == answer.answer
+
+
def test_llm_call_inside_run_becomes_child_of_agent_span(exporter, provider_tracer):
def create(**kwargs):
with provider_tracer.start_as_current_span("llm"):
diff --git a/docs/guides/observability.md b/docs/guides/observability.md
index 7ba76d3..41d1e14 100644
--- a/docs/guides/observability.md
+++ b/docs/guides/observability.md
@@ -32,10 +32,11 @@ pip install "atomic-agents[observability]" arize-phoenix-otel openinference-inst
The `observability` extra installs `openinference-instrumentation`, `openinference-semantic-conventions` and
`opentelemetry-api`. The Phoenix path below works without the extra because `arize-phoenix-otel` already depends on
-those packages; the manual path needs the extra plus an OpenTelemetry SDK and exporter:
+those packages; the manual path needs the extra plus an OpenTelemetry SDK, an exporter, and the instrumentor for
+your LLM client (the example below uses the OpenAI one):
```bash
-pip install "atomic-agents[observability]" opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
+pip install "atomic-agents[observability]" opentelemetry-sdk opentelemetry-exporter-otlp-proto-http openinference-instrumentation-openai
```
## Setup with Phoenix (automatic)
@@ -66,18 +67,26 @@ Two things to know about the automatic path:
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
-from openinference.instrumentation import TraceConfig
+from openinference.instrumentation import OITracer, TraceConfig
from openinference.instrumentation.openai import OpenAIInstrumentor
from atomic_agents.observability import AtomicAgentsInstrumentor
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter("http://localhost:6006/v1/traces")))
+trace_config = TraceConfig()
-AtomicAgentsInstrumentor().instrument(tracer_provider=tracer_provider, config=TraceConfig())
-OpenAIInstrumentor().instrument(tracer_provider=tracer_provider)
+AtomicAgentsInstrumentor().instrument(tracer_provider=tracer_provider, config=trace_config)
+OpenAIInstrumentor().instrument(tracer_provider=tracer_provider, config=trace_config)
+tracer = OITracer(tracer_provider.get_tracer(__name__), config=trace_config)
```
+The last line matters for the grouping section below: a plain SDK provider hands out a plain OpenTelemetry tracer,
+and the `openinference_span_kind` argument, `set_input` and `set_output` only exist on an OpenInference `OITracer`.
+Phoenix's `register()` returns a provider whose tracers already are `OITracer`s, so the automatic path skips this
+step. Pass the same `TraceConfig` to every layer; the config given to `instrument()` configures only the Atomic Agents
+spans.
+
`instrument()` is idempotent. Calling it again with a different provider or config logs a warning and keeps the first
installation; call `uninstrument()` first to change it. `uninstrument()` restores the original methods on
`AtomicAgent` and on every tool class.
@@ -88,8 +97,6 @@ Atomic Agents has no tool-calling loop: your code calls `agent.run(...)`, then `
Each of those is a root span, so without a parent they become separate traces. Open one parent span per unit of work:
```python
-tracer = tracer_provider.get_tracer(__name__)
-
with tracer.start_as_current_span("answer_question", openinference_span_kind="chain") as turn:
turn.set_input(question)
plan = planner.run(BasicChatInputSchema(chat_message=question))
@@ -98,8 +105,9 @@ with tracer.start_as_current_span("answer_question", openinference_span_kind="ch
turn.set_output(answer.chat_message)
```
-The [phoenix-tracing example](https://github.com/eigenwise/atomic-agents/tree/main/atomic-examples/phoenix-tracing)
-is exactly this flow.
+`tracer` is the `OITracer` from the manual setup, or `tracer_provider.get_tracer(__name__)` on the Phoenix path. The
+[phoenix-tracing example](https://github.com/eigenwise/atomic-agents/tree/main/atomic-examples/phoenix-tracing)
+is exactly this flow, and the test suite runs the manual setup plus this grouping offline with an in-memory exporter.
## Sessions, users and metadata
@@ -122,7 +130,11 @@ Agent and tool payloads are exported as `input.value` / `output.value`. To redac
`TraceConfig()` reads these variables.
- On the manual path, pass `TraceConfig(hide_inputs=True, hide_outputs=True)` to `instrument()`.
-Redacted values are exported as `__REDACTED__`. `atomic_agents.system_prompt.sha256` stays available either way. It
+Redacted values are exported as `__REDACTED__`. Masking covers the `input.value` and `output.value` attributes of
+Atomic Agents spans and nothing else: exception events and status descriptions (a validation error can quote input
+values), tool names and descriptions, and whatever you put in `using_metadata` are exported as they are. The LLM
+instrumentor and your own `OITracer` each take their own `TraceConfig`, so pass the same one to all three if you want
+the same masking everywhere. `atomic_agents.system_prompt.sha256` stays available either way. It
hashes the static sections of a `SystemPromptGenerator` (background, steps, output instructions) and ignores what
context providers add per call, so it identifies the prompt version even when inputs are hidden. Multimodal inputs (images, audio, PDFs) are serialized into
`input.value` in full; cap attribute size with `OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT` if that is too much.