Skip to content

Commit 779392e

Browse files
committed
refactor: extract guardrail remote id from cloudformation primary id
1 parent 446a285 commit 779392e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_aws_metric_attribute_generator.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ def _set_remote_type_and_identifier(span: ReadableSpan, attributes: BoundedAttri
423423
)
424424
elif is_key_present(span, AWS_BEDROCK_GUARDRAIL_ID):
425425
remote_resource_type = _NORMALIZED_BEDROCK_SERVICE_NAME + "::Guardrail"
426-
remote_resource_identifier = _escape_delimiters(span.attributes.get(AWS_BEDROCK_GUARDRAIL_ID))
426+
remote_resource_identifier = _getBedrockIdentifierFromArn(
427+
_escape_delimiters(span.attributes.get(AWS_BEDROCK_GUARDRAIL_ARN))
428+
)
427429
cloudformation_primary_identifier = _escape_delimiters(span.attributes.get(AWS_BEDROCK_GUARDRAIL_ARN))
428430
elif is_key_present(span, AWS_BEDROCK_KNOWLEDGE_BASE_ID):
429431
remote_resource_type = _NORMALIZED_BEDROCK_SERVICE_NAME + "::KnowledgeBase"
@@ -480,6 +482,12 @@ def _set_remote_type_and_identifier(span: ReadableSpan, attributes: BoundedAttri
480482
attributes[AWS_CLOUDFORMATION_PRIMARY_IDENTIFIER] = cloudformation_primary_identifier
481483

482484

485+
def _getBedrockIdentifierFromArn(arn: str):
486+
# NOTE: Bedrock Guardrail ARNs follow this pattern:
487+
# arn:aws:bedrock:<region>:<account_id>:guardrail/<identifier>
488+
return arn.split(":")[-1].split("/")[-1]
489+
490+
483491
def _get_db_connection(span: ReadableSpan) -> None:
484492
"""
485493
RemoteResourceIdentifier is populated with rule:

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_metric_attribute_generator.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from amazon.opentelemetry.distro._aws_attribute_keys import (
1111
AWS_BEDROCK_AGENT_ID,
1212
AWS_BEDROCK_DATA_SOURCE_ID,
13+
AWS_BEDROCK_GUARDRAIL_ARN,
1314
AWS_BEDROCK_GUARDRAIL_ID,
1415
AWS_BEDROCK_KNOWLEDGE_BASE_ID,
1516
AWS_CONSUMER_PARENT_SPAN_KIND,
@@ -1073,14 +1074,24 @@ def test_sdk_client_span_with_remote_resource_attributes(self):
10731074
self._mock_attribute([AWS_BEDROCK_DATA_SOURCE_ID, AWS_BEDROCK_KNOWLEDGE_BASE_ID], [None, None])
10741075

10751076
# Validate behaviour of AWS_BEDROCK_GUARDRAIL_ID attribute, then remove it.
1076-
self._mock_attribute([AWS_BEDROCK_GUARDRAIL_ID], ["test_guardrail_id"], keys, values)
1077+
self._mock_attribute(
1078+
[AWS_BEDROCK_GUARDRAIL_ID, AWS_BEDROCK_GUARDRAIL_ARN],
1079+
["test_guardrail_id", "arn:aws:bedrock:us-west-2:123456789012:guardrail/test_guardrail_id"],
1080+
keys,
1081+
values,
1082+
)
10771083
self._validate_remote_resource_attributes("AWS::Bedrock::Guardrail", "test_guardrail_id")
1078-
self._mock_attribute([AWS_BEDROCK_GUARDRAIL_ID], [None])
1084+
self._mock_attribute([AWS_BEDROCK_GUARDRAIL_ID, AWS_BEDROCK_GUARDRAIL_ARN], [None, None])
10791085

10801086
# Validate behaviour of AWS_BEDROCK_GUARDRAIL_ID attribute with special chars(^), then remove it.
1081-
self._mock_attribute([AWS_BEDROCK_GUARDRAIL_ID], ["test_guardrail_^id"], keys, values)
1087+
self._mock_attribute(
1088+
[AWS_BEDROCK_GUARDRAIL_ID, AWS_BEDROCK_GUARDRAIL_ARN],
1089+
["test_guardrail_^id", "arn:aws:bedrock:us-west-2:123456789012:guardrail/test_guardrail_^id"],
1090+
keys,
1091+
values,
1092+
)
10821093
self._validate_remote_resource_attributes("AWS::Bedrock::Guardrail", "test_guardrail_^^id")
1083-
self._mock_attribute([AWS_BEDROCK_GUARDRAIL_ID], [None])
1094+
self._mock_attribute([AWS_BEDROCK_GUARDRAIL_ID, AWS_BEDROCK_GUARDRAIL_ARN], [None, None])
10841095

10851096
# Validate behaviour of AWS_BEDROCK_KNOWLEDGE_BASE_ID attribute, then remove it.
10861097
self._mock_attribute([AWS_BEDROCK_KNOWLEDGE_BASE_ID], ["test_knowledgeBase_id"], keys, values)

0 commit comments

Comments
 (0)