Skip to content

Commit 856cb51

Browse files
style(otel/distributed_tracing): ruff format for CI lint
The reconciled pr-test.yml lint job runs `ruff format --check` with ruff 0.15.20, which reflows two pre-existing files in this folder. Apply the formatting so the lint job passes. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 563ab58 commit 856cb51

2 files changed

Lines changed: 146 additions & 85 deletions

File tree

integrations/otel/distributed_tracing/option_a_explicit_ids.py

Lines changed: 92 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ def llm_attrs(model: str, prompt, completion, usage: dict) -> dict:
144144
`usage` keys map under gen_ai.usage.* (input_tokens, output_tokens, etc.).
145145
"""
146146
attrs = {
147-
"gen_ai.system": "openai", # -> provider
148-
"gen_ai.request.model": model, # -> model, forces span type = llm
149-
"input": prompt, # -> input
150-
"output": completion, # -> output
147+
"gen_ai.system": "openai", # -> provider
148+
"gen_ai.request.model": model, # -> model, forces span type = llm
149+
"input": prompt, # -> input
150+
"output": completion, # -> output
151151
}
152152
for token_type, count in usage.items():
153153
attrs[f"gen_ai.usage.{token_type}"] = count
@@ -167,32 +167,50 @@ def backend_request_1(session_id: str, user_prompt: str) -> dict:
167167
then dispatches it to the UI/external service (out of process).
168168
"""
169169
trace_id = new_id()
170-
s_orchestrator = new_id() # root span (closes last, in finalize())
170+
s_orchestrator = new_id() # root span (closes last, in finalize())
171171
s_routing = new_id()
172172
s_agent = new_id()
173173
s_llm1 = new_id()
174-
s_dispatch = new_id() # tool_dispatch: tool result must nest UNDER this
175-
176-
emit_span("routing.invoke", trace_id=trace_id, span_id=s_routing,
177-
parent_span_id=s_orchestrator, duration_s=0.1)
178-
emit_span("routing.generic_agent", trace_id=trace_id, span_id=s_agent,
179-
parent_span_id=s_routing, duration_s=0.1)
180-
emit_span("llm.request", trace_id=trace_id, span_id=s_llm1, parent_span_id=s_agent,
181-
duration_s=0.3,
182-
attributes=llm_attrs(
183-
model="gpt-4o",
184-
prompt=[{"role": "user", "content": user_prompt}],
185-
completion=[{"role": "assistant", "tool_calls": [
186-
{"id": "call_1", "name": "web_search", "arguments": {"query": user_prompt}}]}],
187-
usage={"input_tokens": 412, "output_tokens": 37},
188-
))
189-
emit_span("tool_dispatch", trace_id=trace_id, span_id=s_dispatch,
190-
parent_span_id=s_llm1, duration_s=0.02,
191-
attributes={
192-
"gen_ai.tool.name": "web_search",
193-
"gen_ai.tool.call.id": "call_1",
194-
"input": {"query": user_prompt},
195-
})
174+
s_dispatch = new_id() # tool_dispatch: tool result must nest UNDER this
175+
176+
emit_span(
177+
"routing.invoke", trace_id=trace_id, span_id=s_routing, parent_span_id=s_orchestrator, duration_s=0.1
178+
)
179+
emit_span(
180+
"routing.generic_agent", trace_id=trace_id, span_id=s_agent, parent_span_id=s_routing, duration_s=0.1
181+
)
182+
emit_span(
183+
"llm.request",
184+
trace_id=trace_id,
185+
span_id=s_llm1,
186+
parent_span_id=s_agent,
187+
duration_s=0.3,
188+
attributes=llm_attrs(
189+
model="gpt-4o",
190+
prompt=[{"role": "user", "content": user_prompt}],
191+
completion=[
192+
{
193+
"role": "assistant",
194+
"tool_calls": [
195+
{"id": "call_1", "name": "web_search", "arguments": {"query": user_prompt}}
196+
],
197+
}
198+
],
199+
usage={"input_tokens": 412, "output_tokens": 37},
200+
),
201+
)
202+
emit_span(
203+
"tool_dispatch",
204+
trace_id=trace_id,
205+
span_id=s_dispatch,
206+
parent_span_id=s_llm1,
207+
duration_s=0.02,
208+
attributes={
209+
"gen_ai.tool.name": "web_search",
210+
"gen_ai.tool.call.id": "call_1",
211+
"input": {"query": user_prompt},
212+
},
213+
)
196214

