Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make an opt-out path for the BINARY format #10

Merged
merged 3 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions basictracer/binary_propagator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import absolute_import

import struct
from .context import SpanContext
from .propagator import Propagator
# This can cause problems when old versions of protobuf are installed
from .wire_pb2 import TracerState
from opentracing import InvalidCarrierException

_proto_size_bytes = 4 # bytes


class BinaryPropagator(Propagator):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the point of this change is that a BasicTracer extender may choose not to even import this BinaryPropagator file and its .wire_pb2 dependency that breaks some builds that also involve other version of protobufs.

"""A BasicTracer Propagator for Format.BINARY."""

def inject(self, span_context, carrier):
if type(carrier) is not bytearray:
raise InvalidCarrierException()
state = TracerState()
state.trace_id = span_context.trace_id
state.span_id = span_context.span_id
state.sampled = span_context.sampled
if span_context.baggage is not None:
for key in span_context.baggage:
state.baggage_items[key] = span_context.baggage[key]

# The binary format is {uint32}{protobuf} using big-endian for the uint
carrier.extend(struct.pack('>I', state.ByteSize()))
carrier.extend(state.SerializeToString())

def extract(self, carrier):
if type(carrier) is not bytearray:
raise InvalidCarrierException()
state = TracerState()
state.ParseFromString(str(carrier[_proto_size_bytes:]))
baggage = {}
for k in state.baggage_items:
baggage[k] = state.baggage_items[k]

return SpanContext(
span_id=state.span_id,
trace_id=state.trace_id,
baggage=baggage,
sampled=state.sampled)
15 changes: 15 additions & 0 deletions basictracer/propagator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import absolute_import

from abc import ABCMeta, abstractmethod


class Propagator(object):
__metaclass__ = ABCMeta

@abstractmethod
def inject(self, span_context, carrier):
pass

@abstractmethod
def extract(self, carrier):
pass
50 changes: 4 additions & 46 deletions basictracer/propagation.py → basictracer/text_propagator.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,8 @@
from __future__ import absolute_import

import struct
from opentracing import InvalidCarrierException, SpanContextCorruptedException
from opentracing import SpanContextCorruptedException
from .context import SpanContext
from .wire_pb2 import TracerState

_proto_size_bytes = 4 # bytes


class BinaryPropagator(object):

def __init__(self, tracer):
self.tracer = tracer

def inject(self, span_context, carrier):
if type(carrier) is not bytearray:
raise InvalidCarrierException()
state = TracerState()
state.trace_id = span_context.trace_id
state.span_id = span_context.span_id
state.sampled = span_context.sampled
if span_context.baggage is not None:
for key in span_context.baggage:
state.baggage_items[key] = span_context.baggage[key]

# The binary format is {uint32}{protobuf} using big-endian for the uint
carrier.extend(struct.pack('>I', state.ByteSize()))
carrier.extend(state.SerializeToString())

def extract(self, carrier):
if type(carrier) is not bytearray:
raise InvalidCarrierException()
state = TracerState()
state.ParseFromString(str(carrier[_proto_size_bytes:]))
baggage = {}
for k in state.baggage_items:
baggage[k] = state.baggage_items[k]

return SpanContext(
span_id=state.span_id,
trace_id=state.trace_id,
baggage=baggage,
sampled=state.sampled)

from .propagator import Propagator

prefix_tracer_state = 'ot-tracer-'
prefix_baggage = 'ot-baggage-'
Expand All @@ -52,10 +12,8 @@ def extract(self, carrier):
field_count = 3


class TextPropagator(object):

def __init__(self, tracer):
self.tracer = tracer
class TextPropagator(Propagator):
"""A BasicTracer Propagator for Format.TEXT_MAP."""

def inject(self, span_context, carrier):
carrier[field_name_trace_id] = '{0:x}'.format(span_context.trace_id)
Expand Down
48 changes: 32 additions & 16 deletions basictracer/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from opentracing import Format, Tracer
from opentracing import UnsupportedFormatException
from .context import SpanContext
from .propagation import BinaryPropagator, TextPropagator
from .recorder import SpanRecorder, DefaultSampler
from .span import BasicSpan
from .util import generate_id
Expand All @@ -13,12 +12,37 @@
class BasicTracer(Tracer):

def __init__(self, recorder=None, sampler=None):
"""Initialize a BasicTracer instance.

Note that the returned BasicTracer has *no* propagators registered. The
user should either call register_propagator() for each needed
inject/extract format and/or the user can simply call
register_required_propagators().

The required formats are opt-in because of protobuf version conflicts
with the binary carrier.
"""

super(BasicTracer, self).__init__()
self.recorder = NoopRecorder() if recorder is None else recorder
self.sampler = DefaultSampler(1) if sampler is None else sampler
self._binary_propagator = BinaryPropagator(self)
self._text_propagator = TextPropagator(self)
return
self._propagators = {}

def register_propagator(self, format, propagator):
"""Register a propagator with this BasicTracer.

:param string format: a Format identifier like Format.TEXT_MAP
:param Propagator propagator: a Propagator instance to handle
inject/extract calls involving `format`
"""
self._propagators[format] = propagator

def register_required_propagators(self):
from .text_propagator import TextPropagator
from .binary_propagator import BinaryPropagator
self.register_propagator(Format.TEXT_MAP, TextPropagator())
self.register_propagator(Format.HTTP_HEADERS, TextPropagator())
self.register_propagator(Format.BINARY, BinaryPropagator())

def start_span(
self,
Expand Down Expand Up @@ -61,22 +85,14 @@ def start_span(
start_time=start_time)

def inject(self, span_context, format, carrier):
if format == Format.BINARY:
self._binary_propagator.inject(span_context, carrier)
elif format == Format.TEXT_MAP:
self._text_propagator.inject(span_context, carrier)
elif format == Format.HTTP_HEADERS:
self._text_propagator.inject(span_context, carrier)
if format in self._propagators:
self._propagators[format].inject(span_context, carrier)
else:
raise UnsupportedFormatException()

def extract(self, format, carrier):
if format == Format.BINARY:
return self._binary_propagator.extract(carrier)
elif format == Format.TEXT_MAP:
return self._text_propagator.extract(carrier)
elif format == Format.HTTP_HEADERS:
return self._text_propagator.extract(carrier)
if format in self._propagators:
return self._propagators[format].extract(carrier)
else:
raise UnsupportedFormatException()

Expand Down
4 changes: 3 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

class APICheckBasicTracer(unittest.TestCase, APICompatibilityCheckMixin):
def tracer(self):
return BasicTracer()
t = BasicTracer()
t.register_required_propagators()
return t

def check_baggage_values(self):
return True
4 changes: 3 additions & 1 deletion tests/test_propagation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import pytest
from opentracing import child_of, Format, UnsupportedFormatException
from opentracing import Format, UnsupportedFormatException
from basictracer import BasicTracer


def test_propagation():
tracer = BasicTracer()
tracer.register_required_propagators()
sp = tracer.start_span(operation_name='test')
sp.context.sampled = False
sp.context.set_baggage_item('foo', 'bar')
Expand All @@ -30,6 +31,7 @@ def test_propagation():
def test_start_span():
""" Test in process child span creation."""
tracer = BasicTracer()
tracer.register_required_propagators()
sp = tracer.start_span(operation_name='test')
sp.context.set_baggage_item('foo', 'bar')

Expand Down