Skip to content
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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

nhulston
Copy link
Contributor

@nhulston nhulston commented Jan 21, 2025

What does this PR do?

When lambda_metric is called with an invalid name or value, we skip the metric and log a warning. This ensures that if multiple metrics are sent in one lambda invocation and only one is invalid, the rest still get delivered.

Motivation

#347

Testing Guidelines

Unit tests

Manual testing; I have a lambda with this handler:

from datadog_lambda.metric import lambda_metric

def main(event, context):
    print('Sending metric...')

    lambda_metric(metric_name='nhulston.test', value=1)
    lambda_metric(metric_name='nhulston.test', value=2.5)
    lambda_metric(metric_name='nhulston.test', value="3")
    lambda_metric(metric_name='nhulston.test', value="abc")
    lambda_metric(metric_name=5, value=5)

    print('Metric sent!')

    return 200

and I see the correct 3 metrics delivered, while seeing logs

Sending metric...
[WARNING]	2025-01-21T20:38:47.187Z	aad67902-8de0-4282-9ed2-fd1b9ca5cc0d	Ignoring metric submission for metric 'nhulston.test' because the value is not numeric: '3'
[WARNING]	2025-01-21T20:38:47.187Z	aad67902-8de0-4282-9ed2-fd1b9ca5cc0d	Ignoring metric submission for metric 'nhulston.test' because the value is not numeric: 'abc'
[WARNING]	2025-01-21T20:38:47.188Z	aad67902-8de0-4282-9ed2-fd1b9ca5cc0d	Ignoring metric submission. Invalid metric name: 5
Metric sent!

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@nhulston nhulston force-pushed the nicholas.hulston/log-when-metric-invalid branch from e1fe582 to 9141673 Compare January 21, 2025 20:52
@nhulston nhulston marked this pull request as ready for review January 21, 2025 20:54
@nhulston nhulston requested a review from a team as a code owner January 21, 2025 20:54

if not isinstance(value, numbers.Number):
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants