Skip to content

Support calling flush_stats with None context. #518

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 2 commits into from
Sep 30, 2024
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
1 change: 1 addition & 0 deletions datadog_lambda/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def flush_stats(lambda_context=None):
lambda_stats.flush()

if extension_thread_stats is not None:
tags = None
if lambda_context is not None:
tags = get_enhanced_metrics_tags(lambda_context)
split_arn = lambda_context.invoked_function_arn.split(":")
Expand Down
10 changes: 9 additions & 1 deletion tests/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datadog.api.exceptions import ClientError
from datetime import datetime, timedelta

from datadog_lambda.metric import lambda_metric
from datadog_lambda.metric import lambda_metric, flush_stats
from datadog_lambda.api import decrypt_kms_api_key, KMS_ENCRYPTION_CONTEXT_KEY
from datadog_lambda.thread_stats_writer import ThreadStatsWriter
from datadog_lambda.tags import dd_lambda_layer_tag
Expand Down Expand Up @@ -101,6 +101,10 @@ def setUp(self):
self.mock_threadstats_flush_distributions = patcher.start()
self.addCleanup(patcher.stop)

patcher = patch("datadog_lambda.metric.extension_thread_stats")
self.mock_extension_thread_stats = patcher.start()
self.addCleanup(patcher.stop)

def test_retry_on_remote_disconnected(self):
# Raise the RemoteDisconnected error
lambda_stats = ThreadStatsWriter(True)
Expand All @@ -123,6 +127,10 @@ def test_flush_stats_with_tags(self):
for tag in tags:
self.assertTrue(tag in lambda_stats.thread_stats.constant_tags)

def test_flush_stats_without_context(self):
flush_stats(lambda_context=None)
self.mock_extension_thread_stats.flush.assert_called_with(None)


MOCK_FUNCTION_NAME = "myFunction"

Expand Down
Loading