Skip to content

Commit

Permalink
Support calling flush_stats with None context. (#518)
Browse files Browse the repository at this point in the history
* Support calling flush_stats with None context.

* Fix lint error.
  • Loading branch information
purple4reina authored Sep 30, 2024
1 parent adfc9e8 commit de6b4ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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

0 comments on commit de6b4ff

Please sign in to comment.