diff --git a/datadog_lambda/metric.py b/datadog_lambda/metric.py index 3bc9955f..44f8e377 100644 --- a/datadog_lambda/metric.py +++ b/datadog_lambda/metric.py @@ -65,6 +65,8 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal timestamp_ceiling = int( (datetime.now() - timedelta(hours=4)).timestamp() ) # 4 hours ago + if isinstance(timestamp, datetime): + timestamp = int(timestamp.timestamp()) if timestamp_ceiling > timestamp: logger.warning( "Timestamp %s is older than 4 hours, not submitting metric %s", diff --git a/tests/test_metric.py b/tests/test_metric.py index 031b1180..10ef9191 100644 --- a/tests/test_metric.py +++ b/tests/test_metric.py @@ -58,6 +58,19 @@ def test_lambda_metric_timestamp_with_extension(self): "test_timestamp", 1, timestamp=timestamp, tags=[dd_lambda_layer_tag] ) + @patch("datadog_lambda.metric.should_use_extension", True) + def test_lambda_metric_datetime_with_extension(self): + patcher = patch("datadog_lambda.metric.extension_thread_stats") + self.mock_metric_extension_thread_stats = patcher.start() + self.addCleanup(patcher.stop) + + delta = timedelta(hours=5) + timestamp = datetime.now() - delta + + lambda_metric("test_timestamp", 1, timestamp) + self.mock_metric_lambda_stats.distribution.assert_not_called() + self.mock_metric_extension_thread_stats.distribution.assert_not_called() + @patch("datadog_lambda.metric.should_use_extension", True) def test_lambda_metric_invalid_timestamp_with_extension(self): patcher = patch("datadog_lambda.metric.extension_thread_stats")