Skip to content

Commit 8ac32ed

Browse files
Handle datetime parameter for historical custom metrics submitted to the API (#509)
handle datetime params
1 parent d4b411e commit 8ac32ed

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

datadog_lambda/metric.py

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal
6565
timestamp_ceiling = int(
6666
(datetime.now() - timedelta(hours=4)).timestamp()
6767
) # 4 hours ago
68+
if isinstance(timestamp, datetime):
69+
timestamp = int(timestamp.timestamp())
6870
if timestamp_ceiling > timestamp:
6971
logger.warning(
7072
"Timestamp %s is older than 4 hours, not submitting metric %s",

tests/test_metric.py

+13
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ def test_lambda_metric_timestamp_with_extension(self):
5858
"test_timestamp", 1, timestamp=timestamp, tags=[dd_lambda_layer_tag]
5959
)
6060

61+
@patch("datadog_lambda.metric.should_use_extension", True)
62+
def test_lambda_metric_datetime_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 = datetime.now() - delta
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+
6174
@patch("datadog_lambda.metric.should_use_extension", True)
6275
def test_lambda_metric_invalid_timestamp_with_extension(self):
6376
patcher = patch("datadog_lambda.metric.extension_thread_stats")

0 commit comments

Comments
 (0)