Skip to content

Commit

Permalink
Merge pull request #83 from lightstep/truncating_padding_b3
Browse files Browse the repository at this point in the history
Remove padding/truncating on B3 trace_id
  • Loading branch information
ocelotl authored Nov 16, 2019
2 parents 719e21a + d4b9ff3 commit 398ef68
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 32 deletions.
18 changes: 11 additions & 7 deletions lightstep/b3_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,38 @@ def inject(self, span_context, carrier):
carrier[_FLAGS] = flags

sampled = baggage.pop(_SAMPLED, None)
if sampled is not None:

if sampled is None:
carrier[_SAMPLED] = 1
else:
if flags == 1:
_LOG.warning(
"x-b3-flags: 1 implies x-b3-sampled: 1, not sending "
"the value of x-b3-sampled"
)
else:
if sampled in [True, False]:
if isinstance(sampled, bool):
warn(
"The value of x-b3-sampled should "
"be {} instead of {}".format(
int(sampled), sampled
)
)
carrier[_SAMPLED] = int(sampled)
carrier[_SAMPLED] = sampled

if sampled is flags is (traceid and spanid) is None:
warn(
"If not propagating only the sampling state, traceid and "
"spanid must be defined"
"spanid must be defined, setting sampling state to 1."
)
carrier[_SAMPLED] = 1

carrier.update(baggage)

if traceid is not None:
carrier[_TRACEID] = format(traceid, "x").ljust(32, "0")
carrier[_TRACEID] = format(traceid, "x")
if spanid is not None:
carrier[_SPANID] = format(spanid, "016x")
carrier[_SPANID] = format(spanid, "x")

def extract(self, carrier):

Expand Down Expand Up @@ -127,7 +131,7 @@ def extract(self, carrier):
"the received value of x-b3-sampled"
)
elif sampled is not None:
baggage[_SAMPLED] = int(sampled, 16)
baggage[_SAMPLED] = sampled

baggage.update(carrier)

Expand Down
34 changes: 23 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@
long_description='',
author='LightStep',
license='',
install_requires=['thrift>=0.10.0,<0.12.0',
'jsonpickle',
'six',
'basictracer>=3.0,<4',
'googleapis-common-protos>=1.5.3,<2.0',
'requests>=2.19,<3.0',
'protobuf>=3.6.0,<4.0'],
tests_require=['pytest',
'sphinx',
'sphinx-epytext'],
install_requires=[
'thrift>=0.10.0,<0.12.0',
'jsonpickle',
'six',
'basictracer>=3.0,<4',
'googleapis-common-protos>=1.5.3,<2.0',
'requests>=2.19,<3.0',
'protobuf>=3.6.0,<4.0'
],
tests_require=[
'pytest',
'sphinx',
'sphinx-epytext',
'ipdb'
],
classifiers=[
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
],

keywords=[ 'opentracing', 'lightstep', 'traceguide', 'tracing', 'microservices', 'distributed' ],
keywords=[
'opentracing',
'lightstep',
'traceguide',
'tracing',
'microservices',
'distributed'
],
packages=find_packages(exclude=['docs*', 'tests*', 'sample*']),
)
97 changes: 83 additions & 14 deletions tests/b3_propagator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ def test_inject(self):
self.assertEqual(
carrier,
{
"x-b3-traceid": (
format(span.context.trace_id, "x").ljust(32, "0")
),
"x-b3-spanid": format(span.context.span_id, "016x"),
"x-b3-traceid": format(span.context.trace_id, "x"),
"x-b3-spanid": format(span.context.span_id, "x"),
"x-b3-sampled": 1,
"checked": "baggage"
}
)
Expand All @@ -46,10 +45,8 @@ def test_inject(self):
self.assertEqual(
carrier,
{
"x-b3-traceid": (
format(span.context.trace_id, "x").ljust(32, "0")
),
"x-b3-spanid": format(span.context.span_id, "016x"),
"x-b3-traceid": format(span.context.trace_id, "x"),
"x-b3-spanid": format(span.context.span_id, "x"),
"x-b3-flags": 1,
}
)
Expand All @@ -59,8 +56,8 @@ def test_extract_multiple_headers(self):
result = self.tracer().extract(
Format.HTTP_HEADERS,
{
"x-b3-traceid": format(12, "032x"),
"x-b3-spanid": format(345, "016x"),
"x-b3-traceid": format(12, "x"),
"x-b3-spanid": format(345, "x"),
"checked": "baggage"
}
)
Expand All @@ -72,8 +69,8 @@ def test_extract_multiple_headers(self):
result = self.tracer().extract(
Format.HTTP_HEADERS,
{
"x-b3-traceid": format(12, "032x"),
"x-b3-spanid": format(345, "016x"),
"x-b3-traceid": format(12, "x"),
"x-b3-spanid": format(345, "x"),
"x-b3-flags": 1,
"x-b3-sampled": 0
}
Expand Down Expand Up @@ -126,7 +123,7 @@ def test_invalid_traceid_spanid(self):
self.tracer().extract(
Format.HTTP_HEADERS,
{
"x-b3-spanid": format(345, "016x"),
"x-b3-spanid": format(345, "x"),
"checked": "baggage"
}
)
Expand All @@ -135,7 +132,79 @@ def test_invalid_traceid_spanid(self):
self.tracer().extract(
Format.HTTP_HEADERS,
{
"x-b3-traceid": format(345, "032x"),
"x-b3-traceid": format(345, "x"),
"checked": "baggage"
}
)

def test_propagation(self):

tracer = self.tracer()

def test_attribute(attribute_name, attribute_value):
inject_span = tracer.start_span("test_propagation")
setattr(
inject_span.context, attribute_name, int(attribute_value, 16)
)

carrier = {}
tracer.inject(inject_span.context, Format.HTTP_HEADERS, carrier)

self.assertEqual(
carrier["x-b3-{}".format(attribute_name.replace("_", ""))],
attribute_value
)

extract_span_context = tracer.extract(Format.HTTP_HEADERS, carrier)

self.assertEqual(
getattr(inject_span.context, attribute_name),
getattr(extract_span_context, attribute_name)
)

test_attribute("trace_id", "ef5705a090040838f1359ebafa5c0c6")
test_attribute("trace_id", "ef5705a09004083")
test_attribute("span_id", "aef5705a09004083")

def test_sampled(sampled_value):

inject_span = tracer.start_span("test_propagation")
inject_span.context.baggage["x-b3-sampled"] = sampled_value

carrier = {}
tracer.inject(inject_span.context, Format.HTTP_HEADERS, carrier)

self.assertTrue(
isinstance(carrier["x-b3-sampled"], type(sampled_value))
)

extract_span_context = tracer.extract(Format.HTTP_HEADERS, carrier)

self.assertEqual(
carrier["x-b3-sampled"],
extract_span_context.baggage["x-b3-sampled"]
)

test_sampled(True)
test_sampled(False)
test_sampled(1)
test_sampled(0)

inject_span = tracer.start_span("test_propagation")

self.assertTrue(
"x-b3-sampled" not in inject_span.context.baggage.keys()
)

carrier = {}

tracer.inject(inject_span.context, Format.HTTP_HEADERS, carrier)

self.assertEqual(carrier["x-b3-sampled"], 1)

extract_span_context = tracer.extract(Format.HTTP_HEADERS, carrier)

self.assertEqual(
carrier["x-b3-sampled"],
extract_span_context.baggage["x-b3-sampled"]
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ envlist = py27, py34, py35, py36, py37
deps =
pytest
mock
ipdb
commands =
python -m pytest {posargs:tests}

0 comments on commit 398ef68

Please sign in to comment.