Skip to content

Commit

Permalink
Handle datetime parameter for historical custom metrics submitted to …
Browse files Browse the repository at this point in the history
…the API (#509)

handle datetime params
  • Loading branch information
DylanLovesCoffee authored Aug 8, 2024
1 parent d4b411e commit 8ac32ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8ac32ed

Please sign in to comment.