Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Fix B3Codec's trace_id zero-padding for 32bit IDs (#322)
Browse files Browse the repository at this point in the history
* fix B3Codec inject bug for traceid

Signed-off-by: mizhexiaoxiao <[email protected]>

* add unit test

Signed-off-by: mizhexiaoxiao <[email protected]>

* format

Signed-off-by: mizhexiaoxiao <[email protected]>

* make lint

Signed-off-by: mizhexiaoxiao <[email protected]>
  • Loading branch information
mizhexiaoxiao authored Aug 29, 2021
1 parent 9ccc29c commit 143e77a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion jaeger_client/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,16 @@ class B3Codec(Codec):
flags_header = 'X-B3-Flags'
_flags_header_lc = flags_header.lower()

def __init__(self, generate_128bit_trace_id=False):
self.generate_128bit_trace_id = generate_128bit_trace_id

def inject(self, span_context, carrier):
if not isinstance(carrier, dict):
raise InvalidCarrierException('carrier not a dictionary')
carrier[self.trace_header] = format(span_context.trace_id, 'x').zfill(16)
if self.generate_128bit_trace_id:
carrier[self.trace_header] = format(span_context.trace_id, 'x').zfill(32)
else:
carrier[self.trace_header] = format(span_context.trace_id, 'x').zfill(16)
carrier[self.span_header] = format(span_context.span_id, 'x').zfill(16)
if span_context.parent_id is not None:
carrier[self.parent_span_header] = format(span_context.parent_id, 'x').zfill(16)
Expand Down
4 changes: 3 additions & 1 deletion jaeger_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ def propagation(self):
propagation = self.config.get('propagation')
if propagation == 'b3':
# replace the codec with a B3 enabled instance
return {Format.HTTP_HEADERS: B3Codec()}
return {Format.HTTP_HEADERS: B3Codec(
generate_128bit_trace_id=self.generate_128bit_trace_id
)}
return {}

def throttler_group(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ def test_zipkin_b3_codec_extract_injected(self):
assert extracted.parent_id == ctx.parent_id
assert extracted.flags == ctx.flags

def test_128bit_trace_id_with_zero_padding(self):
codec = B3Codec(generate_128bit_trace_id=True)

carrier_1 = {'X-B3-SpanId': '39fe73de0012a0e5', 'X-B3-ParentSpanId': '3dbf8a511e159b05',
'X-B3-TraceId': '023f352eaefd8b887a06732f5312e2de', 'X-B3-Flags': '0'}
span_context = codec.extract(carrier_1)

carrier_2 = {}
codec.inject(span_context=span_context, carrier=carrier_2)
assert carrier_1['X-B3-TraceId'] == carrier_2['X-B3-TraceId']

def test_binary_codec(self):
codec = BinaryCodec()
with self.assertRaises(InvalidCarrierException):
Expand Down

0 comments on commit 143e77a

Please sign in to comment.