Skip to content
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
17 changes: 10 additions & 7 deletions instana/text_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@

class TextPropagator():
"""
A Propagator for Format.TEXT_MAP.
Does not support Baggage and Sampling
A Propagator for 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)
if span_context.baggage is not None:
for k in span_context.baggage:
carrier[prefix_baggage+k] = span_context.baggage[k]
try:
carrier[field_name_trace_id] = '{0:x}'.format(span_context.trace_id)
carrier[field_name_span_id] = '{0:x}'.format(span_context.span_id)
if span_context.baggage is not None:
for k in span_context.baggage:
carrier[prefix_baggage+k] = span_context.baggage[k]
except Exception as e:
log.debug("inject error: ", str(e))

def extract(self, carrier): # noqa
try:
Expand All @@ -43,3 +45,4 @@ def extract(self, carrier): # noqa

except Exception as e:
log.debug("extract error: ", str(e))
return SpanContext()
33 changes: 23 additions & 10 deletions instana/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
import struct
import binascii
from instana import log

import sys
if sys.version_info.major is 2:
Expand All @@ -13,6 +14,8 @@
_rnd = random.Random()
_current_pid = 0

BAD_ID_LONG = 3135097598 # Bad Cafe in base 10
BAD_ID_HEADER = "BADDCAFE" # Bad Cafe

def generate_id():
""" Generate a 64bit signed integer for use as a Span or Trace ID """
Expand All @@ -28,20 +31,30 @@ def generate_id():
def id_to_header(id):
""" Convert a 64bit signed integer to an unsigned base 16 hex string """

if not isinstance(id, int):
return ""
try:
if not isinstance(id, int):
return BAD_ID_HEADER

byteString = struct.pack('>q', id)
return str(binascii.hexlify(byteString).decode('UTF-8').lstrip('0'))
byteString = struct.pack('>q', id)
return str(binascii.hexlify(byteString).decode('UTF-8').lstrip('0'))
except Exception as e:
log.debug(e)
return BAD_ID_HEADER


def header_to_id(header):
""" Convert an unsigned base 16 hex string into a 64bit signed integer """

if not isinstance(header, string_types):
return 0

# Pad the header to 16 chars
header = header.zfill(16)
r = binascii.unhexlify(header)
return struct.unpack('>q', r)[0]
return BAD_ID_LONG

try:
# Test that header is truly a hexadecimal value before we try to convert
int(header, 16)

# Pad the header to 16 chars
header = header.zfill(16)
r = binascii.unhexlify(header)
return struct.unpack('>q', r)[0]
except ValueError:
return BAD_ID_LONG
12 changes: 6 additions & 6 deletions tests/test_id_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,21 @@ def test_id_to_header_conversion_with_bogus_id():

# Assert that it is a string and there are no non-hex characters
assert isinstance(converted_id, string_types)
assert converted_id == ''
assert converted_id == instana.util.BAD_ID_HEADER

# Test passing a nil
converted_id = instana.util.id_to_header(None)

# Assert that it is a string and there are no non-hex characters
assert isinstance(converted_id, string_types)
assert converted_id == ''
assert converted_id == instana.util.BAD_ID_HEADER

# Test passing an Array
converted_id = instana.util.id_to_header([])

# Assert that it is a string and there are no non-hex characters
assert isinstance(converted_id, string_types)
assert converted_id == ''
assert converted_id == instana.util.BAD_ID_HEADER


def test_header_to_id_conversion():
Expand All @@ -125,12 +125,12 @@ def test_header_to_id_conversion():
def test_header_to_id_conversion_with_bogus_header():
# Bogus nil arg
bogus_result = instana.util.header_to_id(None)
assert_equals(0, bogus_result)
assert_equals(instana.util.BAD_ID_LONG, bogus_result)

# Bogus Integer arg
bogus_result = instana.util.header_to_id(1234)
assert_equals(0, bogus_result)
assert_equals(instana.util.BAD_ID_LONG, bogus_result)

# Bogus Array arg
bogus_result = instana.util.header_to_id([1234])
assert_equals(0, bogus_result)
assert_equals(instana.util.BAD_ID_LONG, bogus_result)
14 changes: 14 additions & 0 deletions tests/test_opentracing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from opentracing.harness.api_check import APICompatibilityCheckMixin
from instana.tracer import InstanaTracer
from nose.plugins.skip import SkipTest


class TestInstanaTracer(InstanaTracer, APICompatibilityCheckMixin):
def tracer(self):
return self

def test_binary_propagation(self):
raise SkipTest('Binary format is not supported')

def test_mandatory_formats(self):
raise SkipTest('Binary format is not supported')