-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for error case and rework things a bit
- Loading branch information
Showing
4 changed files
with
236 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
instrumentation/opentelemetry-instrumentation-botocore/tests/bedrock_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from opentelemetry.sdk.trace import ReadableSpan | ||
from opentelemetry.semconv._incubating.attributes import ( | ||
gen_ai_attributes as GenAIAttributes, | ||
) | ||
|
||
|
||
def assert_completion_attributes( | ||
span: ReadableSpan, | ||
request_model: str, | ||
response: dict[str, Any] | None, | ||
operation_name: str = "chat", | ||
request_top_p: int | None = None, | ||
request_temperature: int | None = None, | ||
request_max_tokens: int | None = None, | ||
request_stop_sequences: list[str] | None = None, | ||
): | ||
if usage := (response and response.get("usage")): | ||
input_tokens = usage["inputTokens"] | ||
output_tokens = usage["outputTokens"] | ||
else: | ||
input_tokens, output_tokens = None, None | ||
|
||
if response: | ||
finish_reason = (response["stopReason"],) | ||
else: | ||
finish_reason = None | ||
|
||
return assert_all_attributes( | ||
span, | ||
request_model, | ||
input_tokens, | ||
output_tokens, | ||
finish_reason, | ||
operation_name, | ||
request_top_p, | ||
request_temperature, | ||
request_max_tokens, | ||
tuple(request_stop_sequences) | ||
if request_stop_sequences is not None | ||
else request_stop_sequences, | ||
) | ||
|
||
|
||
def assert_equal_or_not_present(value, attribute_name, span): | ||
if value: | ||
assert value == span.attributes[attribute_name] | ||
else: | ||
assert attribute_name not in span.attributes | ||
|
||
|
||
def assert_all_attributes( | ||
span: ReadableSpan, | ||
request_model: str, | ||
input_tokens: int | None = None, | ||
output_tokens: int | None = None, | ||
finish_reason: tuple[str] | None = None, | ||
operation_name: str = "chat", | ||
request_top_p: int | None = None, | ||
request_temperature: int | None = None, | ||
request_max_tokens: int | None = None, | ||
request_stop_sequences: tuple[str] | None = None, | ||
): | ||
assert span.name == f"{operation_name} {request_model}" | ||
assert ( | ||
operation_name | ||
== span.attributes[GenAIAttributes.GEN_AI_OPERATION_NAME] | ||
) | ||
assert ( | ||
GenAIAttributes.GenAiSystemValues.AWS_BEDROCK.value | ||
== span.attributes[GenAIAttributes.GEN_AI_SYSTEM] | ||
) | ||
assert ( | ||
request_model == span.attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] | ||
) | ||
|
||
assert_equal_or_not_present( | ||
input_tokens, GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS, span | ||
) | ||
assert_equal_or_not_present( | ||
output_tokens, GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS, span | ||
) | ||
assert_equal_or_not_present( | ||
finish_reason, GenAIAttributes.GEN_AI_RESPONSE_FINISH_REASONS, span | ||
) | ||
assert_equal_or_not_present( | ||
request_top_p, GenAIAttributes.GEN_AI_REQUEST_TOP_P, span | ||
) | ||
assert_equal_or_not_present( | ||
request_temperature, GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE, span | ||
) | ||
assert_equal_or_not_present( | ||
request_max_tokens, GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS, span | ||
) | ||
assert_equal_or_not_present( | ||
request_stop_sequences, | ||
GenAIAttributes.GEN_AI_REQUEST_STOP_SEQUENCES, | ||
span, | ||
) |
69 changes: 69 additions & 0 deletions
69
...ntelemetry-instrumentation-botocore/tests/cassettes/test_converse_with_invalid_model.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
interactions: | ||
- request: | ||
body: |- | ||
{ | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"text": "Say this is a test" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
headers: | ||
Content-Length: | ||
- '77' | ||
Content-Type: | ||
- !!binary | | ||
YXBwbGljYXRpb24vanNvbg== | ||
User-Agent: | ||
- !!binary | | ||
Qm90bzMvMS4zNS41NiBtZC9Cb3RvY29yZSMxLjM1LjU2IHVhLzIuMCBvcy9saW51eCM2LjEuMC0x | ||
MDM0LW9lbSBtZC9hcmNoI3g4Nl82NCBsYW5nL3B5dGhvbiMzLjEwLjEyIG1kL3B5aW1wbCNDUHl0 | ||
aG9uIGNmZy9yZXRyeS1tb2RlI2xlZ2FjeSBCb3RvY29yZS8xLjM1LjU2 | ||
X-Amz-Date: | ||
- !!binary | | ||
MjAyNTAxMTVUMTEwMTQ3Wg== | ||
X-Amz-Security-Token: | ||
- test_aws_security_token | ||
X-Amzn-Trace-Id: | ||
- !!binary | | ||
Um9vdD0xLWIzM2JhNTkxLTdkYmQ0ZDZmYTBmZTdmYzc2MTExOThmNztQYXJlbnQ9NzRmNmQ1NTEz | ||
MzkzMzUxNTtTYW1wbGVkPTE= | ||
amz-sdk-invocation-id: | ||
- !!binary | | ||
NTQ5MmQ0NTktNzhkNi00ZWY4LTlmMDMtZTA5ODhkZGRiZDI5 | ||
amz-sdk-request: | ||
- !!binary | | ||
YXR0ZW1wdD0x | ||
authorization: | ||
- Bearer test_aws_authorization | ||
method: POST | ||
uri: https://bedrock-runtime.eu-central-1.amazonaws.com/model/does-not-exist/converse | ||
response: | ||
body: | ||
string: |- | ||
{ | ||
"message": "The provided model identifier is invalid." | ||
} | ||
headers: | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '55' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Wed, 15 Jan 2025 11:01:47 GMT | ||
Set-Cookie: test_set_cookie | ||
x-amzn-ErrorType: | ||
- ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/ | ||
x-amzn-RequestId: | ||
- d425bf99-8a4e-4d83-8d77-a48410dd82b2 | ||
status: | ||
code: 400 | ||
message: Bad Request | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters