|
| 1 | +import boto3 |
| 2 | +import pytest |
| 3 | +from botocore.exceptions import ClientError |
| 4 | +from botocore.loaders import Loader |
| 5 | +from botocore.stub import Stubber |
| 6 | + |
| 7 | +import sentry_sdk |
| 8 | +from sentry_sdk.consts import SPANDATA |
| 9 | +from sentry_sdk.integrations.boto3 import Boto3Integration |
| 10 | + |
| 11 | +pytestmark = pytest.mark.skipif( |
| 12 | + "bedrock-runtime" not in Loader().list_available_services("service-2"), |
| 13 | + reason="botocore is too old for bedrock-runtime.", |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def converse_response(): |
| 19 | + return { |
| 20 | + "output": {"message": {"role": "assistant", "content": [{"text": "Hello"}]}}, |
| 21 | + "stopReason": "end_turn", |
| 22 | + "usage": {"inputTokens": 50, "outputTokens": 30, "totalTokens": 80}, |
| 23 | + "metrics": {"latencyMs": 500}, |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 28 | +def test_converse(sentry_init, capture_items, span_streaming, converse_response): |
| 29 | + sentry_init( |
| 30 | + traces_sample_rate=1.0, |
| 31 | + integrations=[Boto3Integration()], |
| 32 | + trace_lifecycle="stream" if span_streaming else "static", |
| 33 | + ) |
| 34 | + |
| 35 | + session = boto3.Session( |
| 36 | + aws_access_key_id="-", aws_secret_access_key="-", region_name="us-east-1" |
| 37 | + ) |
| 38 | + client = session.client("bedrock-runtime") |
| 39 | + stubber = Stubber(client) |
| 40 | + stubber.add_response("converse", converse_response) |
| 41 | + |
| 42 | + items = capture_items() |
| 43 | + |
| 44 | + with stubber, sentry_sdk.start_transaction(name="parent"): |
| 45 | + client.converse( |
| 46 | + modelId="anthropic.claude-3-sonnet-20240229-v1:0", |
| 47 | + messages=[{"role": "user", "content": [{"text": "Hi"}]}], |
| 48 | + ) |
| 49 | + |
| 50 | + sentry_sdk.flush() |
| 51 | + (span,) = (item.payload for item in items if item.type == "span") |
| 52 | + |
| 53 | + assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock" |
| 54 | + assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is False |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 58 | +def test_converse_error(sentry_init, capture_items, span_streaming): |
| 59 | + sentry_init( |
| 60 | + traces_sample_rate=1.0, |
| 61 | + integrations=[Boto3Integration()], |
| 62 | + trace_lifecycle="stream" if span_streaming else "static", |
| 63 | + ) |
| 64 | + |
| 65 | + session = boto3.Session( |
| 66 | + aws_access_key_id="-", aws_secret_access_key="-", region_name="us-east-1" |
| 67 | + ) |
| 68 | + client = session.client("bedrock-runtime") |
| 69 | + original_exception = ClientError( |
| 70 | + error_response={ |
| 71 | + "Error": {"Code": "AccessDeniedException", "Message": "Access denied"}, |
| 72 | + "ResponseMetadata": { |
| 73 | + "RequestId": "test-id", |
| 74 | + "HostId": "", |
| 75 | + "HTTPStatusCode": 403, |
| 76 | + "HTTPHeaders": {}, |
| 77 | + "RetryAttempts": 1, |
| 78 | + }, |
| 79 | + }, |
| 80 | + operation_name="Converse", |
| 81 | + ) |
| 82 | + |
| 83 | + def raise_client_error(**kwargs): |
| 84 | + raise original_exception |
| 85 | + |
| 86 | + client.meta.events.register("before-parameter-build", raise_client_error) |
| 87 | + |
| 88 | + items = capture_items() |
| 89 | + |
| 90 | + with sentry_sdk.start_transaction(name="parent"), pytest.raises(ClientError): |
| 91 | + client.converse( |
| 92 | + modelId="anthropic.claude-3-sonnet-20240229-v1:0", |
| 93 | + messages=[{"role": "user", "content": [{"text": "Hi"}]}], |
| 94 | + ) |
| 95 | + |
| 96 | + sentry_sdk.flush() |
| 97 | + (span,) = (item.payload for item in items if item.type == "span") |
| 98 | + |
| 99 | + assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock" |
| 100 | + assert span["attributes"][SPANDATA.GEN_AI_OPERATION_NAME] == "chat" |
| 101 | + |
| 102 | + assert span["attributes"][SPANDATA.ERROR_TYPE] == "AccessDeniedException" |
| 103 | + assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 403 |
| 104 | + assert span["attributes"][SPANDATA.AWS_REQUEST_ID] == "test-id" |
| 105 | + assert span["attributes"][SPANDATA.HTTP_REQUEST_RESEND_COUNT] == 1 |
| 106 | + assert span["status"] == "error" |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 110 | +def test_converse_stream(sentry_init, capture_items, span_streaming): |
| 111 | + sentry_init( |
| 112 | + traces_sample_rate=1.0, |
| 113 | + integrations=[Boto3Integration()], |
| 114 | + trace_lifecycle="stream" if span_streaming else "static", |
| 115 | + ) |
| 116 | + |
| 117 | + session = boto3.Session( |
| 118 | + aws_access_key_id="-", aws_secret_access_key="-", region_name="us-east-1" |
| 119 | + ) |
| 120 | + client = session.client("bedrock-runtime") |
| 121 | + stubber = Stubber(client) |
| 122 | + stubber.add_response("converse_stream", {}) |
| 123 | + |
| 124 | + items = capture_items() |
| 125 | + |
| 126 | + with stubber, sentry_sdk.start_transaction(name="parent"): |
| 127 | + client.converse_stream( |
| 128 | + modelId="anthropic.claude-3-sonnet-20240229-v1:0", |
| 129 | + messages=[{"role": "user", "content": [{"text": "Hi"}]}], |
| 130 | + ) |
| 131 | + |
| 132 | + sentry_sdk.flush() |
| 133 | + (span,) = (item.payload for item in items if item.type == "span") |
| 134 | + |
| 135 | + assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock" |
| 136 | + assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.parametrize("span_streaming", [True, False]) |
| 140 | +def test_converse_stream_error(sentry_init, capture_items, span_streaming): |
| 141 | + sentry_init( |
| 142 | + traces_sample_rate=1.0, |
| 143 | + integrations=[Boto3Integration()], |
| 144 | + trace_lifecycle="stream" if span_streaming else "static", |
| 145 | + ) |
| 146 | + |
| 147 | + session = boto3.Session( |
| 148 | + aws_access_key_id="-", aws_secret_access_key="-", region_name="us-east-1" |
| 149 | + ) |
| 150 | + client = session.client("bedrock-runtime") |
| 151 | + original_exception = ClientError( |
| 152 | + error_response={ |
| 153 | + "Error": {"Code": "AccessDeniedException", "Message": "Access denied"}, |
| 154 | + "ResponseMetadata": { |
| 155 | + "RequestId": "test-id", |
| 156 | + "HostId": "", |
| 157 | + "HTTPStatusCode": 403, |
| 158 | + "HTTPHeaders": {}, |
| 159 | + "RetryAttempts": 1, |
| 160 | + }, |
| 161 | + }, |
| 162 | + operation_name="ConverseStream", |
| 163 | + ) |
| 164 | + |
| 165 | + def raise_client_error(**kwargs): |
| 166 | + raise original_exception |
| 167 | + |
| 168 | + client.meta.events.register("before-parameter-build", raise_client_error) |
| 169 | + |
| 170 | + items = capture_items() |
| 171 | + |
| 172 | + with sentry_sdk.start_transaction(name="parent"), pytest.raises(ClientError): |
| 173 | + client.converse_stream( |
| 174 | + modelId="anthropic.claude-3-sonnet-20240229-v1:0", |
| 175 | + messages=[{"role": "user", "content": [{"text": "Hi"}]}], |
| 176 | + ) |
| 177 | + |
| 178 | + sentry_sdk.flush() |
| 179 | + (span,) = (item.payload for item in items if item.type == "span") |
| 180 | + |
| 181 | + assert span["attributes"][SPANDATA.GEN_AI_PROVIDER_NAME] == "aws.bedrock" |
| 182 | + assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_STREAMING] is True |
| 183 | + |
| 184 | + assert span["attributes"][SPANDATA.ERROR_TYPE] == "AccessDeniedException" |
| 185 | + assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 403 |
| 186 | + assert span["attributes"][SPANDATA.AWS_REQUEST_ID] == "test-id" |
| 187 | + assert span["attributes"][SPANDATA.HTTP_REQUEST_RESEND_COUNT] == 1 |
| 188 | + assert span["status"] == "error" |
0 commit comments