DataDog / datadog-lambda-python

The Datadog AWS Lambda Layer for Python
https://docs.datadoghq.com/integrations/amazon_lambda/#installing-and-using-the-datadog-layer
Apache License 2.0
86 stars 46 forks source link

`flush_stats` can't be called without a Lambda context #515

Closed nr-synth closed 1 month ago

nr-synth commented 1 month ago

Expected Behavior

metric.flush_stats has an optional lambda_context argument, defaulting to None.

Omitting this argument shouldn't cause issues, and didn't in earlier versions of the library.

Actual Behavior

Exception thrown: builtins.UnboundLocalError: local variable 'tags' referenced before assignment

Steps to Reproduce the Problem

  1. metric.flush_stats()

Specifications

Stacktrace

Traceback (most recent call last):
  [...]
  File "/home/lambda/[...]", line 55, in send
    flush_stats()
  File "/home/lambda/datadog_lambda/metric.py", line 136, in flush_stats
    extension_thread_stats.flush(tags)
UnboundLocalError: local variable 'tags' referenced before assignment
purple4reina commented 1 month ago

Thanks for reporting this @nr-synth, this is indeed a bug in the library. I'll get started working on a fix.

However, you shouldn't need to ever call this method manually yourself. The datadog wrapper will do it for you after each invocation. Are you calling this manually?

nr-synth commented 1 month ago

Thanks @purple4reina - I don't have the full context, best I can surmise is that we were using this function when locally testing Lambda code with Datadog.

purple4reina commented 1 month ago

The change has been merged and will go out with our next release. Thanks again for reporting this!