Skip to content

Commit 61158fb

Browse files
committed
dedupe _get_input_attributes
1 parent 8e0b78d commit 61158fb

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -995,46 +995,51 @@ def _get_input_attributes(template, send_pii, args, kwargs):
995995
"""
996996
attributes = {} # type: dict[str, Any]
997997

998-
if template in [SPANTEMPLATE.AI_AGENT, SPANTEMPLATE.AI_TOOL, SPANTEMPLATE.AI_CHAT]:
999-
for key, value in list(kwargs.items()):
1000-
if key in ("model", "model_name") and isinstance(value, str):
1001-
attributes[SPANDATA.GEN_AI_REQUEST_MODEL] = value
998+
mapping = {
999+
"model": (SPANDATA.GEN_AI_REQUEST_MODEL, str),
1000+
"model_name": (SPANDATA.GEN_AI_REQUEST_MODEL, str),
1001+
"agent": (SPANDATA.GEN_AI_AGENT_NAME, str),
1002+
"agent_name": (SPANDATA.GEN_AI_AGENT_NAME, str),
1003+
"max_tokens": (SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, int),
1004+
"frequency_penalty": (SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, float),
1005+
"presence_penalty": (SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, float),
1006+
"temperature": (SPANDATA.GEN_AI_REQUEST_TEMPERATURE, float),
1007+
"top_p": (SPANDATA.GEN_AI_REQUEST_TOP_P, float),
1008+
"top_k": (SPANDATA.GEN_AI_REQUEST_TOP_K, int),
1009+
}
10021010

1003-
if key in ("agent", "agent_name") and isinstance(value, str):
1004-
attributes[SPANDATA.GEN_AI_AGENT_NAME] = value
1011+
def _set_from_key(key, value):
1012+
# type: (str, Any) -> None
1013+
if key in mapping:
1014+
(attribute, data_type) = mapping[key]
1015+
if value is not None and isinstance(value, data_type):
1016+
attributes[attribute] = value
10051017

1018+
if template in [SPANTEMPLATE.AI_AGENT, SPANTEMPLATE.AI_TOOL, SPANTEMPLATE.AI_CHAT]:
1019+
for key, value in list(kwargs.items()):
10061020
if key == "prompt" and isinstance(value, str):
10071021
attributes.setdefault(SPANDATA.GEN_AI_REQUEST_MESSAGES, []).append(
10081022
{"role": "user", "content": value}
10091023
)
1024+
continue
1025+
10101026
if key == "system_prompt" and isinstance(value, str):
10111027
attributes.setdefault(SPANDATA.GEN_AI_REQUEST_MESSAGES, []).append(
10121028
{"role": "system", "content": value}
10131029
)
1030+
continue
10141031

1015-
if key == "max_tokens" and isinstance(value, int):
1016-
attributes[SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] = value
1017-
if key == "frequency_penalty" and isinstance(value, float):
1018-
attributes[SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY] = value
1019-
if key == "presence_penalty" and isinstance(value, float):
1020-
attributes[SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY] = value
1021-
if key == "temperature" and isinstance(value, float):
1022-
attributes[SPANDATA.GEN_AI_REQUEST_TEMPERATURE] = value
1023-
if key == "top_p" and isinstance(value, float):
1024-
attributes[SPANDATA.GEN_AI_REQUEST_TOP_P] = value
1025-
if key == "top_k" and isinstance(value, int):
1026-
attributes[SPANDATA.GEN_AI_REQUEST_TOP_K] = value
1032+
_set_from_key(key, value)
10271033

10281034
if SPANDATA.GEN_AI_REQUEST_MESSAGES in attributes:
10291035
attributes[SPANDATA.GEN_AI_REQUEST_MESSAGES] = safe_repr(
10301036
attributes[SPANDATA.GEN_AI_REQUEST_MESSAGES]
10311037
)
10321038

10331039
if template == SPANTEMPLATE.AI_TOOL and send_pii:
1034-
if send_pii:
1035-
attributes[SPANDATA.GEN_AI_TOOL_INPUT] = safe_repr(
1036-
{"args": args, "kwargs": kwargs}
1037-
)
1040+
attributes[SPANDATA.GEN_AI_TOOL_INPUT] = safe_repr(
1041+
{"args": args, "kwargs": kwargs}
1042+
)
10381043

10391044
return attributes
10401045

@@ -1059,12 +1064,12 @@ def _get_value(source, key):
10591064
value = None
10601065
return value if isinstance(value, int) else None
10611066

1062-
def _set_from_keys(target_key, keys):
1067+
def _set_from_keys(attribute, keys):
10631068
# type: (str, tuple[str, ...]) -> None
10641069
for key in keys:
10651070
value = _get_value(usage, key)
10661071
if value is not None:
1067-
attributes[target_key] = value
1072+
attributes[attribute] = value
10681073

10691074
_set_from_keys(
10701075
SPANDATA.GEN_AI_USAGE_INPUT_TOKENS,

0 commit comments

Comments
 (0)