Overview
propagate_attributes() is a context manager that attaches attributes to all spans within its scope — including spans created by auto-instrumentation and instrumentation plugins. Uses contextvars for async safety.
Parameters
All parameters are keyword-only:| Parameter | Type | Description |
|---|---|---|
customer_identifier | str | User/customer identifier. Maps to respan.customer_params.customer_identifier. |
customer_email | str | Customer email. Maps to respan.customer_params.customer_email. |
customer_name | str | Customer display name. Maps to respan.customer_params.customer_name. |
thread_identifier | str | Conversation thread ID. Maps to respan.threads.thread_identifier. |
custom_identifier | str | Indexed custom identifier. Maps to respan.custom_identifier. |
group_identifier | str | Group related traces. Maps to respan.trace_group.group_identifier. |
environment | str | Environment name. Maps to respan.environment. |
metadata | dict | Custom key-value pairs. Each key maps to respan.metadata.<key>. Nested calls merge metadata. |
prompt | dict | Prompt config with prompt_id and variables. Stored as JSON for server-side template resolution. |
Examples
Per-request user and thread tracking
Runner.run() — the trace, agent, LLM response, tool calls — will carry the customer_identifier, thread_identifier, and metadata.
Prompt logging
Use theprompt parameter for server-side prompt template resolution:
With direct OpenAI SDK
Nested contexts
Nested calls merge attributes. Inner values override outer values for the same key. Metadata dicts are merged (not replaced).How it works
- Attributes are stored in a
ContextVar(_PROPAGATED_ATTRIBUTES). RespanSpanProcessor.on_start()reads theContextVarand merges attributes onto every new span.build_readable_span()also reads theContextVarand merges into plugin-created spans.- On context exit, the
ContextVaris reset to the previous value.
asyncio — each task gets its own copy of the context.
vs respan_span_attributes()
| Feature | propagate_attributes() | respan_span_attributes() |
|---|---|---|
| Scope | All spans in context (including auto-instrumented) | Current active span only |
| Async safe | Yes (ContextVar-based) | Yes |
| Nested merging | Yes (metadata merges) | Yes (inner overrides outer) |
| Works with plugins | Yes | Only for decorator-created spans |
| Prompt logging | Yes | No |
propagate_attributes() in most cases. Use respan_span_attributes() only when you need to set attributes on a specific decorator-created span.