Skip to content

Commit 095c109

Browse files
authored
propagate Lambda span attribute cloud.resource_id to child spans (#284)
*Issue #, if available:* AppSignals lambda dependency metrics don't show Lambda Alias correctly *Description of changes:* Propagate span attribute cloud.resource_id if there is. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent de63a81 commit 095c109

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def get_ingress_operation(__, span: ReadableSpan) -> str:
5252
"""
5353
operation: str = span.name
5454
if _AWS_LAMBDA_FUNCTION_NAME in os.environ:
55-
operation = os.environ.get(_AWS_LAMBDA_FUNCTION_NAME) + "/Handler"
55+
operation = os.environ.get(_AWS_LAMBDA_FUNCTION_NAME) + "/FunctionHandler"
5656
elif should_use_internal_operation(span):
5757
operation = INTERNAL_OPERATION
5858
elif not _is_valid_operation(span, operation):

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from amazon.opentelemetry.distro._aws_span_processing_util import is_aws_sdk_span, is_local_root
99
from opentelemetry.context import Context
1010
from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor
11+
from opentelemetry.semconv.trace import SpanAttributes
1112
from opentelemetry.trace import SpanKind
1213
from opentelemetry.trace.propagation import get_current_span
1314

@@ -62,6 +63,12 @@ def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None
6263
if _is_consumer_kind(span) and _is_consumer_kind(parent_span):
6364
span.set_attribute(AWS_CONSUMER_PARENT_SPAN_KIND, parent_span.kind.name)
6465

66+
# Propagate span attribute cloud.resource_id for extracting lambda alias for dependency metrics.
67+
parent_resource_id = parent_span.attributes.get(SpanAttributes.CLOUD_RESOURCE_ID)
68+
current_resource_id = span.attributes.get(SpanAttributes.CLOUD_RESOURCE_ID)
69+
if current_resource_id is None and parent_resource_id is not None:
70+
span.set_attribute(SpanAttributes.CLOUD_RESOURCE_ID, parent_resource_id)
71+
6572
propagation_data: str = None
6673
if is_local_root(span):
6774
if not _is_server_kind(span):

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,26 @@ def _validate_span_attributes_inheritance(
195195
self.assertEqual(propagation_value2, leaf_span.attributes.get(_TEST_KEY_2))
196196
else:
197197
self.assertIsNone(leaf_span.attributes.get(_TEST_KEY_2))
198+
199+
def test_attributes_propagation_cloud_resource_id(self):
200+
cloud_resource_id = "arn:x1"
201+
grand_parent_span: Span = self.tracer.start_span(
202+
name="grandparent", kind=SpanKind.INTERNAL, attributes={_TEST_KEY_1: "testValue1"}
203+
)
204+
parent_span: Span = self.tracer.start_span(
205+
name="parent",
206+
kind=SpanKind.SERVER,
207+
attributes={_TEST_KEY_2: "testValue2", SpanAttributes.CLOUD_RESOURCE_ID: cloud_resource_id},
208+
context=set_span_in_context(grand_parent_span),
209+
)
210+
child_span: Span = self.tracer.start_span(
211+
name="child", kind=SpanKind.INTERNAL, context=set_span_in_context(parent_span)
212+
)
213+
grand_child_span: Span = self.tracer.start_span(
214+
name="child", kind=SpanKind.CLIENT, context=set_span_in_context(child_span)
215+
)
216+
217+
self.assertIsNone(grand_parent_span.attributes.get(SpanAttributes.CLOUD_RESOURCE_ID))
218+
self.assertIsNotNone(parent_span.attributes.get(SpanAttributes.CLOUD_RESOURCE_ID))
219+
self.assertEqual(child_span.attributes.get(SpanAttributes.CLOUD_RESOURCE_ID), cloud_resource_id)
220+
self.assertEqual(grand_child_span.attributes.get(SpanAttributes.CLOUD_RESOURCE_ID), cloud_resource_id)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_get_ingress_operation_in_lambda(self):
6262
self.span_data_mock.name = valid_name
6363
self.span_data_mock.kind = SpanKind.SERVER
6464
actual_operation: str = get_ingress_operation(self, self.span_data_mock)
65-
self.assertEqual(actual_operation, "MyLambda/Handler")
65+
self.assertEqual(actual_operation, "MyLambda/FunctionHandler")
6666

6767
def test_get_ingress_operation_http_method_name_and_no_fallback(self):
6868
invalid_name: str = "GET"

0 commit comments

Comments
 (0)