@@ -72,24 +72,24 @@ def _convert_xray_trace_id(xray_trace_id):
72
72
"""
73
73
Convert X-Ray trace id (hex)'s last 63 bits to a Datadog trace id (int).
74
74
"""
75
- return str ( 0x7FFFFFFFFFFFFFFF & int (xray_trace_id [- 16 :], 16 ) )
75
+ return 0x7FFFFFFFFFFFFFFF & int (xray_trace_id [- 16 :], 16 )
76
76
77
77
78
78
def _convert_xray_entity_id (xray_entity_id ):
79
79
"""
80
80
Convert X-Ray (sub)segement id (hex) to a Datadog span id (int).
81
81
"""
82
- return str ( int (xray_entity_id , 16 ) )
82
+ return int (xray_entity_id , 16 )
83
83
84
84
85
85
def _convert_xray_sampling (xray_sampled ):
86
86
"""
87
87
Convert X-Ray sampled (True/False) to its Datadog counterpart.
88
88
"""
89
89
return (
90
- str ( SamplingPriority .USER_KEEP )
90
+ SamplingPriority .USER_KEEP
91
91
if xray_sampled
92
- else str ( SamplingPriority .USER_REJECT )
92
+ else SamplingPriority .USER_REJECT
93
93
)
94
94
95
95
@@ -332,16 +332,16 @@ def extract_context_from_kinesis_event(event, lambda_context):
332
332
return extract_context_from_lambda_context (lambda_context )
333
333
334
334
335
- def _deterministic_md5_hash (s : str ) -> str :
335
+ def _deterministic_md5_hash (s : str ) -> int :
336
336
"""MD5 here is to generate trace_id, not for any encryption."""
337
337
hex_number = hashlib .md5 (s .encode ("ascii" )).hexdigest ()
338
338
binary = bin (int (hex_number , 16 ))
339
339
binary_str = str (binary )
340
340
binary_str_remove_0b = binary_str [2 :].rjust (128 , "0" )
341
341
most_significant_64_bits_without_leading_1 = "0" + binary_str_remove_0b [1 :- 64 ]
342
- result = str ( int (most_significant_64_bits_without_leading_1 , 2 ) )
343
- if result == "0" * 64 :
344
- return "1"
342
+ result = int (most_significant_64_bits_without_leading_1 , 2 )
343
+ if result == 0 :
344
+ return 1
345
345
return result
346
346
347
347
@@ -378,7 +378,9 @@ def extract_context_custom_extractor(extractor, event, lambda_context):
378
378
sampling_priority ,
379
379
) = extractor (event , lambda_context )
380
380
return Context (
381
- trace_id = trace_id , span_id = parent_id , sampling_priority = sampling_priority
381
+ trace_id = int (trace_id ),
382
+ span_id = int (parent_id ),
383
+ sampling_priority = int (sampling_priority ),
382
384
)
383
385
except Exception as e :
384
386
logger .debug ("The trace extractor returned with error %s" , e )
@@ -480,7 +482,7 @@ def extract_dd_trace_context(
480
482
return dd_trace_context , trace_context_source , event_source
481
483
482
484
483
- def get_dd_trace_context ():
485
+ def get_dd_trace_context_obj ():
484
486
"""
485
487
Return the Datadog trace context to be propagated on the outgoing requests.
486
488
@@ -517,9 +519,24 @@ def get_dd_trace_context():
517
519
trace_id = dd_trace_context .trace_id ,
518
520
span_id = xray_context .span_id ,
519
521
sampling_priority = dd_trace_context .sampling_priority ,
522
+ meta = dd_trace_context ._meta .copy (),
523
+ metrics = dd_trace_context ._metrics .copy (),
520
524
)
521
525
522
526
527
+ def get_dd_trace_context ():
528
+ """
529
+ Return the Datadog trace context to be propagated on the outgoing requests,
530
+ as a dict of headers.
531
+ """
532
+ headers = {}
533
+ context = get_dd_trace_context_obj ()
534
+ if not _is_context_complete (context ):
535
+ return headers
536
+ propagator .inject (context , headers )
537
+ return headers
538
+
539
+
523
540
def set_correlation_ids ():
524
541
"""
525
542
Create a dummy span, and overrides its trace_id and span_id, to make
@@ -535,7 +552,7 @@ def set_correlation_ids():
535
552
logger .debug ("using ddtrace implementation for spans" )
536
553
return
537
554
538
- context = get_dd_trace_context ()
555
+ context = get_dd_trace_context_obj ()
539
556
if not _is_context_complete (context ):
540
557
return
541
558
0 commit comments