Skip to content

Skip and log warning when lambda_metric called with invalid name or value #553

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 6 commits into from
Jan 30, 2025
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
16 changes: 16 additions & 0 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal
Note that if the extension is present, it will override the DD_FLUSH_TO_LOG value
and always use the layer to send metrics to the extension
"""
if not metric_name or not isinstance(metric_name, str):
logger.warning(
"Ignoring metric submission. Invalid metric name: %s", metric_name
)
return

try:
float(value)
except (ValueError, TypeError):
logger.warning(
"Ignoring metric submission for metric '%s' because the value is not numeric: %r",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance that lambda_metric("kittens", "1") works? If so, then maybe we do something like

try:
  int(value)
except:
  # log warning and return
  return

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that does work surprisingly. Making the change and updating PR description!

metric_name,
value,
)
return

flush_to_logs = os.environ.get("DD_FLUSH_TO_LOG", "").lower() == "true"
tags = [] if tags is None else list(tags)
tags.append(dd_lambda_layer_tag)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ def test_lambda_metric_flush_to_log(self):

del os.environ["DD_FLUSH_TO_LOG"]

@patch("datadog_lambda.metric.logger.warning")
def test_lambda_metric_invalid_metric_name_none(self, mock_logger_warning):
lambda_metric(None, 1)
self.mock_metric_lambda_stats.distribution.assert_not_called()
mock_logger_warning.assert_called_once_with(
"Ignoring metric submission. Invalid metric name: %s", None
)

@patch("datadog_lambda.metric.logger.warning")
def test_lambda_metric_invalid_metric_name_not_string(self, mock_logger_warning):
lambda_metric(123, 1)
self.mock_metric_lambda_stats.distribution.assert_not_called()
mock_logger_warning.assert_called_once_with(
"Ignoring metric submission. Invalid metric name: %s", 123
)

@patch("datadog_lambda.metric.logger.warning")
def test_lambda_metric_non_numeric_value(self, mock_logger_warning):
lambda_metric("test.non_numeric", "oops")
self.mock_metric_lambda_stats.distribution.assert_not_called()
mock_logger_warning.assert_called_once_with(
"Ignoring metric submission for metric '%s' because the value is not numeric: %r",
"test.non_numeric",
"oops",
)


class TestFlushThreadStats(unittest.TestCase):
def setUp(self):
Expand Down
Loading