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

Merged
merged 6 commits into from
Jan 30, 2025

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="fail") # should fail
    lambda_metric(metric_name='nhulston.test', value=2.5)
    lambda_metric(metric_name='nhulston.test', value="1fail2") # should fail
    lambda_metric(metric_name='nhulston.test', value="3")
    lambda_metric(metric_name='nhulston.test', value="3.5")
    lambda_metric(metric_name=5, value=5) # should fail
    lambda_metric(metric_name='nhulston.test', value="abc") # should fail
    lambda_metric(metric_name='nhulston.test', value=6)

    print('Metric sent!')

    return 200

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

Sending metric...
[WARNING]	2025-01-30T16:18:43.249Z	35c2af43-3b69-4652-8ace-5a9b4567e1fb	Ignoring metric submission for metric 'nhulston.test' because the value cannot be converted to a number: 'fail'
[WARNING]	2025-01-30T16:18:43.249Z	35c2af43-3b69-4652-8ace-5a9b4567e1fb	Ignoring metric submission for metric 'nhulston.test' because the value cannot be converted to a number: '1fail2'
[WARNING]	2025-01-30T16:18:43.249Z	35c2af43-3b69-4652-8ace-5a9b4567e1fb	Ignoring metric submission. Invalid metric name: 5
[WARNING]	2025-01-30T16:18:43.249Z	35c2af43-3b69-4652-8ace-5a9b4567e1fb	Ignoring metric submission for metric 'nhulston.test' because the value cannot be converted to a number: 'abc'
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

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!

@nhulston nhulston requested a review from purple4reina January 30, 2025 16:20
@nhulston nhulston force-pushed the nicholas.hulston/log-when-metric-invalid branch from 0242219 to 8832023 Compare January 30, 2025 16:45
@nhulston nhulston merged commit b64be3b into main Jan 30, 2025
59 checks passed
@nhulston nhulston deleted the nicholas.hulston/log-when-metric-invalid branch January 30, 2025 22:30
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