|
12 | 12 | _select_metrics_handler, |
13 | 13 | flush_stats, |
14 | 14 | lambda_metric, |
| 15 | + submit_batch_item_failures_metric, |
15 | 16 | ) |
16 | 17 | from datadog_lambda.tags import dd_lambda_layer_tag |
17 | 18 | from datadog_lambda.thread_stats_writer import ThreadStatsWriter |
@@ -324,3 +325,80 @@ def decrypt(self, CiphertextBlob=None, EncryptionContext={}): |
324 | 325 | mock_kms_client, MOCK_ENCRYPTED_API_KEY_BASE64 |
325 | 326 | ) |
326 | 327 | self.assertEqual(decrypted_key, EXPECTED_DECRYPTED_API_KEY) |
| 328 | + |
| 329 | + |
| 330 | +class TestBatchItemFailuresMetric(unittest.TestCase): |
| 331 | + def setUp(self): |
| 332 | + patcher = patch("datadog_lambda.metric.lambda_metric") |
| 333 | + self.mock_lambda_metric = patcher.start() |
| 334 | + self.addCleanup(patcher.stop) |
| 335 | + |
| 336 | + patcher = patch("datadog_lambda.config.Config.enhanced_metrics_enabled", True) |
| 337 | + self.mock_enhanced_metrics_enabled = patcher.start() |
| 338 | + self.addCleanup(patcher.stop) |
| 339 | + |
| 340 | + def test_submit_batch_item_failures_with_failures(self): |
| 341 | + response = { |
| 342 | + "batchItemFailures": [ |
| 343 | + {"itemIdentifier": "msg-1"}, |
| 344 | + {"itemIdentifier": "msg-2"}, |
| 345 | + {"itemIdentifier": "msg-3"}, |
| 346 | + ] |
| 347 | + } |
| 348 | + context = unittest.mock.Mock() |
| 349 | + |
| 350 | + with patch("datadog_lambda.metric.get_enhanced_metrics_tags") as mock_get_tags: |
| 351 | + mock_get_tags.return_value = ["tag1:value1"] |
| 352 | + submit_batch_item_failures_metric(response, context) |
| 353 | + |
| 354 | + self.mock_lambda_metric.assert_called_once_with( |
| 355 | + "aws.lambda.enhanced.batch_item_failures", |
| 356 | + 3, |
| 357 | + timestamp=None, |
| 358 | + tags=["tag1:value1"], |
| 359 | + force_async=True, |
| 360 | + ) |
| 361 | + |
| 362 | + def test_submit_batch_item_failures_with_no_failures(self): |
| 363 | + response = {"batchItemFailures": []} |
| 364 | + context = unittest.mock.Mock() |
| 365 | + |
| 366 | + with patch("datadog_lambda.metric.get_enhanced_metrics_tags") as mock_get_tags: |
| 367 | + mock_get_tags.return_value = ["tag1:value1"] |
| 368 | + submit_batch_item_failures_metric(response, context) |
| 369 | + self.mock_lambda_metric.assert_called_once_with( |
| 370 | + "aws.lambda.enhanced.batch_item_failures", |
| 371 | + 0, |
| 372 | + timestamp=None, |
| 373 | + tags=["tag1:value1"], |
| 374 | + force_async=True, |
| 375 | + ) |
| 376 | + |
| 377 | + def test_submit_batch_item_failures_with_no_field(self): |
| 378 | + response = {"statusCode": 200} |
| 379 | + context = unittest.mock.Mock() |
| 380 | + submit_batch_item_failures_metric(response, context) |
| 381 | + self.mock_lambda_metric.assert_not_called() |
| 382 | + |
| 383 | + def test_submit_batch_item_failures_with_none_response(self): |
| 384 | + response = None |
| 385 | + context = unittest.mock.Mock() |
| 386 | + submit_batch_item_failures_metric(response, context) |
| 387 | + self.mock_lambda_metric.assert_not_called() |
| 388 | + |
| 389 | + def test_submit_batch_item_failures_with_non_list_value(self): |
| 390 | + response = {"batchItemFailures": "invalid"} |
| 391 | + context = unittest.mock.Mock() |
| 392 | + submit_batch_item_failures_metric(response, context) |
| 393 | + self.mock_lambda_metric.assert_not_called() |
| 394 | + |
| 395 | + @patch("datadog_lambda.config.Config.enhanced_metrics_enabled", False) |
| 396 | + def test_submit_batch_item_failures_enhanced_metrics_disabled(self): |
| 397 | + response = { |
| 398 | + "batchItemFailures": [ |
| 399 | + {"itemIdentifier": "msg-1"}, |
| 400 | + ] |
| 401 | + } |
| 402 | + context = unittest.mock.Mock() |
| 403 | + submit_batch_item_failures_metric(response, context) |
| 404 | + self.mock_lambda_metric.assert_not_called() |
0 commit comments