Skip to content

Commit 736ba06

Browse files
fix: timestamps we send to the extension should be integers
1 parent 8a01794 commit 736ba06

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

datadog_lambda/metric.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal
111111
if isinstance(timestamp, datetime):
112112
timestamp = int(timestamp.timestamp())
113113

114+
else:
115+
try:
116+
timestamp = int(timestamp)
117+
except Exception:
118+
logger.debug(
119+
"Ignoring metric submission for metric '%s' because the timestamp cannot "
120+
"be turned into an integer: %r",
121+
metric_name,
122+
timestamp,
123+
)
124+
return
125+
114126
timestamp_floor = int((datetime.now() - timedelta(hours=4)).timestamp())
115127
if timestamp < timestamp_floor:
116128
logger.warning(

tests/test_metric.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ def test_lambda_metric_datetime_with_extension(self):
111111
)
112112
self.mock_write_metric_point_to_stdout.assert_not_called()
113113

114+
@patch("datadog_lambda.metric.metrics_handler", MetricsHandler.EXTENSION)
115+
def test_lambda_metric_float_with_extension(self):
116+
delta = timedelta(minutes=1)
117+
timestamp_float = (datetime.now() - delta).timestamp()
118+
timestamp_int = int(timestamp_float)
119+
120+
lambda_metric("test_timestamp", 1, timestamp_float)
121+
self.mock_metric_lambda_stats.distribution.assert_has_calls(
122+
[
123+
call(
124+
"test_timestamp",
125+
1,
126+
timestamp=timestamp_int,
127+
tags=[dd_lambda_layer_tag],
128+
)
129+
]
130+
)
131+
self.mock_write_metric_point_to_stdout.assert_not_called()
132+
133+
@patch("datadog_lambda.metric.metrics_handler", MetricsHandler.EXTENSION)
134+
def test_lambda_metric_timestamp_junk_with_extension(self):
135+
delta = timedelta(minutes=1)
136+
timestamp = (datetime.now() - delta).isoformat()
137+
138+
lambda_metric("test_timestamp", 1, timestamp)
139+
self.mock_metric_lambda_stats.distribution.assert_not_called()
140+
self.mock_write_metric_point_to_stdout.assert_not_called()
141+
114142
@patch("datadog_lambda.metric.metrics_handler", MetricsHandler.EXTENSION)
115143
def test_lambda_metric_invalid_timestamp_with_extension(self):
116144
delta = timedelta(hours=5)

0 commit comments

Comments
 (0)