Skip to content

Refactoring to allow lazy loading of datadog.api #163

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 13 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,24 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal
Otherwise, the metrics will be submitted to the Datadog API
periodically and at the end of the function execution in a
background thread.

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
"""
flush_to_logs = os.environ.get("DD_FLUSH_TO_LOG", "").lower() == "true"
tags = tag_dd_lambda_layer(tags)

if flush_to_logs or (force_async and not should_use_extension):
write_metric_point_to_stdout(metric_name, value, timestamp=timestamp, tags=tags)
else:
logger.debug("Sending metric %s to Datadog via lambda layer", metric_name)
if should_use_extension:
lambda_stats.distribution(metric_name, value, tags=tags, timestamp=timestamp)
Copy link
Contributor Author

@maxday maxday Aug 26, 2021

Choose a reason for hiding this comment

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

I am usually not a big fan of having two if branches which execute the same code but I think for this case it makes the code easier to read

else:
if flush_to_logs or force_async:
write_metric_point_to_stdout(
metric_name, value, timestamp=timestamp, tags=tags
)
else:
lambda_stats.distribution(
metric_name, value, tags=tags, timestamp=timestamp
)


def write_metric_point_to_stdout(metric_name, value, timestamp=None, tags=[]):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from botocore.exceptions import ClientError as BotocoreClientError
from datadog.api.exceptions import ClientError


from datadog_lambda.metric import lambda_metric
from datadog_lambda.api import decrypt_kms_api_key, KMS_ENCRYPTION_CONTEXT_KEY
from datadog_lambda.thread_stats_writer import ThreadStatsWriter
Expand All @@ -33,6 +35,17 @@ def test_lambda_metric_tagged_with_dd_lambda_layer(self):
]
)

# let's fake that the extension is present, this should override DD_FLUSH_TO_LOG
@patch("datadog_lambda.metric.should_use_extension", True)
def test_lambda_metric_flush_to_log_with_extension(self):
os.environ["DD_FLUSH_TO_LOG"] = "True"
lambda_metric("test", 1)
expected_tag = _format_dd_lambda_layer_tag()
self.mock_metric_lambda_stats.distribution.assert_has_calls(
[call("test", 1, timestamp=None, tags=[expected_tag])]
)
del os.environ["DD_FLUSH_TO_LOG"]

def test_lambda_metric_flush_to_log(self):
os.environ["DD_FLUSH_TO_LOG"] = "True"

Expand Down