@@ -77,9 +77,6 @@ class Tracer:
77
77
78
78
When the OTEL_EXPORTER_OTLP_ENDPOINT environment variable is set, traces
79
79
are sent to the OTLP endpoint.
80
-
81
- When the STRANDS_OTEL_ENABLE_CONSOLE_EXPORT environment variable is set,
82
- traces are printed to the console.
83
80
"""
84
81
85
82
def __init__ (
@@ -103,13 +100,15 @@ def _start_span(
103
100
span_name : str ,
104
101
parent_span : Optional [Span ] = None ,
105
102
attributes : Optional [Dict [str , AttributeValue ]] = None ,
103
+ span_kind : trace_api .SpanKind = trace_api .SpanKind .INTERNAL ,
106
104
) -> Optional [Span ]:
107
105
"""Generic helper method to start a span with common attributes.
108
106
109
107
Args:
110
108
span_name: Name of the span to create
111
109
parent_span: Optional parent span to link this span to
112
110
attributes: Dictionary of attributes to set on the span
111
+ span_kind: enum of OptenTelemetry SpanKind
113
112
114
113
Returns:
115
114
The created span, or None if tracing is not enabled
@@ -118,7 +117,7 @@ def _start_span(
118
117
return None
119
118
120
119
context = trace_api .set_span_in_context (parent_span ) if parent_span else None
121
- span = self .tracer .start_span (name = span_name , context = context )
120
+ span = self .tracer .start_span (name = span_name , context = context , kind = span_kind )
122
121
123
122
# Set start time as a common attribute
124
123
span .set_attribute ("gen_ai.event.start_time" , datetime .now (timezone .utc ).isoformat ())
@@ -219,7 +218,7 @@ def start_model_invoke_span(
219
218
"""
220
219
attributes : Dict [str , AttributeValue ] = {
221
220
"gen_ai.system" : "strands-agents" ,
222
- "agent. name" : agent_name ,
221
+ "gen_ai.operation. name" : "chat" ,
223
222
"gen_ai.agent.name" : agent_name ,
224
223
"gen_ai.prompt" : serialize (messages ),
225
224
}
@@ -230,7 +229,7 @@ def start_model_invoke_span(
230
229
# Add additional kwargs as attributes
231
230
attributes .update ({k : v for k , v in kwargs .items () if isinstance (v , (str , int , float , bool ))})
232
231
233
- return self ._start_span ("Model invoke" , parent_span , attributes )
232
+ return self ._start_span ("Model invoke" , parent_span , attributes , span_kind = trace_api . SpanKind . CLIENT )
234
233
235
234
def end_model_invoke_span (
236
235
self , span : Span , message : Message , usage : Usage , error : Optional [Exception ] = None
@@ -246,7 +245,9 @@ def end_model_invoke_span(
246
245
attributes : Dict [str , AttributeValue ] = {
247
246
"gen_ai.completion" : serialize (message ["content" ]),
248
247
"gen_ai.usage.prompt_tokens" : usage ["inputTokens" ],
248
+ "gen_ai.usage.input_tokens" : usage ["inputTokens" ],
249
249
"gen_ai.usage.completion_tokens" : usage ["outputTokens" ],
250
+ "gen_ai.usage.output_tokens" : usage ["outputTokens" ],
250
251
"gen_ai.usage.total_tokens" : usage ["totalTokens" ],
251
252
}
252
253
@@ -265,6 +266,7 @@ def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None
265
266
"""
266
267
attributes : Dict [str , AttributeValue ] = {
267
268
"gen_ai.prompt" : serialize (tool ),
269
+ "gen_ai.system" : "strands-agents" ,
268
270
"tool.name" : tool ["name" ],
269
271
"tool.id" : tool ["toolUseId" ],
270
272
"tool.parameters" : serialize (tool ["input" ]),
@@ -274,7 +276,7 @@ def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None
274
276
attributes .update (kwargs )
275
277
276
278
span_name = f"Tool: { tool ['name' ]} "
277
- return self ._start_span (span_name , parent_span , attributes )
279
+ return self ._start_span (span_name , parent_span , attributes , span_kind = trace_api . SpanKind . INTERNAL )
278
280
279
281
def end_tool_call_span (
280
282
self , span : Span , tool_result : Optional [ToolResult ], error : Optional [Exception ] = None
@@ -335,7 +337,7 @@ def start_event_loop_cycle_span(
335
337
attributes .update ({k : v for k , v in kwargs .items () if isinstance (v , (str , int , float , bool ))})
336
338
337
339
span_name = f"Cycle { event_loop_cycle_id } "
338
- return self ._start_span (span_name , parent_span , attributes )
340
+ return self ._start_span (span_name , parent_span , attributes , span_kind = trace_api . SpanKind . INTERNAL )
339
341
340
342
def end_event_loop_cycle_span (
341
343
self ,
@@ -405,7 +407,7 @@ def start_agent_span(
405
407
# Add additional kwargs as attributes
406
408
attributes .update ({k : v for k , v in kwargs .items () if isinstance (v , (str , int , float , bool ))})
407
409
408
- return self ._start_span (agent_name , attributes = attributes )
410
+ return self ._start_span (agent_name , attributes = attributes , span_kind = trace_api . SpanKind . CLIENT )
409
411
410
412
def end_agent_span (
411
413
self ,
@@ -436,6 +438,8 @@ def end_agent_span(
436
438
{
437
439
"gen_ai.usage.prompt_tokens" : accumulated_usage ["inputTokens" ],
438
440
"gen_ai.usage.completion_tokens" : accumulated_usage ["outputTokens" ],
441
+ "gen_ai.usage.input_tokens" : accumulated_usage ["inputTokens" ],
442
+ "gen_ai.usage.output_tokens" : accumulated_usage ["outputTokens" ],
439
443
"gen_ai.usage.total_tokens" : accumulated_usage ["totalTokens" ],
440
444
}
441
445
)
0 commit comments