2020 logger ,
2121 match_regex_list ,
2222 qualname_from_function ,
23- safe_repr ,
2423 to_string ,
2524 try_convert ,
2625 is_sentry_url ,
@@ -831,213 +830,6 @@ def _sample_rand_range(parent_sampled, sample_rate):
831830 return sample_rate , 1.0
832831
833832
834- def _get_span_name (template , name , kwargs = None ):
835- # type: (Union[str, "SPANTEMPLATE"], str, Optional[dict[str, Any]]) -> str
836- """
837- Get the name of the span based on the template and the name.
838- """
839- span_name = name
840-
841- if template == SPANTEMPLATE .AI_CHAT :
842- model = None
843- if kwargs and "model" in kwargs and isinstance (kwargs ["model" ], str ):
844- model = kwargs ["model" ]
845- elif (
846- kwargs and "model_name" in kwargs and isinstance (kwargs ["model_name" ], str )
847- ):
848- model = kwargs ["model_name" ]
849-
850- span_name = f"chat { model } " if model else "chat"
851-
852- elif template == SPANTEMPLATE .AI_AGENT :
853- span_name = f"invoke_agent { name } "
854-
855- elif template == SPANTEMPLATE .AI_TOOL :
856- span_name = f"execute_tool { name } "
857-
858- return span_name
859-
860-
861- def _get_span_op (template ):
862- # type: (Union[str, "SPANTEMPLATE"]) -> str
863- """
864- Get the operation of the span based on the template.
865- """
866- op = OP .FUNCTION
867-
868- if template == SPANTEMPLATE .AI_CHAT :
869- op = OP .GEN_AI_CHAT
870-
871- elif template == SPANTEMPLATE .AI_AGENT :
872- op = OP .GEN_AI_INVOKE_AGENT
873-
874- elif template == SPANTEMPLATE .AI_TOOL :
875- op = OP .GEN_AI_EXECUTE_TOOL
876-
877- return op
878-
879-
880- def _get_input_attributes (template , send_pii , args , kwargs ):
881- # type: (Union[str, "SPANTEMPLATE"], bool, tuple[Any, ...], dict[str, Any]) -> dict[str, Any]
882- """
883- Get input attributes for the given span template.
884- """
885- attributes = {} # type: dict[str, Any]
886-
887- if template in [SPANTEMPLATE .AI_AGENT , SPANTEMPLATE .AI_TOOL , SPANTEMPLATE .AI_CHAT ]:
888- for key , value in list (kwargs .items ()):
889- if key == "model" and isinstance (value , str ):
890- attributes [SPANDATA .GEN_AI_REQUEST_MODEL ] = value
891- elif key == "model_name" and isinstance (value , str ):
892- attributes [SPANDATA .GEN_AI_REQUEST_MODEL ] = value
893-
894- elif key == "agent" and isinstance (value , str ):
895- attributes [SPANDATA .GEN_AI_AGENT_NAME ] = value
896- elif key == "agent_name" and isinstance (value , str ):
897- attributes [SPANDATA .GEN_AI_AGENT_NAME ] = value
898-
899- elif key == "prompt" and isinstance (value , str ):
900- attributes .setdefault (SPANDATA .GEN_AI_REQUEST_MESSAGES , []).append (
901- {"role" : "user" , "content" : value }
902- )
903- elif key == "system_prompt" and isinstance (value , str ):
904- attributes .setdefault (SPANDATA .GEN_AI_REQUEST_MESSAGES , []).append (
905- {"role" : "system" , "content" : value }
906- )
907-
908- elif key == "max_tokens" and isinstance (value , int ):
909- attributes [SPANDATA .GEN_AI_REQUEST_MAX_TOKENS ] = value
910- elif key == "frequency_penalty" and isinstance (value , float ):
911- attributes [SPANDATA .GEN_AI_REQUEST_FREQUENCY_PENALTY ] = value
912- elif key == "presence_penalty" and isinstance (value , float ):
913- attributes [SPANDATA .GEN_AI_REQUEST_PRESENCE_PENALTY ] = value
914- elif key == "temperature" and isinstance (value , float ):
915- attributes [SPANDATA .GEN_AI_REQUEST_TEMPERATURE ] = value
916- elif key == "top_p" and isinstance (value , float ):
917- attributes [SPANDATA .GEN_AI_REQUEST_TOP_P ] = value
918- elif key == "top_k" and isinstance (value , int ):
919- attributes [SPANDATA .GEN_AI_REQUEST_TOP_K ] = value
920-
921- if SPANDATA .GEN_AI_REQUEST_MESSAGES in attributes :
922- attributes [SPANDATA .GEN_AI_REQUEST_MESSAGES ] = safe_repr (
923- attributes [SPANDATA .GEN_AI_REQUEST_MESSAGES ]
924- )
925-
926- if template == SPANTEMPLATE .AI_TOOL :
927- if send_pii :
928- attributes [SPANDATA .GEN_AI_TOOL_INPUT ] = safe_repr (
929- {"args" : args , "kwargs" : kwargs }
930- )
931-
932- return attributes
933-
934-
935- def _get_usage_attributes (usage ):
936- # type: (Any) -> dict[str, Any]
937- """
938- Get usage attributes.
939- """
940- attributes = {}
941-
942- if hasattr (usage , "prompt_tokens" ) and isinstance (usage .prompt_tokens , int ):
943- attributes [SPANDATA .GEN_AI_USAGE_INPUT_TOKENS ] = usage .prompt_tokens
944- elif hasattr (usage , "input_tokens" ) and isinstance (usage .input_tokens , int ):
945- attributes [SPANDATA .GEN_AI_USAGE_INPUT_TOKENS ] = usage .input_tokens
946- elif hasattr (usage , "completion_tokens" ) and isinstance (
947- usage .completion_tokens , int
948- ):
949- attributes [SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS ] = usage .output_tokens
950- elif hasattr (usage , "output_tokens" ) and isinstance (usage .output_tokens , int ):
951- attributes [SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS ] = usage .completion_tokens
952- elif hasattr (usage , "total_tokens" ) and isinstance (usage .total_tokens , int ):
953- attributes [SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS ] = usage .total_tokens
954-
955- return attributes
956-
957-
958- def _get_output_attributes (template , send_pii , result ):
959- # type: (Union[str, "SPANTEMPLATE"], bool, Any) -> dict[str, Any]
960- """
961- Get output attributes for the given span template.
962- """
963- attributes = {} # type: dict[str, Any]
964-
965- if template in [SPANTEMPLATE .AI_AGENT , SPANTEMPLATE .AI_TOOL , SPANTEMPLATE .AI_CHAT ]:
966- attributes .update (_get_usage_attributes (result ))
967- if hasattr (result , "usage" ):
968- attributes .update (_get_usage_attributes (result .usage ))
969- elif hasattr (result , "metadata" ):
970- if hasattr (result .metadata , "usage" ):
971- attributes .update (_get_usage_attributes (result .metadata .usage ))
972-
973- elif hasattr (result , "model" ) and isinstance (result .model , str ):
974- attributes [SPANDATA .GEN_AI_RESPONSE_MODEL ] = result .model
975- elif hasattr (result , "model_name" ) and isinstance (result .model_name , str ):
976- attributes [SPANDATA .GEN_AI_RESPONSE_MODEL ] = result .model_name
977-
978- if template == SPANTEMPLATE .AI_TOOL :
979- if send_pii :
980- attributes [SPANDATA .GEN_AI_TOOL_OUTPUT ] = safe_repr (result )
981-
982- return attributes
983-
984-
985- def _set_input_attributes (span , template , send_pii , name , f , args , kwargs ):
986- # type: (Span, Union[str, "SPANTEMPLATE"], bool, str, Any, tuple[Any, ...], dict[str, Any]) -> None
987- """
988- Set span input attributes based on the given span template.
989-
990- :param span: The span to set attributes on.
991- :param template: The template to use to set attributes on the span.
992- :param send_pii: Whether to send PII data.
993- :param f: The wrapped function.
994- :param args: The arguments to the wrapped function.
995- :param kwargs: The keyword arguments to the wrapped function.
996- """
997- attributes = {} # type: dict[str, Any]
998-
999- if template == SPANTEMPLATE .AI_AGENT :
1000- attributes = {
1001- SPANDATA .GEN_AI_OPERATION_NAME : "invoke_agent" ,
1002- SPANDATA .GEN_AI_AGENT_NAME : name ,
1003- }
1004- elif template == SPANTEMPLATE .AI_CHAT :
1005- attributes = {
1006- SPANDATA .GEN_AI_OPERATION_NAME : "chat" ,
1007- }
1008- elif template == SPANTEMPLATE .AI_TOOL :
1009- attributes = {
1010- SPANDATA .GEN_AI_OPERATION_NAME : "execute_tool" ,
1011- SPANDATA .GEN_AI_TOOL_NAME : name ,
1012- }
1013-
1014- docstring = f .__doc__
1015- if docstring is not None :
1016- attributes [SPANDATA .GEN_AI_TOOL_DESCRIPTION ] = docstring
1017-
1018- attributes .update (_get_input_attributes (template , send_pii , args , kwargs ))
1019- span .update_data (attributes or {})
1020-
1021-
1022- def _set_output_attributes (span , template , send_pii , result ):
1023- # type: (Span, Union[str, "SPANTEMPLATE"], bool, Any) -> None
1024- """
1025- Set span output attributes based on the given span template.
1026-
1027- :param span: The span to set attributes on.
1028- :param template: The template to use to set attributes on the span.
1029- :param send_pii: Whether to send PII data.
1030- :param result: The result of the wrapped function.
1031- """
1032- attributes = {} # type: dict[str, Any]
1033-
1034- if template == SPANTEMPLATE .AI_AGENT and isinstance (result , str ):
1035- attributes [SPANDATA .GEN_AI_TOOL_OUTPUT ] = result
1036-
1037- attributes .update (_get_output_attributes (template , send_pii , result ))
1038- span .update_data (attributes or {})
1039-
1040-
1041833def create_span_decorator (template , op = None , name = None , attributes = None ):
1042834 # type: (Union[str, "SPANTEMPLATE"], Optional[str], Optional[str], Optional[dict[str, Any]]) -> Any
1043835 """
@@ -1048,7 +840,6 @@ def create_span_decorator(template, op=None, name=None, attributes=None):
1048840 :param name: The name of the span.
1049841 :param attributes: Additional attributes to set on the span.
1050842 """
1051- from sentry_sdk .scope import should_send_default_pii
1052843
1053844 def span_decorator (f ):
1054845 # type: (Any) -> Any
@@ -1074,22 +865,12 @@ async def async_wrapper(*args, **kwargs):
1074865 )
1075866
1076867 function_name = name or qualname_from_function (f ) or ""
1077- send_pii = should_send_default_pii ()
1078868
1079869 with start_span_func (
1080- op = _get_span_op (template ),
1081- name = _get_span_name (template , function_name , kwargs ),
1082- ) as span :
1083-
1084- _set_input_attributes (
1085- span , template , send_pii , function_name , f , args , kwargs
1086- )
1087-
870+ op = OP .FUNCTION ,
871+ name = function_name ,
872+ ):
1088873 result = await f (* args , ** kwargs )
1089-
1090- _set_output_attributes (span , template , send_pii , result )
1091- span .update_data (attributes or {})
1092-
1093874 return result
1094875
1095876 try :
@@ -1115,21 +896,12 @@ def sync_wrapper(*args, **kwargs):
1115896 )
1116897
1117898 function_name = name or qualname_from_function (f ) or ""
1118- send_pii = should_send_default_pii ()
1119899
1120900 with start_span_func (
1121- op = _get_span_op (template ),
1122- name = _get_span_name (template , function_name , kwargs ),
1123- ) as span :
1124- _set_input_attributes (
1125- span , template , send_pii , function_name , f , args , kwargs
1126- )
1127-
901+ op = OP .FUNCTION ,
902+ name = function_name ,
903+ ):
1128904 result = f (* args , ** kwargs )
1129-
1130- _set_output_attributes (span , template , send_pii , result )
1131- span .update_data (attributes or {})
1132-
1133905 return result
1134906
1135907 try :
0 commit comments