Skip to content

Commit 37369f1

Browse files
authored
fix hello route hit too early (#147)
fix hello route hit too early
1 parent 5c01c2e commit 37369f1

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

datadog_lambda/metric.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
logger = logging.getLogger(__name__)
2323

24+
lambda_stats = None
25+
2426

2527
class StatsWriter:
2628
def distribution(self, metric_name, value, tags=[], timestamp=None):
@@ -111,16 +113,17 @@ def stop(self):
111113
self.thread_stats.stop()
112114

113115

114-
lambda_stats = None
115-
if should_use_extension:
116-
lambda_stats = StatsDWriter()
117-
else:
118-
# Periodical flushing in a background thread is NOT guaranteed to succeed
119-
# and leads to data loss. When disabled, metrics are only flushed at the
120-
# end of invocation. To make metrics submitted from a long-running Lambda
121-
# function available sooner, consider using the Datadog Lambda extension.
122-
flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true"
123-
lambda_stats = ThreadStatsWriter(flush_in_thread)
116+
def init_lambda_stats():
117+
global lambda_stats
118+
if should_use_extension:
119+
lambda_stats = StatsDWriter()
120+
else:
121+
# Periodical flushing in a background thread is NOT guaranteed to succeed
122+
# and leads to data loss. When disabled, metrics are only flushed at the
123+
# end of invocation. To make metrics submitted from a long-running Lambda
124+
# function available sooner, consider using the Datadog Lambda extension.
125+
flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true"
126+
lambda_stats = ThreadStatsWriter(flush_in_thread)
124127

125128

126129
def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=False):
@@ -136,6 +139,7 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal
136139
periodically and at the end of the function execution in a
137140
background thread.
138141
"""
142+
global lambda_stats
139143
flush_to_logs = os.environ.get("DD_FLUSH_TO_LOG", "").lower() == "true"
140144
tags = tag_dd_lambda_layer(tags)
141145

@@ -164,6 +168,7 @@ def write_metric_point_to_stdout(metric_name, value, timestamp=None, tags=[]):
164168

165169

166170
def flush_stats():
171+
global lambda_stats
167172
lambda_stats.flush()
168173

169174

datadog_lambda/wrapper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datadog_lambda.cold_start import set_cold_start, is_cold_start
1313
from datadog_lambda.constants import XraySubsegment, TraceContextSource
1414
from datadog_lambda.metric import (
15+
init_lambda_stats,
1516
flush_stats,
1617
submit_invocations_metric,
1718
submit_errors_metric,
@@ -120,6 +121,7 @@ def __call__(self, event, context, **kwargs):
120121
"""Executes when the wrapped function gets called"""
121122
self.trigger_tags = extract_trigger_tags(event, context)
122123
self.response = None
124+
init_lambda_stats()
123125
self._before(event, context)
124126
try:
125127
self.response = self.func(event, context, **kwargs)

tests/test_wrapper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ def lambda_handler(event, context):
133133

134134
def test_datadog_lambda_wrapper_flush_in_thread(self):
135135
# force ThreadStats to flush in thread
136+
os.environ["DD_FLUSH_IN_THREAD"] = "True"
136137
import datadog_lambda.metric as metric_module
137138

138139
metric_module.lambda_stats.stop()
139-
metric_module.lambda_stats = ThreadStatsWriter(True)
140+
metric_module.init_lambda_stats()
140141

141142
@datadog_lambda_wrapper
142143
def lambda_handler(event, context):
@@ -157,6 +158,7 @@ def lambda_handler(event, context):
157158
# reset ThreadStats
158159
metric_module.lambda_stats.stop()
159160
metric_module.lambda_stats = ThreadStatsWriter(False)
161+
del os.environ["DD_FLUSH_IN_THREAD"]
160162

161163
def test_datadog_lambda_wrapper_not_flush_in_thread(self):
162164
# force ThreadStats to not flush in thread

0 commit comments

Comments
 (0)