|
10 | 10 |
|
11 | 11 | from datadog_lambda.extension import should_use_extension |
12 | 12 | from datadog_lambda.tags import get_enhanced_metrics_tags, dd_lambda_layer_tag |
13 | | -from datadog_lambda.api import init_api |
14 | 13 |
|
15 | 14 | logger = logging.getLogger(__name__) |
16 | 15 |
|
17 | 16 | lambda_stats = None |
| 17 | +extension_thread_stats = None |
18 | 18 |
|
19 | | -init_api() |
| 19 | +flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true" |
20 | 20 |
|
21 | 21 | if should_use_extension: |
22 | 22 | from datadog_lambda.statsd_writer import StatsDWriter |
|
28 | 28 | # end of invocation. To make metrics submitted from a long-running Lambda |
29 | 29 | # function available sooner, consider using the Datadog Lambda extension. |
30 | 30 | from datadog_lambda.thread_stats_writer import ThreadStatsWriter |
| 31 | + from datadog_lambda.api import init_api |
31 | 32 |
|
32 | | - flush_in_thread = os.environ.get("DD_FLUSH_IN_THREAD", "").lower() == "true" |
| 33 | + init_api() |
33 | 34 | lambda_stats = ThreadStatsWriter(flush_in_thread) |
34 | 35 |
|
35 | 36 | enhanced_metrics_enabled = ( |
@@ -57,6 +58,22 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None, force_async=Fal |
57 | 58 | tags = [] if tags is None else list(tags) |
58 | 59 | tags.append(dd_lambda_layer_tag) |
59 | 60 |
|
| 61 | + if should_use_extension and timestamp is not None: |
| 62 | + # The extension does not support timestamps for distributions so we create a |
| 63 | + # a thread stats writer to submit metrics with timestamps to the API |
| 64 | + global extension_thread_stats |
| 65 | + if extension_thread_stats is None: |
| 66 | + from datadog_lambda.thread_stats_writer import ThreadStatsWriter |
| 67 | + from datadog_lambda.api import init_api |
| 68 | + |
| 69 | + init_api() |
| 70 | + extension_thread_stats = ThreadStatsWriter(flush_in_thread) |
| 71 | + |
| 72 | + extension_thread_stats.distribution( |
| 73 | + metric_name, value, tags=tags, timestamp=timestamp |
| 74 | + ) |
| 75 | + return |
| 76 | + |
60 | 77 | if should_use_extension: |
61 | 78 | logger.debug( |
62 | 79 | "Sending metric %s value %s to Datadog via extension", metric_name, value |
@@ -94,6 +111,9 @@ def write_metric_point_to_stdout(metric_name, value, timestamp=None, tags=[]): |
94 | 111 | def flush_stats(): |
95 | 112 | lambda_stats.flush() |
96 | 113 |
|
| 114 | + if extension_thread_stats is not None: |
| 115 | + extension_thread_stats.flush() |
| 116 | + |
97 | 117 |
|
98 | 118 | def submit_enhanced_metric(metric_name, lambda_context): |
99 | 119 | """Submits the enhanced metric with the given name |
|
0 commit comments