|
| 1 | +import os |
| 2 | + |
| 3 | +import boto3 |
| 4 | +from braintrust.auto import auto_instrument |
| 5 | +from braintrust.integrations.boto3.patchers import Boto3ConversePatcher |
| 6 | +from braintrust.integrations.test_utils import autoinstrument_test_context |
| 7 | + |
| 8 | + |
| 9 | +def is_patched(target, patcher): |
| 10 | + return bool(getattr(target, patcher.nested_patch_marker_attr(), False)) |
| 11 | + |
| 12 | + |
| 13 | +# verify not patched initially |
| 14 | +original_client = boto3.client( |
| 15 | + "bedrock-runtime", |
| 16 | + aws_access_key_id="test-key", |
| 17 | + aws_secret_access_key="test-access-key", |
| 18 | + aws_session_token="test-session-key", |
| 19 | + region_name="us-east-1", |
| 20 | +) |
| 21 | + |
| 22 | +assert is_patched(original_client, Boto3ConversePatcher) is False |
| 23 | + |
| 24 | +# instrument |
| 25 | +results = auto_instrument() |
| 26 | +assert results.get("boto3") is True |
| 27 | + |
| 28 | +# patch |
| 29 | +patched_client = boto3.client( |
| 30 | + "bedrock-runtime", |
| 31 | + aws_access_key_id="", |
| 32 | + aws_secret_access_key="", |
| 33 | + aws_session_token="", |
| 34 | + region_name="us-east-1", |
| 35 | +) |
| 36 | + |
| 37 | +# idempotent |
| 38 | +results = auto_instrument() |
| 39 | +assert results.get("boto3") is True |
| 40 | + |
| 41 | + |
| 42 | +assert is_patched(patched_client, Boto3ConversePatcher) is True |
| 43 | + |
| 44 | +# make api call |
| 45 | +with autoinstrument_test_context("test_auto_boto3", integration="boto3") as memory_logger: |
| 46 | + client = boto3.client( |
| 47 | + "bedrock-runtime", |
| 48 | + region_name="us-east-1", |
| 49 | + aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), |
| 50 | + aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), |
| 51 | + aws_session_token=os.getenv("AWS_SESSION_TOKEN"), |
| 52 | + ) |
| 53 | + |
| 54 | + response = response = client.converse( |
| 55 | + modelId="anthropic.claude-3-haiku-20240307-v1:0", |
| 56 | + messages=[{"role": "user", "content": [{"text": "what's 2+2"}]}], |
| 57 | + system=[{"text": "answer only in single integer"}], |
| 58 | + ) |
| 59 | + |
| 60 | + spans = memory_logger.pop() |
| 61 | + assert len(spans) == 1, f"Expected 1 span, got {len(spans)}" |
| 62 | + |
| 63 | + span = spans[0] |
| 64 | + assert span["metadata"]["model_provider"] == "anthropic" |
| 65 | + assert "claude" in span["metadata"]["model"] |
0 commit comments