|
1 | 1 | import unittest |
| 2 | +import json |
| 3 | +import base64 |
2 | 4 | from unittest.mock import patch, MagicMock |
3 | 5 |
|
4 | 6 | from datadog_lambda.dsm import ( |
5 | 7 | set_dsm_context, |
6 | 8 | _dsm_set_sqs_context, |
7 | 9 | _dsm_set_sns_context, |
| 10 | + _get_dsm_context_from_lambda, |
8 | 11 | ) |
9 | 12 | from datadog_lambda.trigger import EventTypes, _EventSource |
10 | 13 |
|
@@ -203,3 +206,230 @@ def test_sns_multiple_records_process_each_record(self): |
203 | 206 | self.assertIn(f"topic:{expected_arns[i]}", tags) |
204 | 207 | self.assertIn("type:sns", tags) |
205 | 208 | self.assertEqual(kwargs["payload_size"], 150) |
| 209 | + |
| 210 | + |
| 211 | +class TestGetDSMContext(unittest.TestCase): |
| 212 | + def test_sqs_to_lambda_string_value_format(self): |
| 213 | + """Test format: message.messageAttributes._datadog.stringValue (SQS -> lambda)""" |
| 214 | + trace_context = { |
| 215 | + "x-datadog-trace-id": "789123456", |
| 216 | + "x-datadog-parent-id": "321987654", |
| 217 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 218 | + } |
| 219 | + |
| 220 | + lambda_record = { |
| 221 | + "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d", |
| 222 | + "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...", |
| 223 | + "body": "Test message.", |
| 224 | + "attributes": { |
| 225 | + "ApproximateReceiveCount": "1", |
| 226 | + "SentTimestamp": "1545082649183", |
| 227 | + "SenderId": "AIDAIENQZJOLO23YVJ4VO", |
| 228 | + "ApproximateFirstReceiveTimestamp": "1545082649185", |
| 229 | + }, |
| 230 | + "messageAttributes": { |
| 231 | + "_datadog": { |
| 232 | + "stringValue": json.dumps(trace_context), |
| 233 | + "stringListValues": [], |
| 234 | + "binaryListValues": [], |
| 235 | + "dataType": "String", |
| 236 | + }, |
| 237 | + "myAttribute": { |
| 238 | + "stringValue": "myValue", |
| 239 | + "stringListValues": [], |
| 240 | + "binaryListValues": [], |
| 241 | + "dataType": "String", |
| 242 | + }, |
| 243 | + }, |
| 244 | + "md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3", |
| 245 | + "eventSource": "aws:sqs", |
| 246 | + "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue", |
| 247 | + "awsRegion": "us-east-2", |
| 248 | + } |
| 249 | + |
| 250 | + result = _get_dsm_context_from_lambda(lambda_record) |
| 251 | + |
| 252 | + assert result is not None |
| 253 | + assert result == trace_context |
| 254 | + assert result["x-datadog-trace-id"] == "789123456" |
| 255 | + assert result["x-datadog-parent-id"] == "321987654" |
| 256 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 257 | + |
| 258 | + def test_sns_to_lambda_format(self): |
| 259 | + """Test format: message.Sns.MessageAttributes._datadog.Value.decode() (SNS -> lambda)""" |
| 260 | + trace_context = { |
| 261 | + "x-datadog-trace-id": "111111111", |
| 262 | + "x-datadog-parent-id": "222222222", |
| 263 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 264 | + } |
| 265 | + binary_data = base64.b64encode( |
| 266 | + json.dumps(trace_context).encode("utf-8") |
| 267 | + ).decode("utf-8") |
| 268 | + |
| 269 | + sns_lambda_record = { |
| 270 | + "EventSource": "aws:sns", |
| 271 | + "EventSubscriptionArn": ( |
| 272 | + "arn:aws:sns:us-east-1:123456789012:sns-topic:12345678-1234-1234-1234-123456789012" |
| 273 | + ), |
| 274 | + "Sns": { |
| 275 | + "Type": "Notification", |
| 276 | + "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", |
| 277 | + "TopicArn": "arn:aws:sns:us-east-1:123456789012:sns-topic", |
| 278 | + "Subject": "Test Subject", |
| 279 | + "Message": "Hello from SNS!", |
| 280 | + "Timestamp": "2023-01-01T12:00:00.000Z", |
| 281 | + "MessageAttributes": { |
| 282 | + "_datadog": {"Type": "Binary", "Value": binary_data} |
| 283 | + }, |
| 284 | + }, |
| 285 | + } |
| 286 | + |
| 287 | + result = _get_dsm_context_from_lambda(sns_lambda_record) |
| 288 | + |
| 289 | + assert result is not None |
| 290 | + assert result == trace_context |
| 291 | + assert result["x-datadog-trace-id"] == "111111111" |
| 292 | + assert result["x-datadog-parent-id"] == "222222222" |
| 293 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 294 | + |
| 295 | + def test_sns_to_sqs_to_lambda_binary_value_format(self): |
| 296 | + """Test format: message.messageAttributes._datadog.binaryValue.decode() (SNS -> SQS -> lambda, raw)""" |
| 297 | + trace_context = { |
| 298 | + "x-datadog-trace-id": "777666555", |
| 299 | + "x-datadog-parent-id": "444333222", |
| 300 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 301 | + } |
| 302 | + binary_data = base64.b64encode( |
| 303 | + json.dumps(trace_context).encode("utf-8") |
| 304 | + ).decode("utf-8") |
| 305 | + |
| 306 | + lambda_record = { |
| 307 | + "messageId": "test-message-id", |
| 308 | + "receiptHandle": "test-receipt-handle", |
| 309 | + "body": "Test message body", |
| 310 | + "messageAttributes": { |
| 311 | + "_datadog": {"binaryValue": binary_data, "dataType": "Binary"} |
| 312 | + }, |
| 313 | + "eventSource": "aws:sqs", |
| 314 | + "eventSourceARN": "arn:aws:sqs:us-west-2:123456789012:test-queue", |
| 315 | + } |
| 316 | + |
| 317 | + result = _get_dsm_context_from_lambda(lambda_record) |
| 318 | + |
| 319 | + assert result is not None |
| 320 | + assert result == trace_context |
| 321 | + assert result["x-datadog-trace-id"] == "777666555" |
| 322 | + assert result["x-datadog-parent-id"] == "444333222" |
| 323 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 324 | + |
| 325 | + def test_sns_to_sqs_to_lambda_body_format(self): |
| 326 | + """Test format: message.body.MessageAttributes._datadog.Value.decode() (SNS -> SQS -> lambda)""" |
| 327 | + trace_context = { |
| 328 | + "x-datadog-trace-id": "123987456", |
| 329 | + "x-datadog-parent-id": "654321987", |
| 330 | + "x-datadog-sampling-priority": "1", |
| 331 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 332 | + } |
| 333 | + |
| 334 | + message_body = { |
| 335 | + "Type": "Notification", |
| 336 | + "MessageId": "test-message-id", |
| 337 | + "Message": "Test message from SNS", |
| 338 | + "MessageAttributes": { |
| 339 | + "_datadog": { |
| 340 | + "Type": "Binary", |
| 341 | + "Value": base64.b64encode( |
| 342 | + json.dumps(trace_context).encode("utf-8") |
| 343 | + ).decode("utf-8"), |
| 344 | + } |
| 345 | + }, |
| 346 | + } |
| 347 | + |
| 348 | + lambda_record = { |
| 349 | + "messageId": "lambda-message-id", |
| 350 | + "body": json.dumps(message_body), |
| 351 | + "eventSource": "aws:sqs", |
| 352 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:sns-to-sqs-queue", |
| 353 | + } |
| 354 | + |
| 355 | + result = _get_dsm_context_from_lambda(lambda_record) |
| 356 | + |
| 357 | + assert result is not None |
| 358 | + assert result == trace_context |
| 359 | + assert result["x-datadog-trace-id"] == "123987456" |
| 360 | + assert result["x-datadog-parent-id"] == "654321987" |
| 361 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 362 | + |
| 363 | + def test_kinesis_to_lambda_format(self): |
| 364 | + """Test format: message.kinesis.data.decode()._datadog (Kinesis -> lambda)""" |
| 365 | + trace_context = { |
| 366 | + "x-datadog-trace-id": "555444333", |
| 367 | + "x-datadog-parent-id": "888777666", |
| 368 | + "dd-pathway-ctx": "test-pathway-ctx", |
| 369 | + } |
| 370 | + |
| 371 | + # Create the kinesis data payload |
| 372 | + kinesis_payload = { |
| 373 | + "_datadog": trace_context, |
| 374 | + "actualData": "some business data", |
| 375 | + } |
| 376 | + encoded_kinesis_data = base64.b64encode( |
| 377 | + json.dumps(kinesis_payload).encode("utf-8") |
| 378 | + ).decode("utf-8") |
| 379 | + |
| 380 | + kinesis_lambda_record = { |
| 381 | + "eventSource": "aws:kinesis", |
| 382 | + "eventSourceARN": ( |
| 383 | + "arn:aws:kinesis:us-east-1:123456789012:stream/my-stream" |
| 384 | + ), |
| 385 | + "kinesis": { |
| 386 | + "data": encoded_kinesis_data, |
| 387 | + "partitionKey": "partition-key-1", |
| 388 | + "sequenceNumber": ( |
| 389 | + "49590338271490256608559692538361571095921575989136588898" |
| 390 | + ), |
| 391 | + }, |
| 392 | + } |
| 393 | + |
| 394 | + result = _get_dsm_context_from_lambda(kinesis_lambda_record) |
| 395 | + |
| 396 | + assert result is not None |
| 397 | + assert result == trace_context |
| 398 | + assert result["x-datadog-trace-id"] == "555444333" |
| 399 | + assert result["x-datadog-parent-id"] == "888777666" |
| 400 | + assert result["dd-pathway-ctx"] == "test-pathway-ctx" |
| 401 | + |
| 402 | + def test_no_message_attributes(self): |
| 403 | + """Test message without MessageAttributes returns None.""" |
| 404 | + message = { |
| 405 | + "messageId": "test-message-id", |
| 406 | + "body": "Test message without attributes", |
| 407 | + } |
| 408 | + |
| 409 | + result = _get_dsm_context_from_lambda(message) |
| 410 | + |
| 411 | + assert result is None |
| 412 | + |
| 413 | + def test_no_datadog_attribute(self): |
| 414 | + """Test message with MessageAttributes but no _datadog attribute returns None.""" |
| 415 | + message = { |
| 416 | + "messageId": "test-message-id", |
| 417 | + "body": "Test message", |
| 418 | + "messageAttributes": { |
| 419 | + "customAttribute": {"stringValue": "custom-value", "dataType": "String"} |
| 420 | + }, |
| 421 | + } |
| 422 | + |
| 423 | + result = _get_dsm_context_from_lambda(message) |
| 424 | + assert result is None |
| 425 | + |
| 426 | + def test_empty_datadog_attribute(self): |
| 427 | + """Test message with empty _datadog attribute returns None.""" |
| 428 | + message = { |
| 429 | + "messageId": "test-message-id", |
| 430 | + "messageAttributes": {"_datadog": {}}, |
| 431 | + } |
| 432 | + |
| 433 | + result = _get_dsm_context_from_lambda(message) |
| 434 | + |
| 435 | + assert result is None |
0 commit comments