respan_span_attributes()

Overview

respan_span_attributes() is a context manager that attaches Respan-specific attributes to all spans created within its scope. Use it to set customer_identifier, trace_group_identifier, and metadata without manually calling update_current_span() on each span.

1from respan import respan_span_attributes

Supported attributes

KeyTypeDescription
customer_identifierstrUser or customer identifier for filtering in the dashboard.
trace_group_identifierstrGroup related traces together.
metadataDictCustom key-value pairs for analytics.

Example

1from respan import Respan, workflow, task, respan_span_attributes
2
3respan = Respan(api_key="your-api-key")
4
5@task(name="process_data")
6def process_data():
7 return "processed"
8
9@workflow(name="user_pipeline")
10def user_pipeline(user_id: str):
11 with respan_span_attributes({
12 "customer_identifier": user_id,
13 "trace_group_identifier": "experiment-v2",
14 "metadata": {"team": "search", "priority": "high"},
15 }):
16 return process_data()
17
18user_pipeline("user-456")

All spans created inside the with block (including process_data) will have the Respan attributes attached.

Nested contexts

Attributes from inner contexts override outer contexts for the same keys.

1@workflow(name="nested_example")
2def nested_example():
3 with respan_span_attributes({"customer_identifier": "user-A"}):
4 # Spans here have customer_identifier="user-A"
5
6 with respan_span_attributes({"customer_identifier": "user-B"}):
7 # Spans here have customer_identifier="user-B"
8 pass