|
5 | 5 |
|
6 | 6 | from botocore.exceptions import ClientError as BotocoreClientError |
7 | 7 | from datadog.api.exceptions import ClientError |
8 | | - |
| 8 | +from datetime import datetime, timedelta |
9 | 9 |
|
10 | 10 | from datadog_lambda.metric import lambda_metric |
11 | 11 | from datadog_lambda.api import decrypt_kms_api_key, KMS_ENCRYPTION_CONTEXT_KEY |
@@ -49,12 +49,28 @@ def test_lambda_metric_timestamp_with_extension(self): |
49 | 49 | self.mock_metric_extension_thread_stats = patcher.start() |
50 | 50 | self.addCleanup(patcher.stop) |
51 | 51 |
|
52 | | - lambda_metric("test_timestamp", 1, 123) |
| 52 | + delta = timedelta(minutes=1) |
| 53 | + timestamp = int((datetime.now() - delta).timestamp()) |
| 54 | + |
| 55 | + lambda_metric("test_timestamp", 1, timestamp) |
53 | 56 | self.mock_metric_lambda_stats.distribution.assert_not_called() |
54 | 57 | self.mock_metric_extension_thread_stats.distribution.assert_called_with( |
55 | | - "test_timestamp", 1, timestamp=123, tags=[dd_lambda_layer_tag] |
| 58 | + "test_timestamp", 1, timestamp=timestamp, tags=[dd_lambda_layer_tag] |
56 | 59 | ) |
57 | 60 |
|
| 61 | + @patch("datadog_lambda.metric.should_use_extension", True) |
| 62 | + def test_lambda_metric_invalid_timestamp_with_extension(self): |
| 63 | + patcher = patch("datadog_lambda.metric.extension_thread_stats") |
| 64 | + self.mock_metric_extension_thread_stats = patcher.start() |
| 65 | + self.addCleanup(patcher.stop) |
| 66 | + |
| 67 | + delta = timedelta(hours=5) |
| 68 | + timestamp = int((datetime.now() - delta).timestamp()) |
| 69 | + |
| 70 | + lambda_metric("test_timestamp", 1, timestamp) |
| 71 | + self.mock_metric_lambda_stats.distribution.assert_not_called() |
| 72 | + self.mock_metric_extension_thread_stats.distribution.assert_not_called() |
| 73 | + |
58 | 74 | def test_lambda_metric_flush_to_log(self): |
59 | 75 | os.environ["DD_FLUSH_TO_LOG"] = "True" |
60 | 76 |
|
@@ -84,6 +100,16 @@ def test_retry_on_remote_disconnected(self): |
84 | 100 | lambda_stats.flush() |
85 | 101 | self.assertEqual(self.mock_threadstats_flush_distributions.call_count, 2) |
86 | 102 |
|
| 103 | + def test_flush_stats_with_tags(self): |
| 104 | + lambda_stats = ThreadStatsWriter(True) |
| 105 | + tags = ["tag1:value1", "tag2:value2"] |
| 106 | + lambda_stats.flush(tags) |
| 107 | + self.mock_threadstats_flush_distributions.assert_called_once_with( |
| 108 | + lambda_stats.thread_stats._get_aggregate_metrics_and_dists(float("inf"))[1] |
| 109 | + ) |
| 110 | + for tag in tags: |
| 111 | + self.assertTrue(tag in lambda_stats.thread_stats.constant_tags) |
| 112 | + |
87 | 113 |
|
88 | 114 | MOCK_FUNCTION_NAME = "myFunction" |
89 | 115 |
|
|
0 commit comments