-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from opentracing/bhs/disable_binary_format
Make an opt-out path for the BINARY format
- Loading branch information
Showing
6 changed files
with
101 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
"""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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters