1515 logger ,
1616 nanosecond_time ,
1717 should_be_treated_as_error ,
18+ qualname_from_function ,
1819)
1920
2021from typing import TYPE_CHECKING
@@ -1373,19 +1374,38 @@ async def my_async_function():
13731374 return start_child_span_decorator
13741375
13751376
1376- def set_span_attributes (span , attributes ):
1377+ def _set_span_attributes (span , attributes ):
13771378 # type: (Span, dict[str, Any]) -> None
1379+ """
1380+ Set the given attributes on the given span.
1381+
1382+ :param span: The span to set attributes on.
1383+ :param attributes: The attributes to set on the span.
1384+ """
13781385 for key , value in attributes .items ():
13791386 span .set_data (key , value )
13801387
13811388
1382- def set_input_attributes (span , template , args , kwargs ):
1383- # depending on `template` set some attributes on the span derived from args and kwargs
1389+ def _set_input_attributes (span , template , args , kwargs ):
1390+ """
1391+ Set LLM input attributes based on given information to the given span.
1392+
1393+ :param span: The span to set attributes on.
1394+ :param template: The template to use to set attributes on the span.
1395+ :param args: The arguments to the LLM call.
1396+ :param kwargs: The keyword arguments to the LLM call.
1397+ """
13841398 pass
13851399
13861400
1387- def set_output_attributes (span , template , result ):
1388- # depending on `template` set some attributes on the span derived from result
1401+ def _set_output_attributes (span , template , result ):
1402+ """
1403+ Set LLM output attributes based on given information to the given span.
1404+
1405+ :param span: The span to set attributes on.
1406+ :param template: The template to use to set attributes on the span.
1407+ :param result: The result of the LLM call.
1408+ """
13891409 pass
13901410
13911411
@@ -1403,39 +1423,35 @@ def span_decorator(f, *a, **kw):
14031423 async def async_wrapper (* args , ** kwargs ):
14041424 # type: (*Any, **Any) -> Any
14051425 op = kw .get ("op" , OP .FUNCTION )
1406- span_name = kw .get ("name" , f . __name__ )
1426+ span_name = kw .get ("name" , qualname_from_function ( f ) )
14071427 attributes = kw .get ("attributes" , {})
14081428
14091429 with sentry_sdk .start_span (
14101430 op = op ,
14111431 name = span_name ,
14121432 ) as span :
1413- set_span_attributes (span , attributes )
1414- set_input_attributes (span , as_type , args , kwargs )
1415-
1433+ _set_span_attributes (span , attributes )
1434+ _set_input_attributes (span , as_type , args , kwargs )
14161435 result = await f (* args , ** kwargs )
1417-
1418- set_output_attributes (span , as_type , result )
1436+ _set_output_attributes (span , as_type , result )
14191437
14201438 return result
14211439
14221440 @functools .wraps (f )
14231441 def sync_wrapper (* args , ** kwargs ):
14241442 # type: (*Any, **Any) -> Any
14251443 op = kw .get ("op" , OP .FUNCTION )
1426- span_name = kw .get ("name" , f . __name__ )
1444+ span_name = kw .get ("name" , qualname_from_function ( f ) )
14271445 attributes = kw .get ("attributes" , {})
14281446
14291447 with sentry_sdk .start_span (
14301448 op = op ,
14311449 name = span_name ,
14321450 ) as span :
1433- set_span_attributes (span , attributes )
1434- set_input_attributes (span , as_type , args , kwargs )
1435-
1451+ _set_span_attributes (span , attributes )
1452+ _set_input_attributes (span , as_type , args , kwargs )
14361453 result = f (* args , ** kwargs )
1437-
1438- set_output_attributes (span , as_type , result )
1454+ _set_output_attributes (span , as_type , result )
14391455
14401456 return result
14411457
@@ -1461,34 +1477,35 @@ def ai_chat_decorator(f):
14611477
14621478 def ai_agent_decorator (f ):
14631479 # type: (Optional[Callable[P, R]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1464- span_name = name or f . __name__
1480+ agent_name = name or qualname_from_function ( f )
14651481 attributes = {
1466- "gen_ai.agent.name" : span_name ,
14671482 "gen_ai.operation.name" : "invoke_agent" ,
1483+ "gen_ai.agent.name" : agent_name ,
14681484 }
14691485
14701486 return span_decorator (
14711487 f ,
14721488 op = OP .GEN_AI_INVOKE_AGENT ,
1473- name = f"invoke_agent { span_name } " ,
1489+ name = f"invoke_agent { agent_name } " ,
14741490 attributes = attributes ,
14751491 )
14761492
14771493 def ai_tool_decorator (f ):
14781494 # type: (Optional[Callable[P, R]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1479- span_name = name or f . __name__
1495+ tool_name = name or qualname_from_function ( f )
14801496 attributes = {
1481- "gen_ai.tool.name" : span_name ,
14821497 "gen_ai.operation.name" : "execute_tool" ,
1498+ "gen_ai.tool.name" : tool_name ,
14831499 }
14841500
14851501 return span_decorator (
14861502 f ,
14871503 op = OP .GEN_AI_EXECUTE_TOOL ,
1488- name = f"execute_tool { span_name } " ,
1504+ name = f"execute_tool { tool_name } " ,
14891505 attributes = attributes ,
14901506 )
14911507
1508+ # Select a type based decorator (with default fallback to span_decorator)
14921509 decorator_for_type = {
14931510 "ai_chat" : ai_chat_decorator ,
14941511 "ai_agent" : ai_agent_decorator ,
0 commit comments