|
1 | 1 | import unittest
|
| 2 | +import json |
| 3 | +import base64 |
2 | 4 | from unittest.mock import patch, MagicMock
|
3 | 5 |
|
4 |
| -from datadog_lambda.dsm import set_dsm_context, _dsm_set_sqs_context |
| 6 | +from datadog_lambda.dsm import ( |
| 7 | + set_dsm_context, |
| 8 | + _dsm_set_sqs_context, |
| 9 | + _get_dsm_context_from_lambda, |
| 10 | +) |
5 | 11 | from datadog_lambda.trigger import EventTypes, _EventSource
|
6 | 12 |
|
7 | 13 |
|
8 |
| -class TestDsmSQSContext(unittest.TestCase): |
| 14 | +class TestDSMContext(unittest.TestCase): |
9 | 15 | def setUp(self):
|
10 | 16 | patcher = patch("datadog_lambda.dsm._dsm_set_sqs_context")
|
11 | 17 | self.mock_dsm_set_sqs_context = patcher.start()
|
@@ -110,3 +116,230 @@ def test_sqs_multiple_records_process_each_record(self):
|
110 | 116 | self.assertIn(f"topic:{expected_arns[i]}", tags)
|
111 | 117 | self.assertIn("type:sqs", tags)
|
112 | 118 | self.assertEqual(kwargs["payload_size"], 100)
|
| 119 | + |
| 120 | + |
| 121 | +class TestGetDSMContext(unittest.TestCase): |
| 122 | + def test_sqs_to_lambda_string_value_format(self): |
| 123 | + """Test format: message.messageAttributes._datadog.stringValue (SQS -> lambda)""" |
| 124 | + trace_context = { |
| 125 | + "x-datadog-trace-id": "789123456", |
| 126 | + "x-datadog-parent-id": "321987654", |
| 127 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 128 | + } |
| 129 | + |
| 130 | + lambda_record = { |
| 131 | + "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d", |
| 132 | + "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...", |
| 133 | + "body": "Test message.", |
| 134 | + "attributes": { |
| 135 | + "ApproximateReceiveCount": "1", |
| 136 | + "SentTimestamp": "1545082649183", |
| 137 | + "SenderId": "AIDAIENQZJOLO23YVJ4VO", |
| 138 | + "ApproximateFirstReceiveTimestamp": "1545082649185", |
| 139 | + }, |
| 140 | + "messageAttributes": { |
| 141 | + "_datadog": { |
| 142 | + "stringValue": json.dumps(trace_context), |
| 143 | + "stringListValues": [], |
| 144 | + "binaryListValues": [], |
| 145 | + "dataType": "String", |
| 146 | + }, |
| 147 | + "myAttribute": { |
| 148 | + "stringValue": "myValue", |
| 149 | + "stringListValues": [], |
| 150 | + "binaryListValues": [], |
| 151 | + "dataType": "String", |
| 152 | + }, |
| 153 | + }, |
| 154 | + "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3", |
| 155 | + "eventSource": "aws:sqs", |
| 156 | + "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue", |
| 157 | + "awsRegion": "us-east-2", |
| 158 | + } |
| 159 | + |
| 160 | + result = _get_dsm_context_from_lambda(lambda_record) |
| 161 | + |
| 162 | + assert result is not None |
| 163 | + assert result == trace_context |
| 164 | + assert result["x-datadog-trace-id"] == "789123456" |
| 165 | + assert result["x-datadog-parent-id"] == "321987654" |
| 166 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 167 | + |
| 168 | + def test_sns_to_lambda_format(self): |
| 169 | + """Test format: message.Sns.MessageAttributes._datadog.Value.decode() (SNS -> lambda)""" |
| 170 | + trace_context = { |
| 171 | + "x-datadog-trace-id": "111111111", |
| 172 | + "x-datadog-parent-id": "222222222", |
| 173 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 174 | + } |
| 175 | + binary_data = base64.b64encode( |
| 176 | + json.dumps(trace_context).encode("utf-8") |
| 177 | + ).decode("utf-8") |
| 178 | + |
| 179 | + sns_lambda_record = { |
| 180 | + "EventSource": "aws:sns", |
| 181 | + "EventSubscriptionArn": ( |
| 182 | + "arn:aws:sns:us-east-1:123456789012:sns-topic:12345678-1234-1234-1234-123456789012" |
| 183 | + ), |
| 184 | + "Sns": { |
| 185 | + "Type": "Notification", |
| 186 | + "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", |
| 187 | + "TopicArn": "arn:aws:sns:us-east-1:123456789012:sns-topic", |
| 188 | + "Subject": "Test Subject", |
| 189 | + "Message": "Hello from SNS!", |
| 190 | + "Timestamp": "2023-01-01T12:00:00.000Z", |
| 191 | + "MessageAttributes": { |
| 192 | + "_datadog": {"Type": "Binary", "Value": binary_data} |
| 193 | + }, |
| 194 | + }, |
| 195 | + } |
| 196 | + |
| 197 | + result = _get_dsm_context_from_lambda(sns_lambda_record) |
| 198 | + |
| 199 | + assert result is not None |
| 200 | + assert result == trace_context |
| 201 | + assert result["x-datadog-trace-id"] == "111111111" |
| 202 | + assert result["x-datadog-parent-id"] == "222222222" |
| 203 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 204 | + |
| 205 | + def test_sns_to_sqs_to_lambda_binary_value_format(self): |
| 206 | + """Test format: message.messageAttributes._datadog.binaryValue.decode() (SNS -> SQS -> lambda, raw)""" |
| 207 | + trace_context = { |
| 208 | + "x-datadog-trace-id": "777666555", |
| 209 | + "x-datadog-parent-id": "444333222", |
| 210 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 211 | + } |
| 212 | + binary_data = base64.b64encode( |
| 213 | + json.dumps(trace_context).encode("utf-8") |
| 214 | + ).decode("utf-8") |
| 215 | + |
| 216 | + lambda_record = { |
| 217 | + "messageId": "test-message-id", |
| 218 | + "receiptHandle": "test-receipt-handle", |
| 219 | + "body": "Test message body", |
| 220 | + "messageAttributes": { |
| 221 | + "_datadog": {"binaryValue": binary_data, "dataType": "Binary"} |
| 222 | + }, |
| 223 | + "eventSource": "aws:sqs", |
| 224 | + "eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:test-queue", |
| 225 | + } |
| 226 | + |
| 227 | + result = _get_dsm_context_from_lambda(lambda_record) |
| 228 | + |
| 229 | + assert result is not None |
| 230 | + assert result == trace_context |
| 231 | + assert result["x-datadog-trace-id"] == "777666555" |
| 232 | + assert result["x-datadog-parent-id"] == "444333222" |
| 233 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 234 | + |
| 235 | + def test_sns_to_sqs_to_lambda_body_format(self): |
| 236 | + """Test format: message.body.MessageAttributes._datadog.Value.decode() (SNS -> SQS -> lambda)""" |
| 237 | + trace_context = { |
| 238 | + "x-datadog-trace-id": "123987456", |
| 239 | + "x-datadog-parent-id": "654321987", |
| 240 | + "x-datadog-sampling-priority": "1", |
| 241 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 242 | + } |
| 243 | + |
| 244 | + message_body = { |
| 245 | + "Type": "Notification", |
| 246 | + "MessageId": "test-message-id", |
| 247 | + "Message": "Test message from SNS", |
| 248 | + "MessageAttributes": { |
| 249 | + "_datadog": { |
| 250 | + "Type": "Binary", |
| 251 | + "Value": base64.b64encode( |
| 252 | + json.dumps(trace_context).encode("utf-8") |
| 253 | + ).decode("utf-8"), |
| 254 | + } |
| 255 | + }, |
| 256 | + } |
| 257 | + |
| 258 | + lambda_record = { |
| 259 | + "messageId": "lambda-message-id", |
| 260 | + "body": json.dumps(message_body), |
| 261 | + "eventSource": "aws:sqs", |
| 262 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:sns-to-sqs-queue", |
| 263 | + } |
| 264 | + |
| 265 | + result = _get_dsm_context_from_lambda(lambda_record) |
| 266 | + |
| 267 | + assert result is not None |
| 268 | + assert result == trace_context |
| 269 | + assert result["x-datadog-trace-id"] == "123987456" |
| 270 | + assert result["x-datadog-parent-id"] == "654321987" |
| 271 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 272 | + |
| 273 | + def test_kinesis_to_lambda_format(self): |
| 274 | + """Test format: message.kinesis.data.decode()._datadog (Kinesis -> lambda)""" |
| 275 | + trace_context = { |
| 276 | + "x-datadog-trace-id": "555444333", |
| 277 | + "x-datadog-parent-id": "888777666", |
| 278 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 279 | + } |
| 280 | + |
| 281 | + # Create the kinesis data payload |
| 282 | + kinesis_payload = { |
| 283 | + "_datadog": trace_context, |
| 284 | + "actualData": "some business data", |
| 285 | + } |
| 286 | + encoded_kinesis_data = base64.b64encode( |
| 287 | + json.dumps(kinesis_payload).encode("utf-8") |
| 288 | + ).decode("utf-8") |
| 289 | + |
| 290 | + kinesis_lambda_record = { |
| 291 | + "eventSource": "aws:kinesis", |
| 292 | + "eventSourceARN": ( |
| 293 | + "arn:aws:kinesis:us-east-1:123456789012:stream/my-stream" |
| 294 | + ), |
| 295 | + "kinesis": { |
| 296 | + "data": encoded_kinesis_data, |
| 297 | + "partitionKey": "partition-key-1", |
| 298 | + "sequenceNumber": ( |
| 299 | + "49590338271490256608559692538361571095921575989136588898" |
| 300 | + ), |
| 301 | + }, |
| 302 | + } |
| 303 | + |
| 304 | + result = _get_dsm_context_from_lambda(kinesis_lambda_record) |
| 305 | + |
| 306 | + assert result is not None |
| 307 | + assert result == trace_context |
| 308 | + assert result["x-datadog-trace-id"] == "555444333" |
| 309 | + assert result["x-datadog-parent-id"] == "888777666" |
| 310 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 311 | + |
| 312 | + def test_no_message_attributes(self): |
| 313 | + """Test message without MessageAttributes returns None.""" |
| 314 | + message = { |
| 315 | + "messageId": "test-message-id", |
| 316 | + "body": "Test message without attributes", |
| 317 | + } |
| 318 | + |
| 319 | + result = _get_dsm_context_from_lambda(message) |
| 320 | + |
| 321 | + assert result is None |
| 322 | + |
| 323 | + def test_no_datadog_attribute(self): |
| 324 | + """Test message with MessageAttributes but no _datadog attribute returns None.""" |
| 325 | + message = { |
| 326 | + "messageId": "test-message-id", |
| 327 | + "body": "Test message", |
| 328 | + "messageAttributes": { |
| 329 | + "customAttribute": {"stringValue": "custom-value", "dataType": "String"} |
| 330 | + }, |
| 331 | + } |
| 332 | + |
| 333 | + result = _get_dsm_context_from_lambda(message) |
| 334 | + assert result is None |
| 335 | + |
| 336 | + def test_empty_datadog_attribute(self): |
| 337 | + """Test message with empty _datadog attribute returns None.""" |
| 338 | + message = { |
| 339 | + "messageId": "test-message-id", |
| 340 | + "messageAttributes": {"_datadog": {}}, |
| 341 | + } |
| 342 | + |
| 343 | + result = _get_dsm_context_from_lambda(message) |
| 344 | + |
| 345 | + assert result is None |
0 commit comments