Skip to content

Commit 38e6dea

Browse files
merge master
2 parents 9c7ab68 + f73ce4e commit 38e6dea

5 files changed

Lines changed: 548 additions & 422 deletions

File tree

‎sentry_sdk/integrations/boto3/_services/bedrock.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
3434
SPANDATA.GEN_AI_RESPONSE_STREAMING: ctx.operation_name == "ConverseStream",
3535
}
3636

37+
model_id = ctx.params.get("modelId")
38+
if model_id is not None:
39+
attributes[SPANDATA.GEN_AI_REQUEST_MODEL] = model_id
40+
3741
inference_config = ctx.params.get("inferenceConfig")
3842

3943
if not isinstance(inference_config, dict):

‎sentry_sdk/integrations/gql.py‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import sentry_sdk
2+
from sentry_sdk.data_collection import (
3+
_apply_data_collection_filtering_to_query_string,
4+
)
25
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
36
from sentry_sdk.scope import should_send_default_pii
47
from sentry_sdk.utils import (
8+
capture_internal_exceptions,
59
ensure_integration_enabled,
610
event_from_exception,
711
has_data_collection_enabled,
12+
parse_url,
813
parse_version,
914
)
1015

@@ -36,7 +41,7 @@
3641
from typing import TYPE_CHECKING
3742

3843
if TYPE_CHECKING:
39-
from typing import Any, Dict, Tuple, Union
44+
from typing import Any, Dict, Optional, Tuple, Union
4045

4146
from sentry_sdk._types import Event, EventProcessor
4247

@@ -99,9 +104,29 @@ def _request_info_from_transport(
99104
}
100105

101106
try:
102-
request_info["url"] = transport.url
107+
url = transport.url
103108
except AttributeError:
104-
pass
109+
return request_info
110+
111+
with capture_internal_exceptions():
112+
client_options = sentry_sdk.get_client().options
113+
use_data_collection = has_data_collection_enabled(client_options)
114+
115+
# Some transports accept a URL object as well as a string.
116+
# Credentials are always removed from the URL. Query values are
117+
# filtered here only when data_collection is not configured, since
118+
# its url_query_params setting does its own filtering.
119+
parsed_url = parse_url(str(url), sanitize=not use_data_collection)
120+
request_info["url"] = parsed_url.url
121+
122+
query_string: "Optional[str]" = parsed_url.query
123+
if use_data_collection and query_string:
124+
query_string = _apply_data_collection_filtering_to_query_string(
125+
query_string=query_string,
126+
behaviour=client_options["data_collection"]["url_query_params"],
127+
)
128+
if query_string:
129+
request_info["query_string"] = query_string
105130

106131
return request_info
107132

