-
Notifications
You must be signed in to change notification settings - Fork 44
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
Changes from 18 commits
ac374c2
db0c56c
c12e2dc
8f4f2d8
e7ebe21
67efeca
2c94f07
701ed35
dd26482
116b7d9
6c8ec7f
9dbf2de
779e6a5
e6a8b4e
7507357
bbf219b
254b864
dda744e
1f888b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
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 | ||
|
||
|
||
class TestDsmContext(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 = {"Records": [{"body": "test"}]} | ||
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. nit: Just to make this a tad less confusing, this event object sure looks like an sqs event to me. The test of course passes bc we look at the event source, not the event for its type. That said, can we make this event something like |
||
|
||
mock_event_source = MagicMock() | ||
mock_event_source.equals.return_value = False # Not SQS | ||
|
||
set_dsm_context(event, mock_event_source) | ||
|
||
mock_event_source.equals.assert_called_once_with(EventTypes.SQS) | ||
self.mock_dsm_set_sqs_context.assert_not_called() | ||
|
||
def test_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!", | ||
} | ||
] | ||
} | ||
|
||
mock_event_source = MagicMock() | ||
mock_event_source.equals.return_value = True | ||
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. I'm thinking toward the future, where you'll be adding support for sns and kinesis, etc. Once those are in place, you're not gonna want Basically, this code works just great right now. But your future self is gonna have to change it, so why not make it work right the first time. Instead, it's pretty easy to create a workable event source object. Instead of a mock, what about something like: event_source = _EventSource(EventTypes.SQS)
set_dsm_context(sqs_event, event_source) Make that change to remove all the |
||
|
||
set_dsm_context(sqs_event, mock_event_source) | ||
|
||
self.mock_dsm_set_sqs_context.assert_called_once_with(sqs_event) | ||
|
||
def test_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) |
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): | ||
|
Uh oh!
There was an error while loading. Please reload this page.