-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bedrock): support metrics for bedrock (#1957)
Co-authored-by: Nir Gazit <nirga@users.noreply.github.com>
- Loading branch information
Showing
18 changed files
with
392 additions
and
77 deletions.
There are no files selected for viewing
246 changes: 207 additions & 39 deletions
246
...s/opentelemetry-instrumentation-bedrock/opentelemetry/instrumentation/bedrock/__init__.py
Large diffs are not rendered by default.
Oops, something went wrong.
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
1 change: 1 addition & 0 deletions
1
packages/opentelemetry-instrumentation-bedrock/tests/metrics/__init__.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 @@ | ||
"""unit tests.""" |
54 changes: 54 additions & 0 deletions
54
...ation-bedrock/tests/metrics/cassettes/test_bedrock_metrics/test_invoke_model_metrics.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,54 @@ | ||
interactions: | ||
- request: | ||
body: '{"inputText": "Tell me a joke about opentelemetry", "textGenerationConfig": | ||
{"maxTokenCount": 200, "temperature": 0.5, "topP": 0.5}}' | ||
headers: | ||
Accept: | ||
- !!binary | | ||
YXBwbGljYXRpb24vanNvbg== | ||
Content-Length: | ||
- '132' | ||
Content-Type: | ||
- !!binary | | ||
YXBwbGljYXRpb24vanNvbg== | ||
User-Agent: | ||
- !!binary | | ||
Qm90bzMvMS4zNC4xNjIgbWQvQm90b2NvcmUjMS4zNC4xNjIgdWEvMi4wIG9zL21hY29zIzIzLjYu | ||
MCBtZC9hcmNoI2FybTY0IGxhbmcvcHl0aG9uIzMuMTEuNSBtZC9weWltcGwjQ1B5dGhvbiBjZmcv | ||
cmV0cnktbW9kZSNsZWdhY3kgQm90b2NvcmUvMS4zNC4xNjI= | ||
X-Amz-Date: | ||
- !!binary | | ||
MjAyNDA5MTlUMjE0NjE5Wg== | ||
amz-sdk-invocation-id: | ||
- !!binary | | ||
MGVmMmNlZWUtNzA1OS00M2Y2LTk4OTUtZWUzMDdjNDFmNWI2 | ||
amz-sdk-request: | ||
- !!binary | | ||
YXR0ZW1wdD0x | ||
method: POST | ||
uri: https://bedrock-runtime.us-east-1.amazonaws.com/model/amazon.titan-text-express-v1/invoke | ||
response: | ||
body: | ||
string: '{"inputTextTokenCount":9,"results":[{"tokenCount":17,"outputText":"\nWhat | ||
do you call a bear with no teeth?\nA gummy bear.","completionReason":"FINISH"}]}' | ||
headers: | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '154' | ||
Content-Type: | ||
- application/json | ||
Date: | ||
- Thu, 19 Sep 2024 21:46:20 GMT | ||
X-Amzn-Bedrock-Input-Token-Count: | ||
- '9' | ||
X-Amzn-Bedrock-Invocation-Latency: | ||
- '1155' | ||
X-Amzn-Bedrock-Output-Token-Count: | ||
- '17' | ||
x-amzn-RequestId: | ||
- 58c863f2-7a84-4bf8-8b93-1d51ca8aa150 | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
68 changes: 68 additions & 0 deletions
68
packages/opentelemetry-instrumentation-bedrock/tests/metrics/test_bedrock_metrics.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,68 @@ | ||
import json | ||
|
||
import pytest | ||
from opentelemetry.semconv_ai import Meters, SpanAttributes | ||
|
||
|
||
@pytest.mark.vcr | ||
def test_invoke_model_metrics(test_context, brt): | ||
if brt is None: | ||
print("test_invoke_model_metrics test skipped.") | ||
return | ||
|
||
_, _, reader = test_context | ||
|
||
body = json.dumps( | ||
{ | ||
"inputText": "Tell me a joke about opentelemetry", | ||
"textGenerationConfig": { | ||
"maxTokenCount": 200, | ||
"temperature": 0.5, | ||
"topP": 0.5, | ||
}, | ||
} | ||
) | ||
|
||
brt.invoke_model( | ||
body=body, | ||
modelId='amazon.titan-text-express-v1', | ||
accept='application/json', | ||
contentType='application/json' | ||
) | ||
|
||
metrics_data = reader.get_metrics_data() | ||
resource_metrics = metrics_data.resource_metrics | ||
assert len(resource_metrics) > 0 | ||
|
||
found_token_metric = False | ||
found_duration_metric = False | ||
|
||
for rm in resource_metrics: | ||
for sm in rm.scope_metrics: | ||
for metric in sm.metrics: | ||
|
||
if metric.name == Meters.LLM_TOKEN_USAGE: | ||
found_token_metric = True | ||
for data_point in metric.data.data_points: | ||
assert data_point.attributes[SpanAttributes.LLM_TOKEN_TYPE] in [ | ||
"output", | ||
"input", | ||
] | ||
assert data_point.sum > 0 | ||
|
||
if metric.name == Meters.LLM_OPERATION_DURATION: | ||
found_duration_metric = True | ||
assert any( | ||
data_point.count > 0 for data_point in metric.data.data_points | ||
) | ||
assert any( | ||
data_point.sum > 0 for data_point in metric.data.data_points | ||
) | ||
|
||
assert ( | ||
metric.data.data_points[0].attributes[SpanAttributes.LLM_SYSTEM] | ||
== "bedrock" | ||
) | ||
|
||
assert found_token_metric is True | ||
assert found_duration_metric is True |
1 change: 1 addition & 0 deletions
1
packages/opentelemetry-instrumentation-bedrock/tests/traces/__init__.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 @@ | ||
"""unit tests.""" |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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
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