‎tests/integrations/boto3/test_bedrock.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def test_converse(
7474
assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
7575
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False
7676

77+
assert (
78+
span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL]
79+
== "anthropic.claude-3-sonnet-20240229-v1:0"
80+
)
7781
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
7882
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
7983
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9
@@ -97,6 +101,10 @@ def test_converse(
97101
assert span["data"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock"
98102
assert span["data"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
99103
assert span["data"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False
104+
assert (
105+
span["data"][SPANDATA.GEN_AI_REQUEST_MODEL]
106+
== "anthropic.claude-3-sonnet-20240229-v1:0"
107+
)
100108

101109
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
102110
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
@@ -162,6 +170,10 @@ def raise_client_error(**kwargs):
162170

163171
assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock"
164172
assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
173+
assert (
174+
span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL]
175+
== "anthropic.claude-3-sonnet-20240229-v1:0"
176+
)
165177

166178
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
167179
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
@@ -191,6 +203,10 @@ def raise_client_error(**kwargs):
191203

192204
assert span["data"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock"
193205
assert span["data"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
206+
assert (
207+
span["data"][SPANDATA.GEN_AI_REQUEST_MODEL]
208+
== "anthropic.claude-3-sonnet-20240229-v1:0"
209+
)
194210

195211
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
196212
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
@@ -248,6 +264,10 @@ def test_converse_stream(
248264
assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
249265
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True
250266

267+
assert (
268+
span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL]
269+
== "anthropic.claude-3-sonnet-20240229-v1:0"
270+
)
251271
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
252272
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
253273
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9
@@ -271,6 +291,10 @@ def test_converse_stream(
271291
assert span["data"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock"
272292
assert span["data"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
273293
assert span["data"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True
294+
assert (
295+
span["data"][SPANDATA.GEN_AI_REQUEST_MODEL]
296+
== "anthropic.claude-3-sonnet-20240229-v1:0"
297+
)
274298

275299
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
276300
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
@@ -337,6 +361,10 @@ def raise_client_error(**kwargs):
337361
assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock"
338362
assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
339363
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True
364+
assert (
365+
span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL]
366+
== "anthropic.claude-3-sonnet-20240229-v1:0"
367+
)
340368

341369
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
342370
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
@@ -367,6 +395,10 @@ def raise_client_error(**kwargs):
367395
assert span["data"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock"
368396
assert span["data"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat"
369397
assert span["data"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True
398+
assert (
399+
span["data"][SPANDATA.GEN_AI_REQUEST_MODEL]
400+
== "anthropic.claude-3-sonnet-20240229-v1:0"
401+
)
370402

371403
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
372404
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7

‎tests/integrations/gql/test_gql.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import pytest
24
import responses
35
from gql import Client, __version__, gql
@@ -273,3 +275,84 @@ def test_real_gql_request_with_error_data_collection_variables(
273275
assert event["request"]["data"].get("variables")
274276
else:
275277
assert not event["request"]["data"].get("variables")
278+
279+
280+
@pytest.mark.parametrize(
281+
"init_kwargs,expected_query_string",
282+
[
283+
pytest.param(
284+
{},
285+
"access_token=[Filtered]&v=[Filtered]&page=[Filtered]",
286+
id="no_pii_no_data_collection",
287+
),
288+
pytest.param(
289+
{"send_default_pii": True},
290+
"access_token=[Filtered]&v=[Filtered]&page=[Filtered]",
291+
id="legacy_pii_on",
292+
),
293+
pytest.param(
294+
{"data_collection": {}},
295+
"access_token=%5BFiltered%5D&v=1&page=2",
296+
id="data_collection_defaults",
297+
),
298+
pytest.param(
299+
{"data_collection": {"url_query_params": {"mode": "off"}}},
300+
None,
301+
id="data_collection_query_params_off",
302+
),
303+
pytest.param(
304+
{
305+
"data_collection": {
306+
"url_query_params": {"mode": "denylist", "terms": ["page"]}
307+
}
308+
},
309+
"access_token=%5BFiltered%5D&v=1&page=%5BFiltered%5D",
310+
id="data_collection_denylist",
311+
),
312+
pytest.param(
313+
{
314+
"data_collection": {
315+
"url_query_params": {"mode": "allowlist", "terms": ["page"]}
316+
}
317+
},
318+
"access_token=%5BFiltered%5D&v=%5BFiltered%5D&page=2",
319+
id="data_collection_allowlist",
320+
),
321+
pytest.param(
322+
{
323+
"send_default_pii": True,
324+
"data_collection": {"url_query_params": {"mode": "off"}},
325+
},
326+
None,
327+
id="data_collection_takes_precedence_over_send_default_pii",
328+
),
329+
],
330+
)
331+
@responses.activate
332+
def test_transport_url_is_sanitized(
333+
sentry_init, capture_events, init_kwargs, expected_query_string
334+
):
335+
sentry_init(integrations=[GQLIntegration()], **init_kwargs)
336+
events = capture_events()
337+
338+
responses.add(
339+
method=responses.POST,
340+
url=re.compile(r"https://.*api\.example\.com/graphql.*"),
341+
json={"errors": [{"message": "some error"}]},
342+
status=200,
343+
)
344+
transport = RequestsHTTPTransport(
345+
url="https://user:password@api.example.com/graphql?access_token=secret&v=1&page=2#frag"
346+
)
347+
client = Client(transport=transport)
348+
349+
with pytest.raises(TransportQueryError):
350+
client.execute(gql("query Example { example }"))
351+
352+
(event,) = events
353+
assert event["request"]["url"] == (
354+
"https://[Filtered]:[Filtered]@api.example.com/graphql"
355+
)
356+
assert event["request"].get("query_string") == expected_query_string
357+
assert "password" not in str(event["request"])
358+
assert "secret" not in str(event["request"])

0 commit comments

Comments
 (0)