generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 26
feat: Add botocore instrumentation extension for Bedrock AgentCore services #490
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be08bab
add new bedrock agentcore patching and attributes
98d567a
add changelog
52744ef
Merge branch 'main' into bedrock-agentcore-resource-support
liustve 17716ac
lint fix
25f91ac
fix arn names
9e39da9
fix arn namse in test
9c06e50
Merge branch 'main' into bedrock-agentcore-resource-support
liustve 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
This file contains hidden or 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 hidden or 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
92 changes: 92 additions & 0 deletions
92
...pentelemetry-distro/src/amazon/opentelemetry/distro/patches/_bedrock_agentcore_patches.py
This file contains hidden or 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,92 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from typing import Any, Dict | ||
|
||
from amazon.opentelemetry.distro._aws_attribute_keys import ( | ||
AWS_AUTH_CREDENTIAL_PROVIDER_ARN, | ||
AWS_BEDROCK_AGENTCORE_BROWSER_ARN, | ||
AWS_BEDROCK_AGENTCORE_CODE_INTERPRETER_ARN, | ||
AWS_BEDROCK_AGENTCORE_GATEWAY_ARN, | ||
AWS_BEDROCK_AGENTCORE_MEMORY_ARN, | ||
AWS_BEDROCK_AGENTCORE_RUNTIME_ARN, | ||
AWS_BEDROCK_AGENTCORE_RUNTIME_ENDPOINT_ARN, | ||
AWS_BEDROCK_AGENTCORE_WORKLOAD_IDENTITY_ARN, | ||
AWS_GATEWAY_TARGET_ID, | ||
) | ||
from opentelemetry.instrumentation.botocore.extensions.types import ( | ||
_AttributeMapT, | ||
_AwsSdkExtension, | ||
_BotocoreInstrumentorContext, | ||
_BotoResultT, | ||
) | ||
from opentelemetry.trace.span import Span | ||
|
||
GEN_AI_RUNTIME_ID = "gen_ai.runtime.id" | ||
GEN_AI_BROWSER_ID = "gen_ai.browser.id" | ||
GEN_AI_CODE_INTERPRETER_ID = "gen_ai.code_interpreter.id" | ||
GEN_AI_MEMORY_ID = "gen_ai.memory.id" | ||
GEN_AI_GATEWAY_ID = "gen_ai.gateway.id" | ||
|
||
# Mapping of flattened JSON paths to attribute keys | ||
_ATTRIBUTE_MAPPING = { | ||
"agentRuntimeArn": AWS_BEDROCK_AGENTCORE_RUNTIME_ARN, | ||
"agentRuntimeEndpointArn": AWS_BEDROCK_AGENTCORE_RUNTIME_ENDPOINT_ARN, | ||
"agentRuntimeId": GEN_AI_RUNTIME_ID, | ||
"browserArn": AWS_BEDROCK_AGENTCORE_BROWSER_ARN, | ||
"browserId": GEN_AI_BROWSER_ID, | ||
"browserIdentifier": GEN_AI_BROWSER_ID, | ||
"codeInterpreterArn": AWS_BEDROCK_AGENTCORE_CODE_INTERPRETER_ARN, | ||
"codeInterpreterId": GEN_AI_CODE_INTERPRETER_ID, | ||
"codeInterpreterIdentifier": GEN_AI_CODE_INTERPRETER_ID, | ||
"gatewayArn": AWS_BEDROCK_AGENTCORE_GATEWAY_ARN, | ||
"gatewayId": GEN_AI_GATEWAY_ID, | ||
"gatewayIdentifier": GEN_AI_GATEWAY_ID, | ||
"targetId": AWS_GATEWAY_TARGET_ID, | ||
"memory.arn": AWS_BEDROCK_AGENTCORE_MEMORY_ARN, | ||
"memory.id": GEN_AI_MEMORY_ID, | ||
"memoryId": GEN_AI_MEMORY_ID, | ||
"credentialProviderArn": AWS_AUTH_CREDENTIAL_PROVIDER_ARN, | ||
"workloadIdentityArn": AWS_BEDROCK_AGENTCORE_WORKLOAD_IDENTITY_ARN, | ||
"workloadIdentityDetails.workloadIdentityArn": AWS_BEDROCK_AGENTCORE_WORKLOAD_IDENTITY_ARN, | ||
} | ||
|
||
|
||
class _BedrockAgentCoreExtension(_AwsSdkExtension): | ||
def extract_attributes(self, attributes: _AttributeMapT): | ||
extracted_attrs = self._extract_attributes(self._call_context.params) | ||
attributes.update(extracted_attrs) | ||
|
||
def on_success( | ||
self, | ||
span: Span, | ||
result: _BotoResultT, | ||
instrumentor_context: _BotocoreInstrumentorContext, | ||
): | ||
if span is None or not span.is_recording(): | ||
return | ||
|
||
extracted_attrs = self._extract_attributes(result) | ||
for attr_name, attr_value in extracted_attrs.items(): | ||
span.set_attribute(attr_name, attr_value) | ||
|
||
@staticmethod | ||
def _extract_attributes(params: Dict[str, Any]): | ||
"""Extracts all Bedrock AgentCore attributes using mapping-based traversal""" | ||
attrs = {} | ||
for path, attr_key in _ATTRIBUTE_MAPPING.items(): | ||
value = _BedrockAgentCoreExtension._get_nested_value(params, path) | ||
if value: | ||
attrs[attr_key] = value | ||
return attrs | ||
|
||
@staticmethod | ||
def _get_nested_value(data: Dict[str, Any], path: str): | ||
"""Get value from nested dictionary using dot notation path""" | ||
keys = path.split(".") | ||
value = data | ||
for key in keys: | ||
if isinstance(value, dict) and key in value: | ||
value = value[key] | ||
else: | ||
return None | ||
liustve marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return value |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.