-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(bedrock): support metrics for bedrock #1957
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8579d61
Support metrics for bedrock
jinsongo 4f2e54c
Fix lint and build issues
jinsongo e17835a
Add metrics tests and adjust directory for traces
jinsongo 682d2d2
Update metrics tests and add cassettes file
jinsongo 934e99e
Add pytest_plugins at top level
jinsongo 6138f4a
Fix region_name and is_recording problem
jinsongo e2605e5
Use same session context for trace and metrics
jinsongo e1a2bc6
Remove whitespace for linting issues
jinsongo 2fffbb7
Fix exception counter name for bedrock
jinsongo 1580800
Remove the semantic variable by the comments
jinsongo 0f6c7f1
Change variable name by styling comments
jinsongo 8f08d53
Merge branch 'main' into bedrock-metrics
nirga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
245 changes: 206 additions & 39 deletions
245
...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 | | ||
MjAyNDA5MTlUMDgxNTMyWg== | ||
amz-sdk-invocation-id: | ||
- !!binary | | ||
YmNjNTcxNjYtYjRmOC00MzQzLTg4YmYtNWE4YzBhODBmZTM5 | ||
amz-sdk-request: | ||
- !!binary | | ||
YXR0ZW1wdD0x | ||
method: POST | ||
uri: https://bedrock-runtime.us-west-2.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 08:15:34 GMT | ||
X-Amzn-Bedrock-Input-Token-Count: | ||
- '9' | ||
X-Amzn-Bedrock-Invocation-Latency: | ||
- '1229' | ||
X-Amzn-Bedrock-Output-Token-Count: | ||
- '17' | ||
x-amzn-RequestId: | ||
- 6278b8bf-c1a7-46d7-822b-9f85ee7805b6 | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
42 changes: 42 additions & 0 deletions
42
packages/opentelemetry-instrumentation-bedrock/tests/metrics/conftest.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,42 @@ | ||
"""Unit tests configuration module.""" | ||
|
||
import pytest | ||
from opentelemetry import metrics | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.metrics import MeterProvider | ||
from opentelemetry.sdk.metrics.export import InMemoryMetricReader | ||
from opentelemetry.instrumentation.bedrock import BedrockInstrumentor | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def metrics_test_context(): | ||
resource = Resource.create() | ||
reader = InMemoryMetricReader() | ||
provider = MeterProvider(metric_readers=[reader], resource=resource) | ||
|
||
metrics.set_meter_provider(provider) | ||
|
||
# Without the following lines, span.is_recording() is False | ||
# so that _handle_call and _handle_stream_call will be skipped | ||
exporter = InMemorySpanExporter() | ||
processor = SimpleSpanProcessor(exporter) | ||
trace_provider = TracerProvider() | ||
trace_provider.add_span_processor(processor) | ||
trace.set_tracer_provider(trace_provider) | ||
|
||
BedrockInstrumentor(enrich_token_usage=True).instrument() | ||
|
||
return provider, reader | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def clear_metrics_test_context(metrics_test_context): | ||
provider, reader = metrics_test_context | ||
|
||
reader.shutdown() | ||
provider.shutdown() |
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(metrics_test_context, brt2): | ||
if brt2 is None: | ||
print("test_invoke_model_metrics test skipped.") | ||
return | ||
|
||
_, reader = metrics_test_context | ||
|
||
body = json.dumps( | ||
{ | ||
"inputText": "Tell me a joke about opentelemetry", | ||
"textGenerationConfig": { | ||
"maxTokenCount": 200, | ||
"temperature": 0.5, | ||
"topP": 0.5, | ||
}, | ||
} | ||
) | ||
|
||
brt2.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.
34 changes: 34 additions & 0 deletions
34
packages/opentelemetry-instrumentation-bedrock/tests/traces/conftest.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,34 @@ | ||
"""Unit tests configuration module.""" | ||
|
||
import pytest | ||
from opentelemetry import trace | ||
from opentelemetry.instrumentation.bedrock import BedrockInstrumentor | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def exporter(): | ||
exporter = InMemorySpanExporter() | ||
processor = SimpleSpanProcessor(exporter) | ||
|
||
provider = TracerProvider() | ||
provider.add_span_processor(processor) | ||
trace.set_tracer_provider(provider) | ||
|
||
return exporter | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def instrument(exporter): | ||
BedrockInstrumentor(enrich_token_usage=True).instrument() | ||
|
||
yield | ||
|
||
exporter.shutdown() | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def clear_exporter(exporter): | ||
exporter.clear() |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this required?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an open issue. I think it's better to use a common name, for example
llm.completions.exceptions
for all ai system to count exceptions. If yes, I would like to use another PR to fix all related code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jinsongo if we're doing this here I'd try to use the new genAI conventions - https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-metrics
I think they're already released so you can even use them directly from the original otel semconv package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(in this case - it means exceptions are counted in the duration metric)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nirga I cannot find an appropriate metric name for the exceptions counter of Bedrock. As you know, version 0.4.1 of opentelemetry-semantic-conventions-ai does not include
LLM_BEDROCK_COMPLETIONS_EXCEPTIONS
although it's already included in this current PR commits, which causes the build to fail when I use it in the Bedrock instrumentation code. Actually, that's the same approach for OpenAI, Authropic and Watsonx.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jinsongo this should be in the standard
opentelemetry-semantic-conventions
package. And you should count exceptions on the duration metric as specified in the spec.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jinsongo If this parameter is not required, I think we can ignore this for now, and we can fix this in future as a consolidation for semantic convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gyliu513 I have removed
LLM_BEDROCK_COMPLETIONS_EXCEPTIONS
fromsemconv_ai
, and added aTODO
comment about fixing in future as a consolidation for semantic convention.