Skip to content

fix: timestamps we send to the extension should be integers #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datadog_lambda/dogstatsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _serialize_metric(self, metric, metric_type, value, tags, timestamp):
value,
metric_type,
("|#" + ",".join(self.normalize_tags(tags))) if tags else "",
("|T" + str(timestamp)) if timestamp is not None else "",
("|T" + str(int(timestamp))) if timestamp is not None else "",
)

def _report(self, metric, metric_type, value, tags, timestamp):
Expand Down
12 changes: 12 additions & 0 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal
if isinstance(timestamp, datetime):
timestamp = int(timestamp.timestamp())

else:
try:
timestamp = int(timestamp)
except Exception:
logger.debug(
"Ignoring metric submission for metric '%s' because the timestamp cannot "
"be turned into an integer: %r",
metric_name,
timestamp,
)
return

timestamp_floor = int((datetime.now() - timedelta(hours=4)).timestamp())
if timestamp < timestamp_floor:
logger.warning(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_dogstatsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ def test_distribution_with_tags(self):
def test_distribution_with_timestamp(self):
statsd.distribution("my.test.timestamp.metric", 9, timestamp=123456789)
self._checkOnlyOneMetric("my.test.timestamp.metric:9|d|T123456789")

def test_distribution_with_float_timestamp(self):
statsd.distribution("my.test.timestamp.metric", 9, timestamp=123456789.123)
self._checkOnlyOneMetric("my.test.timestamp.metric:9|d|T123456789")
28 changes: 28 additions & 0 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ def test_lambda_metric_datetime_with_extension(self):
)
self.mock_write_metric_point_to_stdout.assert_not_called()

@patch("datadog_lambda.metric.metrics_handler", MetricsHandler.EXTENSION)
def test_lambda_metric_float_with_extension(self):
delta = timedelta(minutes=1)
timestamp_float = (datetime.now() - delta).timestamp()
timestamp_int = int(timestamp_float)

lambda_metric("test_timestamp", 1, timestamp_float)
self.mock_metric_lambda_stats.distribution.assert_has_calls(
[
call(
"test_timestamp",
1,
timestamp=timestamp_int,
tags=[dd_lambda_layer_tag],
)
]
)
self.mock_write_metric_point_to_stdout.assert_not_called()

@patch("datadog_lambda.metric.metrics_handler", MetricsHandler.EXTENSION)
def test_lambda_metric_timestamp_junk_with_extension(self):
delta = timedelta(minutes=1)
timestamp = (datetime.now() - delta).isoformat()

lambda_metric("test_timestamp", 1, timestamp)
self.mock_metric_lambda_stats.distribution.assert_not_called()
self.mock_write_metric_point_to_stdout.assert_not_called()

@patch("datadog_lambda.metric.metrics_handler", MetricsHandler.EXTENSION)
def test_lambda_metric_invalid_timestamp_with_extension(self):
delta = timedelta(hours=5)
Expand Down
Loading