@@ -149,15 +149,20 @@ def test_start_model_invoke_span(mock_tracer):
149149
150150 messages = [{"role" : "user" , "content" : [{"text" : "Hello" }]}]
151151 model_id = "test-model"
152+ custom_attrs = {"custom_key" : "custom_value" , "user_id" : "12345" }
152153
153- span = tracer .start_model_invoke_span (messages = messages , agent_name = "TestAgent" , model_id = model_id )
154+ span = tracer .start_model_invoke_span (
155+ messages = messages , agent_name = "TestAgent" , model_id = model_id , custom_trace_attributes = custom_attrs
156+ )
154157
155158 mock_tracer .start_span .assert_called_once ()
156159 assert mock_tracer .start_span .call_args [1 ]["name" ] == "chat"
157160 assert mock_tracer .start_span .call_args [1 ]["kind" ] == SpanKind .INTERNAL
158161 mock_span .set_attribute .assert_any_call ("gen_ai.system" , "strands-agents" )
159162 mock_span .set_attribute .assert_any_call ("gen_ai.operation.name" , "chat" )
160163 mock_span .set_attribute .assert_any_call ("gen_ai.request.model" , model_id )
164+ mock_span .set_attribute .assert_any_call ("custom_key" , "custom_value" )
165+ mock_span .set_attribute .assert_any_call ("user_id" , "12345" )
161166 mock_span .add_event .assert_called_with (
162167 "gen_ai.user.message" , attributes = {"content" : json .dumps (messages [0 ]["content" ])}
163168 )
@@ -293,15 +298,18 @@ def test_start_tool_call_span(mock_tracer):
293298 mock_tracer .start_span .return_value = mock_span
294299
295300 tool = {"name" : "test-tool" , "toolUseId" : "123" , "input" : {"param" : "value" }}
301+ custom_attrs = {"session_id" : "abc123" , "environment" : "production" }
296302
297- span = tracer .start_tool_call_span (tool )
303+ span = tracer .start_tool_call_span (tool , custom_trace_attributes = custom_attrs )
298304
299305 mock_tracer .start_span .assert_called_once ()
300306 assert mock_tracer .start_span .call_args [1 ]["name" ] == "execute_tool test-tool"
301307 mock_span .set_attribute .assert_any_call ("gen_ai.tool.name" , "test-tool" )
302308 mock_span .set_attribute .assert_any_call ("gen_ai.system" , "strands-agents" )
303309 mock_span .set_attribute .assert_any_call ("gen_ai.operation.name" , "execute_tool" )
304310 mock_span .set_attribute .assert_any_call ("gen_ai.tool.call.id" , "123" )
311+ mock_span .set_attribute .assert_any_call ("session_id" , "abc123" )
312+ mock_span .set_attribute .assert_any_call ("environment" , "production" )
305313 mock_span .add_event .assert_any_call (
306314 "gen_ai.tool.message" , attributes = {"role" : "tool" , "content" : json .dumps ({"param" : "value" }), "id" : "123" }
307315 )
@@ -361,14 +369,17 @@ def test_start_swarm_call_span_with_string_task(mock_tracer):
361369 mock_tracer .start_span .return_value = mock_span
362370
363371 task = "Design foo bar"
372+ custom_attrs = {"workflow_id" : "wf-789" , "priority" : "high" }
364373
365- span = tracer .start_multiagent_span (task , "swarm" )
374+ span = tracer .start_multiagent_span (task , "swarm" , custom_trace_attributes = custom_attrs )
366375
367376 mock_tracer .start_span .assert_called_once ()
368377 assert mock_tracer .start_span .call_args [1 ]["name" ] == "invoke_swarm"
369378 mock_span .set_attribute .assert_any_call ("gen_ai.system" , "strands-agents" )
370379 mock_span .set_attribute .assert_any_call ("gen_ai.agent.name" , "swarm" )
371380 mock_span .set_attribute .assert_any_call ("gen_ai.operation.name" , "invoke_swarm" )
381+ mock_span .set_attribute .assert_any_call ("workflow_id" , "wf-789" )
382+ mock_span .set_attribute .assert_any_call ("priority" , "high" )
372383 mock_span .add_event .assert_any_call ("gen_ai.user.message" , attributes = {"content" : "Design foo bar" })
373384 assert span is not None
374385
@@ -575,12 +586,17 @@ def test_start_event_loop_cycle_span(mock_tracer):
575586
576587 event_loop_kwargs = {"event_loop_cycle_id" : "cycle-123" }
577588 messages = [{"role" : "user" , "content" : [{"text" : "Hello" }]}]
589+ custom_attrs = {"request_id" : "req-456" , "trace_level" : "debug" }
578590
579- span = tracer .start_event_loop_cycle_span (event_loop_kwargs , messages = messages )
591+ span = tracer .start_event_loop_cycle_span (
592+ event_loop_kwargs , messages = messages , custom_trace_attributes = custom_attrs
593+ )
580594
581595 mock_tracer .start_span .assert_called_once ()
582596 assert mock_tracer .start_span .call_args [1 ]["name" ] == "execute_event_loop_cycle"
583597 mock_span .set_attribute .assert_any_call ("event_loop.cycle_id" , "cycle-123" )
598+ mock_span .set_attribute .assert_any_call ("request_id" , "req-456" )
599+ mock_span .set_attribute .assert_any_call ("trace_level" , "debug" )
584600 mock_span .add_event .assert_any_call (
585601 "gen_ai.user.message" , attributes = {"content" : json .dumps ([{"text" : "Hello" }])}
586602 )
0 commit comments