Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from basictracer.propagator import Propagator
from basictracer.context import SpanContext
from opentracing import SpanContext as OTSpanContext
from opentracing import SpanContextCorruptedException
_LOG = getLogger(__name__)
_SINGLE_HEADER = "b3"
# Lower case is used here as the B3 specification recommends
_TRACEID = "x-b3-traceid"
_SPANID = "x-b3-spanid"
_PARENTSPANID = "x-b3-parentspanid"
_SAMPLED = "x-b3-sampled"
_FLAGS = "x-b3-flags"
class B3Propagator(Propagator):
"""
Propagator for the B3 HTTP header format.
See: https://github.com/openzipkin/b3-propagation
"""
def inject(self, span_context, carrier):
traceid = span_context.trace_id
spanid = span_context.span_id
baggage = span_context.baggage
parentspanid = baggage.pop(_PARENTSPANID, None)
if parentspanid is not None:
carrier[_PARENTSPANID] = parentspanid
from __future__ import absolute_import
from base64 import standard_b64decode
from base64 import standard_b64encode
from basictracer.context import SpanContext
from basictracer.propagator import Propagator
# This can cause problems when old versions of protobuf are installed
from opentracing import InvalidCarrierException
from lightstep.lightstep_carrier_pb2 import BinaryCarrier
from lightstep.lightstep_carrier_pb2 import BasicTracerCarrier
class LightStepBinaryPropagator(Propagator):
"""A BasicTracer Propagator for LightStepFormat.LIGHTSTEP_BINARY."""
def inject(self, span_context, carrier):
if type(carrier) is not bytearray:
raise InvalidCarrierException()
state = BinaryCarrier()
basic_ctx = state.basic_ctx
basic_ctx.trace_id = span_context.trace_id
basic_ctx.span_id = span_context.span_id
basic_ctx.sampled = span_context.sampled
if span_context.baggage is not None:
for key in span_context.baggage:
basic_ctx.baggage_items[key] = span_context.baggage[key]
if value in ('true', '1'):
return True
elif value in ('false', '0'):
return False
msg = (
'{field} got an invalid value {value!r}, '
"should be one of \'true\', \'false\', \'0\', \'1\'"
)
raise SpanContextCorruptedException(msg.format(
value=value,
field=field_name_sampled
))
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)
carrier[field_name_span_id] = '{0:x}'.format(span_context.span_id)
carrier[field_name_sampled] = str(span_context.sampled).lower()
if span_context.baggage is not None:
for k in span_context.baggage:
carrier[prefix_baggage+k] = span_context.baggage[k]
def extract(self, carrier): # noqa
count = 0
span_id, trace_id, sampled = (0, 0, False)
baggage = {}
for k in carrier:
v = carrier[k]
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):
"""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())