@@ -72,25 +72,21 @@ 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
- return (
90
- str (SamplingPriority .USER_KEEP )
91
- if xray_sampled
92
- else str (SamplingPriority .USER_REJECT )
93
- )
89
+ return SamplingPriority .USER_KEEP if xray_sampled else SamplingPriority .USER_REJECT
94
90
95
91
96
92
def _get_xray_trace_context ():
@@ -332,16 +328,16 @@ def extract_context_from_kinesis_event(event, lambda_context):
332
328
return extract_context_from_lambda_context (lambda_context )
333
329
334
330
335
- def _deterministic_md5_hash (s : str ) -> str :
331
+ def _deterministic_md5_hash (s : str ) -> int :
336
332
"""MD5 here is to generate trace_id, not for any encryption."""
337
333
hex_number = hashlib .md5 (s .encode ("ascii" )).hexdigest ()
338
334
binary = bin (int (hex_number , 16 ))
339
335
binary_str = str (binary )
340
336
binary_str_remove_0b = binary_str [2 :].rjust (128 , "0" )
341
337
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"
338
+ result = int (most_significant_64_bits_without_leading_1 , 2 )
339
+ if result == 0 :
340
+ return 1
345
341
return result
346
342
347
343
@@ -378,7 +374,9 @@ def extract_context_custom_extractor(extractor, event, lambda_context):
378
374
sampling_priority ,
379
375
) = extractor (event , lambda_context )
380
376
return Context (
381
- trace_id = trace_id , span_id = parent_id , sampling_priority = sampling_priority
377
+ trace_id = int (trace_id ),
378
+ span_id = int (parent_id ),
379
+ sampling_priority = int (sampling_priority ),
382
380
)
383
381
except Exception as e :
384
382
logger .debug ("The trace extractor returned with error %s" , e )
@@ -480,7 +478,7 @@ def extract_dd_trace_context(
480
478
return dd_trace_context , trace_context_source , event_source
481
479
482
480
483
- def get_dd_trace_context ():
481
+ def get_dd_trace_context_obj ():
484
482
"""
485
483
Return the Datadog trace context to be propagated on the outgoing requests.
486
484
@@ -517,9 +515,24 @@ def get_dd_trace_context():
517
515
trace_id = dd_trace_context .trace_id ,
518
516
span_id = xray_context .span_id ,
519
517
sampling_priority = dd_trace_context .sampling_priority ,
518
+ meta = dd_trace_context ._meta .copy (),
519
+ metrics = dd_trace_context ._metrics .copy (),
520
520
)
521
521
522
522
523
+ def get_dd_trace_context ():
524
+ """
525
+ Return the Datadog trace context to be propagated on the outgoing requests,
526
+ as a dict of headers.
527
+ """
528
+ headers = {}
529
+ context = get_dd_trace_context_obj ()
530
+ if not _is_context_complete (context ):
531
+ return headers
532
+ propagator .inject (context , headers )
533
+ return headers
534
+
535
+
523
536
def set_correlation_ids ():
524
537
"""
525
538
Create a dummy span, and overrides its trace_id and span_id, to make
@@ -535,7 +548,7 @@ def set_correlation_ids():
535
548
logger .debug ("using ddtrace implementation for spans" )
536
549
return
537
550
538
- context = get_dd_trace_context ()
551
+ context = get_dd_trace_context_obj ()
539
552
if not _is_context_complete (context ):
540
553
return
541
554
0 commit comments