-
Notifications
You must be signed in to change notification settings - Fork 45
feat: Enable sqs -> lambda support for DSM #604
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
purple4reina
merged 19 commits into
DataDog:main
from
michael-zhao459:michael.zhao/lambda-support
Jun 5, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ac374c2
set context for sqs-> lambda
michael-zhao459 db0c56c
needed queue name for consistent hash
michael-zhao459 c12e2dc
add context prop test
michael-zhao459 8f4f2d8
comment
michael-zhao459 e7ebe21
only import when DSM is enabled
michael-zhao459 67efeca
fix lint
michael-zhao459 2c94f07
fix
michael-zhao459 701ed35
fix
michael-zhao459 dd26482
refactorings
michael-zhao459 116b7d9
fix
michael-zhao459 6c8ec7f
Update datadog_lambda/dsm.py
michael-zhao459 9dbf2de
fix
michael-zhao459 779e6a5
fixes
michael-zhao459 e6a8b4e
fixes
michael-zhao459 7507357
test fixes
michael-zhao459 bbf219b
fix
michael-zhao459 254b864
fix
michael-zhao459 dda744e
unit test fixes
michael-zhao459 1f888b1
fixes
michael-zhao459 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,38 @@ | ||
from datadog_lambda import logger | ||
from datadog_lambda.trigger import EventTypes | ||
|
||
|
||
def set_dsm_context(event, event_source): | ||
|
||
if event_source.equals(EventTypes.SQS): | ||
_dsm_set_sqs_context(event) | ||
|
||
|
||
def _dsm_set_sqs_context(event): | ||
from datadog_lambda.wrapper import format_err_with_traceback | ||
from ddtrace.internal.datastreams import data_streams_processor | ||
from ddtrace.internal.datastreams.processor import DsmPathwayCodec | ||
from ddtrace.internal.datastreams.botocore import ( | ||
get_datastreams_context, | ||
calculate_sqs_payload_size, | ||
) | ||
|
||
records = event.get("Records") | ||
if records is None: | ||
return | ||
processor = data_streams_processor() | ||
|
||
for record in records: | ||
try: | ||
queue_arn = record.get("eventSourceARN", "") | ||
|
||
contextjson = get_datastreams_context(record) | ||
payload_size = calculate_sqs_payload_size(record) | ||
|
||
ctx = DsmPathwayCodec.decode(contextjson, processor) | ||
ctx.set_checkpoint( | ||
["direction:in", f"topic:{queue_arn}", "type:sqs"], | ||
payload_size=payload_size, | ||
) | ||
except Exception as e: | ||
logger.error(format_err_with_traceback(e)) |
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,112 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
|
||
from datadog_lambda.dsm import set_dsm_context, _dsm_set_sqs_context | ||
from datadog_lambda.trigger import EventTypes, _EventSource | ||
|
||
|
||
class TestDsmSQSContext(unittest.TestCase): | ||
def setUp(self): | ||
patcher = patch("datadog_lambda.dsm._dsm_set_sqs_context") | ||
self.mock_dsm_set_sqs_context = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
patcher = patch("ddtrace.internal.datastreams.data_streams_processor") | ||
self.mock_data_streams_processor = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
patcher = patch("ddtrace.internal.datastreams.botocore.get_datastreams_context") | ||
self.mock_get_datastreams_context = patcher.start() | ||
self.mock_get_datastreams_context.return_value = {} | ||
self.addCleanup(patcher.stop) | ||
|
||
patcher = patch( | ||
"ddtrace.internal.datastreams.botocore.calculate_sqs_payload_size" | ||
) | ||
self.mock_calculate_sqs_payload_size = patcher.start() | ||
self.mock_calculate_sqs_payload_size.return_value = 100 | ||
self.addCleanup(patcher.stop) | ||
|
||
patcher = patch("ddtrace.internal.datastreams.processor.DsmPathwayCodec.decode") | ||
self.mock_dsm_pathway_codec_decode = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
def test_non_sqs_event_source_does_nothing(self): | ||
"""Test that non-SQS event sources don't trigger DSM context setting""" | ||
event = {} | ||
# Use Unknown Event Source | ||
event_source = _EventSource(EventTypes.UNKNOWN) | ||
set_dsm_context(event, event_source) | ||
|
||
# DSM context should not be set for non-SQS events | ||
self.mock_dsm_set_sqs_context.assert_not_called() | ||
|
||
def test_sqs_event_with_no_records_does_nothing(self): | ||
"""Test that events where Records is None don't trigger DSM processing""" | ||
events_with_no_records = [ | ||
{}, | ||
{"Records": None}, | ||
{"someOtherField": "value"}, | ||
] | ||
|
||
for event in events_with_no_records: | ||
_dsm_set_sqs_context(event) | ||
self.mock_data_streams_processor.assert_not_called() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice. |
||
|
||
def test_sqs_event_triggers_dsm_sqs_context(self): | ||
"""Test that SQS event sources trigger the SQS-specific DSM context function""" | ||
sqs_event = { | ||
"Records": [ | ||
{ | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:my-queue", | ||
"body": "Hello from SQS!", | ||
} | ||
] | ||
} | ||
|
||
event_source = _EventSource(EventTypes.SQS) | ||
set_dsm_context(sqs_event, event_source) | ||
|
||
self.mock_dsm_set_sqs_context.assert_called_once_with(sqs_event) | ||
|
||
def test_sqs_multiple_records_process_each_record(self): | ||
"""Test that each record in an SQS event gets processed individually""" | ||
multi_record_event = { | ||
"Records": [ | ||
{ | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue1", | ||
"body": "Message 1", | ||
}, | ||
{ | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue2", | ||
"body": "Message 2", | ||
}, | ||
{ | ||
"eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:queue3", | ||
"body": "Message 3", | ||
}, | ||
] | ||
} | ||
|
||
mock_context = MagicMock() | ||
self.mock_dsm_pathway_codec_decode.return_value = mock_context | ||
|
||
_dsm_set_sqs_context(multi_record_event) | ||
|
||
self.assertEqual(mock_context.set_checkpoint.call_count, 3) | ||
|
||
calls = mock_context.set_checkpoint.call_args_list | ||
expected_arns = [ | ||
"arn:aws:sqs:us-east-1:123456789012:queue1", | ||
"arn:aws:sqs:us-east-1:123456789012:queue2", | ||
"arn:aws:sqs:us-east-1:123456789012:queue3", | ||
] | ||
|
||
for i, call in enumerate(calls): | ||
args, kwargs = call | ||
tags = args[0] | ||
self.assertIn("direction:in", tags) | ||
self.assertIn(f"topic:{expected_arns[i]}", tags) | ||
self.assertIn("type:sqs", tags) | ||
self.assertEqual(kwargs["payload_size"], 100) |
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 |
---|---|---|
|
@@ -76,6 +76,10 @@ def setUp(self): | |
self.mock_dd_lambda_layer_tag = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
patcher = patch("datadog_lambda.wrapper.set_dsm_context") | ||
self.mock_set_dsm_context = patcher.start() | ||
self.addCleanup(patcher.stop) | ||
|
||
def test_datadog_lambda_wrapper(self): | ||
wrapper.dd_tracing_enabled = False | ||
|
||
|
@@ -563,6 +567,62 @@ def return_type_test(event, context): | |
self.assertEqual(result, test_result) | ||
self.assertFalse(MockPrintExc.called) | ||
|
||
def test_set_dsm_context_called_when_DSM_and_tracing_enabled(self): | ||
os.environ["DD_DATA_STREAMS_ENABLED"] = "true" | ||
wrapper.dd_tracing_enabled = True | ||
|
||
@wrapper.datadog_lambda_wrapper | ||
def lambda_handler(event, context): | ||
return "ok" | ||
|
||
result = lambda_handler({}, get_mock_context()) | ||
self.assertEqual(result, "ok") | ||
self.mock_set_dsm_context.assert_called_once() | ||
|
||
del os.environ["DD_DATA_STREAMS_ENABLED"] | ||
|
||
def test_set_dsm_context_not_called_when_only_DSM_enabled(self): | ||
os.environ["DD_DATA_STREAMS_ENABLED"] = "true" | ||
wrapper.dd_tracing_enabled = False | ||
|
||
@wrapper.datadog_lambda_wrapper | ||
def lambda_handler(event, context): | ||
return "ok" | ||
|
||
result = lambda_handler({}, get_mock_context()) | ||
self.assertEqual(result, "ok") | ||
self.mock_set_dsm_context.assert_not_called() | ||
|
||
del os.environ["DD_DATA_STREAMS_ENABLED"] | ||
|
||
def test_set_dsm_context_not_called_when_only_tracing_enabled(self): | ||
os.environ["DD_DATA_STREAMS_ENABLED"] = "false" | ||
wrapper.dd_tracing_enabled = True | ||
|
||
@wrapper.datadog_lambda_wrapper | ||
def lambda_handler(event, context): | ||
return "ok" | ||
|
||
result = lambda_handler({}, get_mock_context()) | ||
self.assertEqual(result, "ok") | ||
self.mock_set_dsm_context.assert_not_called() | ||
|
||
del os.environ["DD_DATA_STREAMS_ENABLED"] | ||
|
||
def test_set_dsm_context_not_called_when_tracing_and_DSM_disabled(self): | ||
os.environ["DD_DATA_STREAMS_ENABLED"] = "false" | ||
wrapper.dd_tracing_enabled = False | ||
|
||
@wrapper.datadog_lambda_wrapper | ||
def lambda_handler(event, context): | ||
return "ok" | ||
|
||
result = lambda_handler({}, get_mock_context()) | ||
self.assertEqual(result, "ok") | ||
self.mock_set_dsm_context.assert_not_called() | ||
|
||
del os.environ["DD_DATA_STREAMS_ENABLED"] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These look great! Super easy to read and follow. |
||
|
||
class TestLambdaDecoratorSettings(unittest.TestCase): | ||
def test_some_envs_should_depend_on_dd_tracing_enabled(self): | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.