197215
# Persist the two ids the second request needs to stitch the trace correctly.
198216
SESSION_STORE[session_id] = {
@@ -221,28 +239,42 @@ def backend_request_2(session_id: str, tool_result: dict) -> str:
221239

222240
# THE KEY LINE: parent is the dispatch span id from request 1 — no longer a
223241
# disconnected root.
224-
emit_span("tool_execution", trace_id=trace_id, span_id=s_tool,
225-
parent_span_id=ctx["dispatch_span_id"], duration_s=0.2,
226-
attributes={
227-
"gen_ai.tool.name": "web_search",
228-
"gen_ai.tool.call.id": "call_1",
229-
"input": ctx["user_prompt"],
230-
"output": tool_result,
231-
})
232-
emit_span("routing.invoke", trace_id=trace_id, span_id=s_routing2,
233-
parent_span_id=s_tool, duration_s=0.1)
234-
emit_span("routing.generic_agent", trace_id=trace_id, span_id=s_agent2,
235-
parent_span_id=s_routing2, duration_s=0.1)
242+
emit_span(
243+
"tool_execution",
244+
trace_id=trace_id,
245+
span_id=s_tool,
246+
parent_span_id=ctx["dispatch_span_id"],
247+
duration_s=0.2,
248+
attributes={
249+
"gen_ai.tool.name": "web_search",
250+
"gen_ai.tool.call.id": "call_1",
251+
"input": ctx["user_prompt"],
252+
"output": tool_result,
253+
},
254+
)
255+
emit_span("routing.invoke", trace_id=trace_id, span_id=s_routing2, parent_span_id=s_tool, duration_s=0.1)
256+
emit_span(
257+
"routing.generic_agent",
258+
trace_id=trace_id,
259+
span_id=s_agent2,
260+
parent_span_id=s_routing2,
261+
duration_s=0.1,
262+
)
236263

237264
final_answer = "The capital of France is Paris."
238-
emit_span("llm.request", trace_id=trace_id, span_id=s_llm2, parent_span_id=s_agent2,
239-
duration_s=0.3,
240-
attributes=llm_attrs(
241-
model="gpt-4o",
242-
prompt=[{"role": "tool", "content": tool_result}],
243-
completion=[{"role": "assistant", "content": final_answer}],
244-
usage={"input_tokens": 690, "output_tokens": 122},
245-
))
265+
emit_span(
266+
"llm.request",
267+
trace_id=trace_id,
268+
span_id=s_llm2,
269+
parent_span_id=s_agent2,
270+
duration_s=0.3,
271+
attributes=llm_attrs(
272+
model="gpt-4o",
273+
prompt=[{"role": "tool", "content": tool_result}],
274+
completion=[{"role": "assistant", "content": final_answer}],
275+
usage={"input_tokens": 690, "output_tokens": 122},
276+
),
277+
)
246278

247279
ctx["final_answer"] = final_answer
248280
return final_answer
@@ -254,14 +286,19 @@ def finalize(session_id: str) -> None:
254286
thread_id groups the trace into a conversation thread in Opik.
255287
"""
256288
ctx = SESSION_STORE[session_id]
257-
emit_span("orchestrator_request", trace_id=ctx["trace_id"],
258-
span_id=ctx["orchestrator_span_id"], parent_span_id=None, duration_s=0.05,
259-
attributes={
260-
"thread_id": session_id, # -> Opik thread grouping
261-
"input": ctx["user_prompt"], # -> trace input
262-
"output": ctx["final_answer"], # -> trace output
263-
"opik.tags": ["tool-call", "distributed", "demo"],
264-
})
289+
emit_span(
290+
"orchestrator_request",
291+
trace_id=ctx["trace_id"],
292+
span_id=ctx["orchestrator_span_id"],
293+
parent_span_id=None,
294+
duration_s=0.05,
295+
attributes={
296+
"thread_id": session_id, # -> Opik thread grouping
297+
"input": ctx["user_prompt"], # -> trace input
298+
"output": ctx["final_answer"], # -> trace output
299+
"opik.tags": ["tool-call", "distributed", "demo"],
300+
},
301+
)
265302

266303

267304
def print_tree() -> None:

integrations/otel/distributed_tracing/option_b_w3c_propagation.py

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@
9090
def set_attrs(span, attributes: dict) -> None:
9191
"""OTel attribute values must be primitives; JSON-encode structured payloads."""
9292
span.set_attributes(
93-
{k: (v if isinstance(v, (str, bool, int, float)) else json.dumps(v))
94-
for k, v in attributes.items()}
93+
{k: (v if isinstance(v, (str, bool, int, float)) else json.dumps(v)) for k, v in attributes.items()}
9594
)
9695

9796

@@ -130,28 +129,47 @@ def backend_request_1(session_id: str, user_prompt: str) -> dict:
130129
with tracer.start_as_current_span("routing.generic_agent"):
131130
time.sleep(0.05)
132131
with tracer.start_as_current_span("llm.request") as llm1:
133-
set_attrs(llm1, llm_attrs(
134-
model="gpt-4o",
135-
prompt=[{"role": "user", "content": user_prompt}],
136-
completion=[{"role": "assistant", "tool_calls": [
137-
{"id": "call_1", "name": "web_search", "arguments": {"query": user_prompt}}]}],
138-
usage={"input_tokens": 412, "output_tokens": 37},
139-
))
132+
set_attrs(
133+
llm1,
134+
llm_attrs(
135+
model="gpt-4o",
136+
prompt=[{"role": "user", "content": user_prompt}],
137+
completion=[
138+
{
139+
"role": "assistant",
140+
"tool_calls": [
141+
{
142+
"id": "call_1",
143+
"name": "web_search",
144+
"arguments": {"query": user_prompt},
145+
}
146+
],
147+
}
148+
],
149+
usage={"input_tokens": 412, "output_tokens": 37},
150+
),
151+
)
140152
time.sleep(0.2)
141153
# Inject WHILE tool_dispatch is the current span so the carrier
142154
# encodes it as the parent for the tool execution in request 2.
143155
with tracer.start_as_current_span("tool_dispatch") as dispatch:
144-
set_attrs(dispatch, {
145-
"gen_ai.tool.name": "web_search",
146-
"gen_ai.tool.call.id": "call_1",
147-
"input": {"query": user_prompt},
148-
})
156+
set_attrs(
157+
dispatch,
158+
{
159+
"gen_ai.tool.name": "web_search",
160+
"gen_ai.tool.call.id": "call_1",
161+
"input": {"query": user_prompt},
162+
},
163+
)
149164
inject(carrier) # -> carrier["traceparent"] = 00-<trace>-<dispatch span>-01
150165

151-
SESSION_STORE[session_id] = {"orchestrator": orchestrator, "carrier": carrier,
152-
"user_prompt": user_prompt}
153-
return {"tool": "web_search", "args": {"query": user_prompt},
154-
"session_id": session_id, "traceparent": carrier.get("traceparent")}
166+
SESSION_STORE[session_id] = {"orchestrator": orchestrator, "carrier": carrier, "user_prompt": user_prompt}
167+
return {
168+
"tool": "web_search",
169+
"args": {"query": user_prompt},
170+
"session_id": session_id,
171+
"traceparent": carrier.get("traceparent"),
172+
}
155173

156174

157175
def backend_request_2(session_id: str, tool_result: dict) -> str:
@@ -163,25 +181,31 @@ def backend_request_2(session_id: str, tool_result: dict) -> str:
163181
# THE KEY LINE: context=parent_ctx -> tool_execution becomes a child of
164182
# tool_dispatch, sharing the same trace id.
165183
with tracer.start_as_current_span("tool_execution", context=parent_ctx) as tool:
166-
set_attrs(tool, {
167-
"gen_ai.tool.name": "web_search",
168-
"gen_ai.tool.call.id": "call_1",
169-
"input": ctx_data["user_prompt"],
170-
"output": tool_result,
171-
})
184+
set_attrs(
185+
tool,
186+
{
187+
"gen_ai.tool.name": "web_search",
188+
"gen_ai.tool.call.id": "call_1",
189+
"input": ctx_data["user_prompt"],
190+
"output": tool_result,
191+
},
192+
)
172193
time.sleep(0.1)
173194
with tracer.start_as_current_span("routing.invoke"):
174195
time.sleep(0.05)
175196
with tracer.start_as_current_span("routing.generic_agent"):
176197
time.sleep(0.05)
177198
final_answer = "The capital of France is Paris."
178199
with tracer.start_as_current_span("llm.request") as llm2:
179-
set_attrs(llm2, llm_attrs(
180-
model="gpt-4o",
181-
prompt=[{"role": "tool", "content": tool_result}],
182-
completion=[{"role": "assistant", "content": final_answer}],
183-
usage={"input_tokens": 690, "output_tokens": 122},
184-
))
200+
set_attrs(
201+
llm2,
202+
llm_attrs(
203+
model="gpt-4o",
204+
prompt=[{"role": "tool", "content": tool_result}],
205+
completion=[{"role": "assistant", "content": final_answer}],
206+
usage={"input_tokens": 690, "output_tokens": 122},
207+
),
208+
)
185209
time.sleep(0.2)
186210

187211
ctx_data["final_answer"] = final_answer

0 commit comments

Comments
 (0)