Skip to content

Commit 4f2d6b4

Browse files
committed
chore: update spanKind
1 parent 6a1ccea commit 4f2d6b4

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/strands/telemetry/tracer.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ def _start_span(
103103
span_name: str,
104104
parent_span: Optional[Span] = None,
105105
attributes: Optional[Dict[str, AttributeValue]] = None,
106+
span_kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
106107
) -> Optional[Span]:
107108
"""Generic helper method to start a span with common attributes.
108109
109110
Args:
110111
span_name: Name of the span to create
111112
parent_span: Optional parent span to link this span to
112113
attributes: Dictionary of attributes to set on the span
114+
span_kind: enum of OptenTelemetry SpanKind
113115
114116
Returns:
115117
The created span, or None if tracing is not enabled
@@ -118,7 +120,7 @@ def _start_span(
118120
return None
119121

120122
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)
123+
span = self.tracer.start_span(name=span_name, context=context, kind=span_kind)
122124

123125
# Set start time as a common attribute
124126
span.set_attribute("gen_ai.event.start_time", datetime.now(timezone.utc).isoformat())
@@ -230,7 +232,7 @@ def start_model_invoke_span(
230232
# Add additional kwargs as attributes
231233
attributes.update({k: v for k, v in kwargs.items() if isinstance(v, (str, int, float, bool))})
232234

233-
return self._start_span("Model invoke", parent_span, attributes)
235+
return self._start_span("Model invoke", parent_span, attributes, span_kind=trace_api.SpanKind.CLIENT)
234236

235237
def end_model_invoke_span(
236238
self, span: Span, message: Message, usage: Usage, error: Optional[Exception] = None
@@ -274,7 +276,7 @@ def start_tool_call_span(self, tool: ToolUse, parent_span: Optional[Span] = None
274276
attributes.update(kwargs)
275277

276278
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)
278280

279281
def end_tool_call_span(
280282
self, span: Span, tool_result: Optional[ToolResult], error: Optional[Exception] = None
@@ -335,7 +337,7 @@ def start_event_loop_cycle_span(
335337
attributes.update({k: v for k, v in kwargs.items() if isinstance(v, (str, int, float, bool))})
336338

337339
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)
339341

340342
def end_event_loop_cycle_span(
341343
self,
@@ -405,7 +407,7 @@ def start_agent_span(
405407
# Add additional kwargs as attributes
406408
attributes.update({k: v for k, v in kwargs.items() if isinstance(v, (str, int, float, bool))})
407409

408-
return self._start_span(agent_name, attributes=attributes)
410+
return self._start_span(agent_name, attributes=attributes, span_kind=trace_api.SpanKind.CLIENT)
409411

410412
def end_agent_span(
411413
self,

tests/strands/telemetry/test_tracer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pytest
77
from opentelemetry.trace import (
8+
SpanKind,
89
StatusCode, # type: ignore
910
)
1011

@@ -75,7 +76,7 @@ def test_start_span(mock_tracer):
7576

7677
span = tracer._start_span("test_span", attributes={"key": "value"})
7778

78-
mock_tracer.start_span.assert_called_once_with(name="test_span", context=None)
79+
mock_tracer.start_span.assert_called_once_with(name="test_span", context=None, kind=SpanKind.INTERNAL)
7980
mock_span.set_attribute.assert_any_call("key", "value")
8081
assert span is not None
8182

@@ -151,6 +152,7 @@ def test_start_model_invoke_span(mock_tracer):
151152

152153
mock_tracer.start_span.assert_called_once()
153154
assert mock_tracer.start_span.call_args[1]["name"] == "Model invoke"
155+
assert mock_tracer.start_span.call_args[1]["kind"] == SpanKind.CLIENT
154156
mock_span.set_attribute.assert_any_call("gen_ai.system", "strands-agents")
155157
mock_span.set_attribute.assert_any_call("agent.name", "TestAgent")
156158
mock_span.set_attribute.assert_any_call("gen_ai.request.model", model_id)

0 commit comments

Comments
 (0)