Skip to content

Commit 2011ab4

Browse files
committed
Check PII settings before adding tool input/output
1 parent a6eed59 commit 2011ab4

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,8 @@ def _get_span_op(template):
877877
return op
878878

879879

880-
def _get_input_attributes(template, args, kwargs):
881-
# type: (Union[str, "SpanTemplate"], tuple[Any, ...], dict[str, Any]) -> dict[str, Any]
880+
def _get_input_attributes(template, send_pii, args, kwargs):
881+
# type: (Union[str, "SpanTemplate"], bool, tuple[Any, ...], dict[str, Any]) -> dict[str, Any]
882882
"""
883883
Get input attributes for the given span template.
884884
"""
@@ -927,15 +927,16 @@ def _get_input_attributes(template, args, kwargs):
927927
attributes.setdefault(SPANDATA.GEN_AI_REQUEST_TOP_K, []).append(value)
928928

929929
if template == SpanTemplate.AI_TOOL:
930-
attributes[SPANDATA.GEN_AI_TOOL_INPUT] = safe_repr(
931-
{"args": args, "kwargs": kwargs}
932-
)
930+
if send_pii:
931+
attributes[SPANDATA.GEN_AI_TOOL_INPUT] = safe_repr(
932+
{"args": args, "kwargs": kwargs}
933+
)
933934

934935
return attributes
935936

936937

937-
def _get_output_attributes(template, result):
938-
# type: (Union[str, "SpanTemplate"], Any) -> dict[str, Any]
938+
def _get_output_attributes(template, send_pii, result):
939+
# type: (Union[str, "SpanTemplate"], bool, Any) -> dict[str, Any]
939940
"""
940941
Get output attributes for the given span template.
941942
"""
@@ -987,18 +988,20 @@ def _get_output_attributes(template, result):
987988
attributes[SPANDATA.GEN_AI_RESPONSE_MODEL] = result.model_name
988989

989990
if template == SpanTemplate.AI_TOOL:
990-
attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = safe_repr(result)
991+
if send_pii:
992+
attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = safe_repr(result)
991993

992994
return attributes
993995

994996

995-
def _set_input_attributes(span, template, name, f, args, kwargs):
996-
# type: (Span, Union[str, "SpanTemplate"], str, Any, tuple[Any, ...], dict[str, Any]) -> None
997+
def _set_input_attributes(span, template, send_pii, name, f, args, kwargs):
998+
# type: (Span, Union[str, "SpanTemplate"], bool, str, Any, tuple[Any, ...], dict[str, Any]) -> None
997999
"""
9981000
Set span input attributes based on the given span template.
9991001
10001002
:param span: The span to set attributes on.
10011003
:param template: The template to use to set attributes on the span.
1004+
:param send_pii: Whether to send PII data.
10021005
:param f: The wrapped function.
10031006
:param args: The arguments to the wrapped function.
10041007
:param kwargs: The keyword arguments to the wrapped function.
@@ -1024,25 +1027,26 @@ def _set_input_attributes(span, template, name, f, args, kwargs):
10241027
if docstring is not None:
10251028
attributes[SPANDATA.GEN_AI_TOOL_DESCRIPTION] = docstring
10261029

1027-
attributes.update(_get_input_attributes(template, args, kwargs))
1030+
attributes.update(_get_input_attributes(template, send_pii, args, kwargs))
10281031
span.set_data(attributes)
10291032

10301033

1031-
def _set_output_attributes(span, template, result):
1032-
# type: (Span, Union[str, "SpanTemplate"], Any) -> None
1034+
def _set_output_attributes(span, template, send_pii, result):
1035+
# type: (Span, Union[str, "SpanTemplate"], bool, Any) -> None
10331036
"""
10341037
Set span output attributes based on the given span template.
10351038
10361039
:param span: The span to set attributes on.
10371040
:param template: The template to use to set attributes on the span.
1041+
:param send_pii: Whether to send PII data.
10381042
:param result: The result of the wrapped function.
10391043
"""
10401044
attributes = {} # type: dict[str, Any]
10411045

10421046
if template == SpanTemplate.AI_AGENT and isinstance(result, str):
10431047
attributes[SPANDATA.GEN_AI_TOOL_OUTPUT] = result
10441048

1045-
attributes.update(_get_output_attributes(template, result))
1049+
attributes.update(_get_output_attributes(template, send_pii, result))
10461050
span.set_data(attributes)
10471051

10481052

@@ -1056,6 +1060,7 @@ def create_span_decorator(template, op=None, name=None, attributes=None):
10561060
:param name: The name of the span.
10571061
:param attributes: Additional attributes to set on the span.
10581062
"""
1063+
from sentry_sdk.scope import should_send_default_pii
10591064

10601065
def span_decorator(f):
10611066
# type: (Any) -> Any
@@ -1081,15 +1086,20 @@ async def async_wrapper(*args, **kwargs):
10811086
)
10821087

10831088
function_name = name or qualname_from_function(f) or ""
1089+
send_pii = should_send_default_pii()
1090+
10841091
with start_span_func(
10851092
op=_get_span_op(template),
10861093
name=_get_span_name(template, function_name, kwargs),
10871094
) as span:
1088-
_set_input_attributes(span, template, function_name, f, args, kwargs)
1095+
1096+
_set_input_attributes(
1097+
span, template, send_pii, function_name, f, args, kwargs
1098+
)
10891099

10901100
result = await f(*args, **kwargs)
10911101

1092-
_set_output_attributes(span, template, result)
1102+
_set_output_attributes(span, template, send_pii, result)
10931103
span.set_data(attributes)
10941104

10951105
return result
@@ -1117,15 +1127,19 @@ def sync_wrapper(*args, **kwargs):
11171127
)
11181128

11191129
function_name = name or qualname_from_function(f) or ""
1130+
send_pii = should_send_default_pii()
1131+
11201132
with start_span_func(
11211133
op=_get_span_op(template),
11221134
name=_get_span_name(template, function_name, kwargs),
11231135
) as span:
1124-
_set_input_attributes(span, template, function_name, f, args, kwargs)
1136+
_set_input_attributes(
1137+
span, template, send_pii, function_name, f, args, kwargs
1138+
)
11251139

11261140
result = f(*args, **kwargs)
11271141

1128-
_set_output_attributes(span, template, result)
1142+
_set_output_attributes(span, template, send_pii, result)
11291143
span.set_data(attributes)
11301144

11311145
return result
@@ -1144,12 +1158,12 @@ def sync_wrapper(*args, **kwargs):
11441158

11451159

11461160
# Circular imports
1161+
from sentry_sdk.consts import SpanTemplate
11471162
from sentry_sdk.tracing import (
11481163
BAGGAGE_HEADER_NAME,
11491164
LOW_QUALITY_TRANSACTION_SOURCES,
11501165
SENTRY_TRACE_HEADER_NAME,
11511166
)
1152-
from sentry_sdk.consts import SpanTemplate
11531167

11541168
if TYPE_CHECKING:
11551169
from sentry_sdk.tracing import Span

0 commit comments

Comments
 (0)