1010import logging
1111
1212import boto3
13- from datadog import api
13+ from datadog import api , initialize , statsd
1414from datadog .threadstats import ThreadStats
15+ from datadog_lambda .extension import should_use_extension
1516from datadog_lambda .tags import get_enhanced_metrics_tags , tag_dd_lambda_layer
1617
1718
1819ENHANCED_METRICS_NAMESPACE_PREFIX = "aws.lambda.enhanced"
1920
2021logger = logging .getLogger (__name__ )
2122
22- lambda_stats = ThreadStats ()
23- lambda_stats .start ()
23+
24+ class StatsDWrapper :
25+ """
26+ Wraps StatsD calls, to give an identical interface to ThreadStats
27+ """
28+
29+ def __init__ (self ):
30+ options = {"statsd_host" : "127.0.0.1" , "statsd_port" : 8125 }
31+ initialize (** options )
32+
33+ def distribution (self , metric_name , value , tags = [], timestamp = None ):
34+ statsd .distribution (metric_name , value , tags = tags )
35+
36+ def flush (self , value ):
37+ pass
2438
2539
26- def lambda_metric (metric_name , value , timestamp = None , tags = None ):
40+ lambda_stats = None
41+ if should_use_extension :
42+ lambda_stats = StatsDWrapper ()
43+ else :
44+ lambda_stats = ThreadStats ()
45+ lambda_stats .start ()
46+
47+
48+ def lambda_metric (metric_name , value , timestamp = None , tags = None , force_async = False ):
2749 """
2850 Submit a data point to Datadog distribution metrics.
2951 https://docs.datadoghq.com/graphing/metrics/distributions/
@@ -36,12 +58,14 @@ def lambda_metric(metric_name, value, timestamp=None, tags=None):
3658 periodically and at the end of the function execution in a
3759 background thread.
3860 """
61+ flush_to_logs = os .environ .get ("DD_FLUSH_TO_LOG" , "" ).lower () == "true"
3962 tags = tag_dd_lambda_layer (tags )
40- if os .environ .get ("DD_FLUSH_TO_LOG" , "" ).lower () == "true" :
41- write_metric_point_to_stdout (metric_name , value , timestamp , tags )
63+
64+ if flush_to_logs or (force_async and not should_use_extension ):
65+ write_metric_point_to_stdout (metric_name , value , timestamp = timestamp , tags = tags )
4266 else :
4367 logger .debug ("Sending metric %s to Datadog via lambda layer" , metric_name )
44- lambda_stats .distribution (metric_name , value , timestamp = timestamp , tags = tags )
68+ lambda_stats .distribution (metric_name , value , tags = tags , timestamp = timestamp )
4569
4670
4771def write_metric_point_to_stdout (metric_name , value , timestamp = None , tags = []):
@@ -85,13 +109,10 @@ def submit_enhanced_metric(metric_name, lambda_context):
85109 metric_name ,
86110 )
87111 return
88-
89- # Enhanced metrics are always written to logs
90- write_metric_point_to_stdout (
91- "{}.{}" .format (ENHANCED_METRICS_NAMESPACE_PREFIX , metric_name ),
92- 1 ,
93- tags = get_enhanced_metrics_tags (lambda_context ),
94- )
112+ tags = get_enhanced_metrics_tags (lambda_context )
113+ metric_name = "aws.lambda.enhanced." + metric_name
114+ # Enhanced metrics always use an async submission method, (eg logs or extension).
115+ lambda_metric (metric_name , 1 , timestamp = None , tags = tags , force_async = True )
95116
96117
97118def submit_invocations_metric (lambda_context ):
0 commit comments