Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions sentry_sdk/integrations/boto3/_services/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
if model_id is not None:
attributes[SPANDATA.GEN_AI_REQUEST_MODEL] = model_id

inference_config = ctx.params.get("inferenceConfig")

if not isinstance(inference_config, dict):
return attributes

max_tokens = inference_config.get("maxTokens")
if max_tokens is not None:
attributes[SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] = max_tokens

temperature = inference_config.get("temperature")
if temperature is not None:
attributes[SPANDATA.GEN_AI_REQUEST_TEMPERATURE] = temperature

top_p = inference_config.get("topP")
if top_p is not None:
attributes[SPANDATA.GEN_AI_REQUEST_TOP_P] = top_p

return attributes

def get_response_attributes(
Expand Down
72 changes: 72 additions & 0 deletions tests/integrations/boto3/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def test_converse(
client.converse(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

sentry_sdk.flush()
Expand All @@ -72,13 +77,22 @@ def test_converse(
span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL]
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9
else:
events = capture_events()

with stubber, sentry_sdk.start_transaction(name="parent"):
client.converse(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

(transaction,) = events
Expand All @@ -92,6 +106,10 @@ def test_converse(
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["data"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9


@pytest.mark.parametrize("span_streaming", [True, False])
@pytest.mark.parametrize("stream_gen_ai_spans", [True, False])
Expand Down Expand Up @@ -135,6 +153,11 @@ def raise_client_error(**kwargs):
client.converse(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

sentry_sdk.flush()
Expand All @@ -152,6 +175,10 @@ def raise_client_error(**kwargs):
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9

assert span["attributes"][SPANDATA.ERROR_TYPE] == "AccessDeniedException"
assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 403
assert span["attributes"][SPANDATA.AWS_REQUEST_ID] == "test-id"
Expand All @@ -164,6 +191,11 @@ def raise_client_error(**kwargs):
client.converse(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

(transaction,) = events
Expand All @@ -176,6 +208,10 @@ def raise_client_error(**kwargs):
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["data"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9

assert span["data"][SPANDATA.ERROR_TYPE] == "AccessDeniedException"
assert span["data"][SPANDATA.HTTP_STATUS_CODE] == 403
assert span["data"][SPANDATA.AWS_REQUEST_ID] == "test-id"
Expand Down Expand Up @@ -209,6 +245,11 @@ def test_converse_stream(
client.converse_stream(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

sentry_sdk.flush()
Expand All @@ -226,13 +267,22 @@ def test_converse_stream(
span["attributes"][SPANDATA.GEN_AI_REQUEST_MODEL]
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9
else:
events = capture_events()

with stubber, sentry_sdk.start_transaction(name="parent"):
client.converse_stream(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

(transaction,) = events
Expand All @@ -246,6 +296,10 @@ def test_converse_stream(
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["data"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9


@pytest.mark.parametrize("span_streaming", [True, False])
@pytest.mark.parametrize("stream_gen_ai_spans", [True, False])
Expand Down Expand Up @@ -289,6 +343,11 @@ def raise_client_error(**kwargs):
client.converse_stream(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

sentry_sdk.flush()
Expand All @@ -307,6 +366,10 @@ def raise_client_error(**kwargs):
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["attributes"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["attributes"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9

assert span["attributes"][SPANDATA.ERROR_TYPE] == "AccessDeniedException"
assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 403
assert span["attributes"][SPANDATA.AWS_REQUEST_ID] == "test-id"
Expand All @@ -319,6 +382,11 @@ def raise_client_error(**kwargs):
client.converse_stream(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": [{"text": "Hi"}]}],
inferenceConfig={
"maxTokens": 1024,
"temperature": 0.7,
"topP": 0.9,
},
)

(transaction,) = events
Expand All @@ -332,6 +400,10 @@ def raise_client_error(**kwargs):
== "anthropic.claude-3-sonnet-20240229-v1:0"
)

assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 1024
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["data"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9

assert span["data"][SPANDATA.ERROR_TYPE] == "AccessDeniedException"
assert span["data"][SPANDATA.HTTP_STATUS_CODE] == 403
assert span["data"][SPANDATA.AWS_REQUEST_ID] == "test-id"
Expand Down
Loading