8000 fix: timestamps we send to the extension should be integers (#590) · DataDog/datadog-lambda-python@8ca58b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ca58b4

Browse files
fix: timestamps we send to the extension should be integers (#590)
Also added some defense in depth for our lower level statsd call.
1 parent 8a01794 commit 8ca58b4

File tree

4 files changed

+45
-1
lines changed
  • tests
  • 4 files changed

    +45
    -1
    lines changed

    datadog_lambda/dogstatsd.py

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -97,7 +97,7 @@ def _serialize_metric(self, metric, metric_type, value, tags, timestamp):
    9797
    value,
    9898
    metric_type,
    9999
    ("|#" + ",".join(self.normalize_tags(tags))) if tags else "",
    100-
    ("|T" + str(timestamp)) if timestamp is not None else "",
    100+
    ("|T" + str(int(timestamp))) if timestamp is not None else "",
    101101
    )
    102102

    103103
    def _report(self, metric, metric_type, value, tags, timestamp):

    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_dogstatsd.py

    Lines changed: 4 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -53,3 +53,7 @@ def test_distribution_with_tags(self):
    5353
    def test_distribution_with_timestamp(self):
    5454
    statsd.distribution("my.test.timestamp.metric", 9, timestamp=123456789)
    5555
    self._checkOnlyOneMetric("my.test.timestamp.metric:9|d|T123456789")
    56+
    57+
    def test_distribution_with_float_timestamp(self):
    58+
    statsd.distribution("my.test.timestamp.metric", 9, timestamp=123456789.123)
    59+
    self._checkOnlyOneMetric("my.test.timestamp.metric:9|d|T123456789")

    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)
    0