Skip to content
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: PoC for adding lambda function trigger identifier #1906

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,43 @@ def _determine_parent_context(
return parent_context


def _get_trigger_info(
lambda_event: Any,
) -> str:
if not lambda_event or not isinstance(lambda_event, dict):
return

# elb
if lambda_event.get("headers"):
if "host" in lambda_event["headers"]:
return lambda_event["headers"]["host"]

# sns, sqs, s3
records = lambda_event.get("Records")
if not records or not isinstance(records, list) or not records[0]:
return

event_content = records[0]
event_source = event_content.get("eventSource")
if not event_source:
event_source = event_content.get("EventSource")
if not event_source:
return

if event_source == "aws:sns":
sns_tag = event_content.get("Sns")
if not sns_tag:
return
return sns_tag.get("TopicArn")
elif event_source == "aws:sqs":
return event_content.get("eventSourceARN")
elif event_source == "aws:s3":
s3_tag = event_content.get("s3")
if not s3_tag or not s3_tag["bucket"]:
return
return s3_tag["bucket"].get("arn")


def _determine_links() -> Optional[Sequence[Link]]:
"""Determine if a Link should be added to the Span based on the
environment variable `_X_AMZN_TRACE_ID`.
Expand Down Expand Up @@ -370,6 +407,11 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
result.get("statusCode"),
)

# Add information of trigger to span if available
trigger_id = _get_trigger_info(lambda_event)
if trigger_id:
span.set_attribute("faas.trigger_id", trigger_id)

now = time.time()
_tracer_provider = tracer_provider or get_tracer_provider()
if hasattr(_tracer_provider, "force_flush"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def __init__(self, aws_request_id, invoked_function_arn):
MOCK_W3C_TRACE_STATE_KEY = "vendor_specific_key"
MOCK_W3C_TRACE_STATE_VALUE = "test_value"

MOCK_TRIGGER_ARN = "arn:aws:trigger:us-east-2:123456789012:abc.xyz"
MOCK_HOST = "lambda-alb-123578498.us-east-1.elb.amazonaws.com"


def mock_execute_lambda(event=None):
"""Mocks the AWS Lambda execution.
Expand Down Expand Up @@ -428,3 +431,51 @@ def test_no_op_tracer_provider(self):
spans = self.memory_exporter.get_finished_spans()
assert spans is not None
self.assertEqual(len(spans), 0)

def test_set_trigger_info(self):
@dataclass
class TestCase:
name: str
expected_link_span_attr_val: str
event_payload: dict

tests = [
TestCase(
name="valid_sns_event",
expected_link_span_attr_val=MOCK_TRIGGER_ARN,
event_payload={"Records": [{"EventSource": "aws:sns", "Sns": {"TopicArn": MOCK_TRIGGER_ARN}}]},
),
TestCase(
name="valid_sqs_event",
expected_link_span_attr_val=MOCK_TRIGGER_ARN,
event_payload={"Records": [{"eventSource": "aws:sqs", "eventSourceARN": MOCK_TRIGGER_ARN}]},
),
TestCase(
name="valid_s3_event",
expected_link_span_attr_val=MOCK_TRIGGER_ARN,
event_payload={"Records": [{"eventSource": "aws:s3", "s3": {"bucket": {"arn": MOCK_TRIGGER_ARN}}}]},
),
TestCase(
name="valid_elb_event",
expected_link_span_attr_val=MOCK_HOST,
event_payload={"headers": {"host": MOCK_HOST}},
),
]
for test in tests:
AwsLambdaInstrumentor().instrument()
mock_execute_lambda(test.event_payload)
spans = self.memory_exporter.get_finished_spans()
assert spans

self.assertEqual(len(spans), 1)
span = spans[0]

self.assertSpanHasAttributes(
span,
{
"faas.trigger_id": test.expected_link_span_attr_val,
},
)

self.memory_exporter.clear()
AwsLambdaInstrumentor().uninstrument()
Loading