Skip to content

Commit 77736c9

Browse files
committed
Inject headers when patching http requests.
1 parent 3312a30 commit 77736c9

File tree

2 files changed

+174
-137
lines changed

2 files changed

+174
-137
lines changed

datadog_lambda/tracing.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,24 @@ def _convert_xray_trace_id(xray_trace_id):
7272
"""
7373
Convert X-Ray trace id (hex)'s last 63 bits to a Datadog trace id (int).
7474
"""
75-
return str(0x7FFFFFFFFFFFFFFF & int(xray_trace_id[-16:], 16))
75+
return 0x7FFFFFFFFFFFFFFF & int(xray_trace_id[-16:], 16)
7676

7777

7878
def _convert_xray_entity_id(xray_entity_id):
7979
"""
8080
Convert X-Ray (sub)segement id (hex) to a Datadog span id (int).
8181
"""
82-
return str(int(xray_entity_id, 16))
82+
return int(xray_entity_id, 16)
8383

8484

8585
def _convert_xray_sampling(xray_sampled):
8686
"""
8787
Convert X-Ray sampled (True/False) to its Datadog counterpart.
8888
"""
8989
return (
90-
str(SamplingPriority.USER_KEEP)
90+
SamplingPriority.USER_KEEP
9191
if xray_sampled
92-
else str(SamplingPriority.USER_REJECT)
92+
else SamplingPriority.USER_REJECT
9393
)
9494

9595

@@ -332,16 +332,16 @@ def extract_context_from_kinesis_event(event, lambda_context):
332332
return extract_context_from_lambda_context(lambda_context)
333333

334334

335-
def _deterministic_md5_hash(s: str) -> str:
335+
def _deterministic_md5_hash(s: str) -> int:
336336
"""MD5 here is to generate trace_id, not for any encryption."""
337337
hex_number = hashlib.md5(s.encode("ascii")).hexdigest()
338338
binary = bin(int(hex_number, 16))
339339
binary_str = str(binary)
340340
binary_str_remove_0b = binary_str[2:].rjust(128, "0")
341341
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
345345
return result
346346

347347

@@ -378,7 +378,9 @@ def extract_context_custom_extractor(extractor, event, lambda_context):
378378
sampling_priority,
379379
) = extractor(event, lambda_context)
380380
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),
382384
)
383385
except Exception as e:
384386
logger.debug("The trace extractor returned with error %s", e)
@@ -480,7 +482,7 @@ def extract_dd_trace_context(
480482
return dd_trace_context, trace_context_source, event_source
481483

482484

483-
def get_dd_trace_context():
485+
def get_dd_trace_context_obj():
484486
"""
485487
Return the Datadog trace context to be propagated on the outgoing requests.
486488
@@ -517,9 +519,24 @@ def get_dd_trace_context():
517519
trace_id=dd_trace_context.trace_id,
518520
span_id=xray_context.span_id,
519521
sampling_priority=dd_trace_context.sampling_priority,
522+
meta=dd_trace_context._meta.copy(),
523+
metrics=dd_trace_context._metrics.copy(),
520524
)
521525

522526

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+
523540
def set_correlation_ids():
524541
"""
525542
Create a dummy span, and overrides its trace_id and span_id, to make
@@ -535,7 +552,7 @@ def set_correlation_ids():
535552
logger.debug("using ddtrace implementation for spans")
536553
return
537554

538-
context = get_dd_trace_context()
555+
context = get_dd_trace_context_obj()
539556
if not _is_context_complete(context):
540557
return
541558

0 commit comments

Comments
 (0)