99from aws_xray_sdk .core .lambda_launcher import LambdaContext
1010
1111from ddtrace import patch , tracer
12- from datadog_lambda .constants import (
13- SamplingPriority ,
14- TraceHeader ,
15- XraySubsegment ,
16- )
12+ from datadog_lambda .constants import SamplingPriority , TraceHeader , XraySubsegment
1713
1814logger = logging .getLogger (__name__ )
1915
@@ -38,8 +34,11 @@ def _convert_xray_sampling(xray_sampled):
3834 """
3935 Convert X-Ray sampled (True/False) to its Datadog counterpart.
4036 """
41- return str (SamplingPriority .USER_KEEP ) if xray_sampled \
37+ return (
38+ str (SamplingPriority .USER_KEEP )
39+ if xray_sampled
4240 else str (SamplingPriority .USER_REJECT )
41+ )
4342
4443
4544def extract_dd_trace_context (event ):
@@ -54,33 +53,31 @@ def extract_dd_trace_context(event):
5453 the correct context.
5554 """
5655 global dd_trace_context
57- headers = event .get (' headers' , {})
56+ headers = event .get (" headers" , {})
5857 lowercase_headers = {k .lower (): v for k , v in headers .items ()}
5958
6059 trace_id = lowercase_headers .get (TraceHeader .TRACE_ID )
6160 parent_id = lowercase_headers .get (TraceHeader .PARENT_ID )
6261 sampling_priority = lowercase_headers .get (TraceHeader .SAMPLING_PRIORITY )
6362 if trace_id and parent_id and sampling_priority :
64- logger .debug (' Extracted Datadog trace context from headers' )
63+ logger .debug (" Extracted Datadog trace context from headers" )
6564 dd_trace_context = {
66- ' trace-id' : trace_id ,
67- ' parent-id' : parent_id ,
68- ' sampling-priority' : sampling_priority ,
65+ " trace-id" : trace_id ,
66+ " parent-id" : parent_id ,
67+ " sampling-priority" : sampling_priority ,
6968 }
7069 xray_recorder .begin_subsegment (XraySubsegment .NAME )
7170 subsegment = xray_recorder .current_subsegment ()
7271 subsegment .put_metadata (
73- XraySubsegment .KEY ,
74- dd_trace_context ,
75- XraySubsegment .NAMESPACE
72+ XraySubsegment .KEY , dd_trace_context , XraySubsegment .NAMESPACE
7673 )
7774 xray_recorder .end_subsegment ()
7875 else :
7976 # AWS Lambda runtime caches global variables between invocations,
8077 # reset to avoid using the context from the last invocation.
8178 dd_trace_context = {}
8279
83- logger .debug (' extracted dd trace context %s' , dd_trace_context )
80+ logger .debug (" extracted dd trace context %s" , dd_trace_context )
8481
8582
8683def get_dd_trace_context ():
@@ -96,28 +93,24 @@ def get_dd_trace_context():
9693 context to an outgoing request.
9794 """
9895 if not is_lambda_context ():
99- logger .debug (' get_dd_trace_context is only supported in LambdaContext' )
96+ logger .debug (" get_dd_trace_context is only supported in LambdaContext" )
10097 return {}
10198
10299 global dd_trace_context
103100 xray_trace_entity = xray_recorder .get_trace_entity () # xray (sub)segment
104101 if dd_trace_context :
105102 return {
106- TraceHeader .TRACE_ID :
107- dd_trace_context ['trace-id' ],
108- TraceHeader .PARENT_ID : _convert_xray_entity_id (
109- xray_trace_entity .id ),
110- TraceHeader .SAMPLING_PRIORITY :
111- dd_trace_context ['sampling-priority' ],
103+ TraceHeader .TRACE_ID : dd_trace_context ["trace-id" ],
104+ TraceHeader .PARENT_ID : _convert_xray_entity_id (xray_trace_entity .id ),
105+ TraceHeader .SAMPLING_PRIORITY : dd_trace_context ["sampling-priority" ],
112106 }
113107 else :
114108 return {
115- TraceHeader .TRACE_ID : _convert_xray_trace_id (
116- xray_trace_entity .trace_id ),
117- TraceHeader .PARENT_ID : _convert_xray_entity_id (
118- xray_trace_entity .id ),
109+ TraceHeader .TRACE_ID : _convert_xray_trace_id (xray_trace_entity .trace_id ),
110+ TraceHeader .PARENT_ID : _convert_xray_entity_id (xray_trace_entity .id ),
119111 TraceHeader .SAMPLING_PRIORITY : _convert_xray_sampling (
120- xray_trace_entity .sampled ),
112+ xray_trace_entity .sampled
113+ ),
121114 }
122115
123116
@@ -130,16 +123,16 @@ def set_correlation_ids():
130123 TODO: Remove me when Datadog tracer is natively supported in Lambda.
131124 """
132125 if not is_lambda_context ():
133- logger .debug (' set_correlation_ids is only supported in LambdaContext' )
126+ logger .debug (" set_correlation_ids is only supported in LambdaContext" )
134127 return
135128
136129 context = get_dd_trace_context ()
137130
138- span = tracer .trace (' dummy.span' )
131+ span = tracer .trace (" dummy.span" )
139132 span .trace_id = context [TraceHeader .TRACE_ID ]
140133 span .span_id = context [TraceHeader .PARENT_ID ]
141134
142- logger .debug (' correlation ids set' )
135+ logger .debug (" correlation ids set" )
143136
144137
145138def inject_correlation_ids ():
@@ -153,17 +146,19 @@ def inject_correlation_ids():
153146 # Override the log format of the AWS provided LambdaLoggerHandler
154147 root_logger = logging .getLogger ()
155148 for handler in root_logger .handlers :
156- if handler .__class__ .__name__ == 'LambdaLoggerHandler' :
157- handler .setFormatter (logging .Formatter (
158- '[%(levelname)s]\t %(asctime)s.%(msecs)dZ\t %(aws_request_id)s\t '
159- '[dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s]\t %(message)s\n ' ,
160- '%Y-%m-%dT%H:%M:%S'
161- ))
149+ if handler .__class__ .__name__ == "LambdaLoggerHandler" :
150+ handler .setFormatter (
151+ logging .Formatter (
152+ "[%(levelname)s]\t %(asctime)s.%(msecs)dZ\t %(aws_request_id)s\t "
153+ "[dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s]\t %(message)s\n " ,
154+ "%Y-%m-%dT%H:%M:%S" ,
155+ )
156+ )
162157
163158 # Patch `logging.Logger.makeRecord` to actually inject correlation ids
164159 patch (logging = True )
165160
166- logger .debug (' logs injection configured' )
161+ logger .debug (" logs injection configured" )
167162
168163
169164def is_lambda_context ():
0 commit comments