From 8ca58b40dcfa8ad4c2169a27ddca27b19c7fb348 Mon Sep 17 00:00:00 2001 From: Aleksandr Pasechnik Date: Thu, 8 May 2025 11:19:03 -0400 Subject: [PATCH 1/2] fix: timestamps we send to the extension should be integers (#590) Also added some defense in depth for our lower level statsd call. --- datadog_lambda/dogstatsd.py | 2 +- datadog_lambda/metric.py | 12 ++++++++++++ tests/test_dogstatsd.py | 4 ++++ tests/test_metric.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/datadog_lambda/dogstatsd.py b/datadog_lambda/dogstatsd.py index f30a2039..a08e2592 100644 --- a/datadog_lambda/dogstatsd.py +++ b/datadog_lambda/dogstatsd.py @@ -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): diff --git a/datadog_lambda/metric.py b/datadog_lambda/metric.py index 0c18b517..c9b978d6 100644 --- a/datadog_lambda/metric.py +++ b/datadog_lambda/metric.py @@ -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( diff --git a/tests/test_dogstatsd.py b/tests/test_dogstatsd.py index ea6afd48..6fe79372 100644 --- a/tests/test_dogstatsd.py +++ b/tests/test_dogstatsd.py @@ -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") diff --git a/tests/test_metric.py b/tests/test_metric.py index a4b0be2c..e7dab2c3 100644 --- a/tests/test_metric.py +++ b/tests/test_metric.py @@ -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) From 1226b2de560afc6a8fe3e631ad84960b5dd03eef Mon Sep 17 00:00:00 2001 From: Aleksandr Pasechnik Date: Fri, 9 May 2025 12:46:04 -0400 Subject: [PATCH 2/2] v6.109.0 (#591) --- datadog_lambda/version.py | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/datadog_lambda/version.py b/datadog_lambda/version.py index bcd37def..c3aaa6b7 100644 --- a/datadog_lambda/version.py +++ b/datadog_lambda/version.py @@ -1 +1 @@ -__version__ = "6.108.0" +__version__ = "6.109.0" diff --git a/pyproject.toml b/pyproject.toml index 8f16b438..cccef63e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "datadog_lambda" -version = "6.108.0" +version = "6.109.0" description = "The Datadog AWS Lambda Library" authors = ["Datadog, Inc. "] license = "Apache-2.0"