@@ -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 ())
@@ -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
@@ -274,7 +273,7 @@ def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None
274
273
attributes .update (kwargs )
275
274
276
275
span_name = f"Tool: { tool ['name' ]} "
277
- return self ._start_span (span_name , parent_span , attributes )
276
+ return self ._start_span (span_name , parent_span , attributes , span_kind = trace_api . SpanKind . INTERNAL )
278
277
279
278
def end_tool_call_span (
280
279
self , span : Span , tool_result : Optional [ToolResult ], error : Optional [Exception ] = None
@@ -335,7 +334,7 @@ def start_event_loop_cycle_span(
335
334
attributes .update ({k : v for k , v in kwargs .items () if isinstance (v , (str , int , float , bool ))})
336
335
337
336
span_name = f"Cycle { event_loop_cycle_id } "
338
- return self ._start_span (span_name , parent_span , attributes )
337
+ return self ._start_span (span_name , parent_span , attributes , span_kind = trace_api . SpanKind . INTERNAL )
339
338
340
339
def end_event_loop_cycle_span (
341
340
self ,
@@ -405,7 +404,7 @@ def start_agent_span(
405
404
# Add additional kwargs as attributes
406
405
attributes .update ({k : v for k , v in kwargs .items () if isinstance (v , (str , int , float , bool ))})
407
406
408
- return self ._start_span (agent_name , attributes = attributes )
407
+ return self ._start_span (agent_name , attributes = attributes , span_kind = trace_api . SpanKind . CLIENT )
409
408
410
409
def end_agent_span (
411
410
self ,
0 commit comments