Sub topic 3
Attributes, events and status on a span
This chapter assumes you can create spans with start_as_current_span and know that a trace is a tree of spans. It covers the three ways a span carries information: attributes, events and status.
Attributes: facts about the whole span
An attribute is a key-value pair attached to a span. Use attributes for facts you will filter or group by later, such as the model name, the tool name or the token count.
Values must be a string, boolean, integer or float, or a list of one of those types. Nested dictionaries are not allowed. The SDK drops or rejects values it cannot represent, so flatten structures into separate keys.
from opentelemetry import trace
tracer = trace.get_tracer("support_agent")
with tracer.start_as_current_span("chat example-model") as span:
span.set_attribute("gen_ai.request.model", "example-model")
span.set_attribute("gen_ai.request.temperature", 0.2)
span.set_attributes({"app.user_tier": "free", "app.retry_count": 0})Attributes set at span creation are better than attributes added later. Samplers can only see the ones present at creation. Pass them with attributes={...} to start_as_current_span when you know them up front.
Events: things that happened during the span
An event is a named, timestamped record inside a span. Use events for moments, not for properties. Examples are "the first token arrived" or "a retry was scheduled". A span can hold many events, and each event can carry its own attributes.
import time
from opentelemetry import trace
tracer = trace.get_tracer("support_agent")
with tracer.start_as_current_span("chat example-model") as span:
span.add_event("request.sent")
time.sleep(0.05) # stand-in for waiting on the model
span.add_event("first_token", {"elapsed_ms": 50})record_exception is a special case. It adds an event named exception with the exception type, message and stack trace.
Status: did the operation succeed?
Status has three values: UNSET, OK and ERROR. A new span is UNSET, which means no error was reported. Set ERROR when the operation failed, with a short description. Leave successful spans as UNSET. Set OK only when you want to mark a span as explicitly successful, and note that this overrides any later attempt to set ERROR.
from opentelemetry.trace import Status, StatusCode
try:
result = 1 / 0
except Exception as exc:
span = trace.get_current_span()
span.record_exception(exc)
span.set_status(Status(StatusCode.ERROR, str(exc)))Choosing between them
| Use | When | Example |
|---|---|---|
| Attribute | A fact about the whole span | `gen_ai.request.model` |
| Event | A moment inside the span | `first_token` |
| Status | The final outcome | `ERROR` with a message |
Common mistakes
- Putting a large prompt in an attribute. Backends limit attribute size, and prompts may hold private data. Chapter 14 covers content capture.
- Using unbounded values, such as a user ID, as attribute keys. Keep keys fixed and put variable data in values.
- Reporting an error without setting status. An exception event alone does not make the span show as failed.
- Setting attributes after the span has ended. The call is ignored.
Exercise
A model call times out after a retry was scheduled. Which parts of the span record that the retry happened, and which part marks the call as failed?
Show answer
Record the retry as an event (for example retry.scheduled with an attempt number), because it is a moment inside the span. Mark the failure with status ERROR and a description. You can also call record_exception to add the exception event with its stack trace.
Exercise
Why is span.set_attribute("gen_ai.usage", {"input": 12, "output": 3}) a poor choice?
Show answer
Attribute values cannot be dictionaries. Use separate flat keys such as gen_ai.usage.input_tokens and gen_ai.usage.output_tokens.