-
Notifications
You must be signed in to change notification settings - Fork 47
feat: add durable function execution tags to Lambda spans #728
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
Open
lym953
wants to merge
5
commits into
main
Choose a base branch
from
yiming.luo/durable-add-tags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2019 Datadog, Inc. | ||
| import logging | ||
| import re | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _parse_durable_execution_arn(arn): | ||
| """ | ||
| Parses a DurableExecutionArn to extract execution name and ID. | ||
| ARN format: | ||
| arn:aws:lambda:{region}:{account}:function:{func}:{version}/durable-execution/{name}/{id} | ||
| Returns (execution_name, execution_id) or None if parsing fails. | ||
| """ | ||
| match = re.search(r"/durable-execution/([^/]+)/([^/]+)$", arn) | ||
| if not match: | ||
| return None | ||
| execution_name, execution_id = match.group(1), match.group(2) | ||
| if not execution_name or not execution_id: | ||
| return None | ||
| return execution_name, execution_id | ||
|
|
||
|
|
||
| def extract_durable_function_tags(event): | ||
| """ | ||
| Extracts durable function tags from the Lambda event payload. | ||
| Returns a dict with durable function tags, or an empty dict if the event | ||
| is not a durable function invocation. | ||
| """ | ||
| if not isinstance(event, dict): | ||
| return {} | ||
|
|
||
| durable_execution_arn = event.get("DurableExecutionArn") | ||
| if not isinstance(durable_execution_arn, str): | ||
| return {} | ||
|
|
||
| parsed = _parse_durable_execution_arn(durable_execution_arn) | ||
| if not parsed: | ||
| logger.error("Failed to parse DurableExecutionArn: %s", durable_execution_arn) | ||
| return {} | ||
|
|
||
| execution_name, execution_id = parsed | ||
| return { | ||
| "durable_function_execution_name": execution_name, | ||
| "durable_function_execution_id": execution_id, | ||
| } | ||
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
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,91 @@ | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2019 Datadog, Inc. | ||
| import unittest | ||
|
|
||
| from datadog_lambda.durable import ( | ||
| _parse_durable_execution_arn, | ||
| extract_durable_function_tags, | ||
| ) | ||
|
|
||
|
|
||
| class TestParseDurableExecutionArn(unittest.TestCase): | ||
| def test_returns_name_and_id_for_valid_arn(self): | ||
| arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123/550e8400-e29b-41d4-a716-446655440001" | ||
| result = _parse_durable_execution_arn(arn) | ||
| self.assertEqual(result, ("order-123", "550e8400-e29b-41d4-a716-446655440001")) | ||
|
|
||
| def test_returns_none_for_arn_without_durable_execution_marker(self): | ||
| arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST" | ||
| result = _parse_durable_execution_arn(arn) | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_returns_none_for_malformed_arn_with_only_execution_name(self): | ||
| arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123" | ||
| result = _parse_durable_execution_arn(arn) | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_returns_none_for_malformed_arn_with_empty_execution_name(self): | ||
| arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution//550e8400-e29b-41d4-a716-446655440002" | ||
| result = _parse_durable_execution_arn(arn) | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_returns_none_for_malformed_arn_with_empty_execution_id(self): | ||
| arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:$LATEST/durable-execution/order-123/" | ||
| result = _parse_durable_execution_arn(arn) | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_works_with_numeric_version_qualifier(self): | ||
| arn = "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004" | ||
| result = _parse_durable_execution_arn(arn) | ||
| self.assertEqual( | ||
| result, ("my-execution", "550e8400-e29b-41d4-a716-446655440004") | ||
| ) | ||
|
|
||
|
|
||
| class TestExtractDurableFunctionTags(unittest.TestCase): | ||
| def test_extracts_tags_from_event_with_durable_execution_arn(self): | ||
| event = { | ||
| "DurableExecutionArn": "arn:aws:lambda:us-east-1:123456789012:function:my-func:1/durable-execution/my-execution/550e8400-e29b-41d4-a716-446655440004", | ||
| "CheckpointToken": "some-token", | ||
| "InitialExecutionState": {"Operations": []}, | ||
| } | ||
| result = extract_durable_function_tags(event) | ||
| self.assertEqual( | ||
| result, | ||
| { | ||
| "durable_function_execution_name": "my-execution", | ||
| "durable_function_execution_id": "550e8400-e29b-41d4-a716-446655440004", | ||
| }, | ||
| ) | ||
|
|
||
| def test_returns_empty_dict_for_regular_lambda_event(self): | ||
| event = { | ||
| "body": '{"key": "value"}', | ||
| "headers": {"Content-Type": "application/json"}, | ||
| } | ||
| result = extract_durable_function_tags(event) | ||
| self.assertEqual(result, {}) | ||
|
|
||
| def test_returns_empty_dict_when_event_is_none(self): | ||
| result = extract_durable_function_tags(None) | ||
| self.assertEqual(result, {}) | ||
|
|
||
| def test_returns_empty_dict_when_event_is_not_a_dict(self): | ||
| result = extract_durable_function_tags("not-a-dict") | ||
| self.assertEqual(result, {}) | ||
|
|
||
| def test_returns_empty_dict_when_durable_execution_arn_is_not_a_string(self): | ||
| event = {"DurableExecutionArn": 12345} | ||
| result = extract_durable_function_tags(event) | ||
| self.assertEqual(result, {}) | ||
|
|
||
| def test_returns_empty_dict_when_durable_execution_arn_cannot_be_parsed(self): | ||
| event = {"DurableExecutionArn": "invalid-arn-without-durable-execution-marker"} | ||
| result = extract_durable_function_tags(event) | ||
| self.assertEqual(result, {}) | ||
|
|
||
| def test_returns_empty_dict_when_event_is_empty(self): | ||
| result = extract_durable_function_tags({}) | ||
| self.assertEqual(result, {}) |
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.
Have we talked with APM team on tag convention?
Uh oh!
There was an error while loading. Please reload this page.
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.
Good point. Talked with @lucaspimentel on Slack. Although ideally we should use dots to separate the name into levels (e.g.
durable_function.execution_name), practically, since most of the other AWS Lambda tags (e.g.function_arn,function_version,functionname,init_type) are already on the root level, let's keep this convention and adddurable_function_execution_nameanddurable_function_execution_idon the root level.