diff --git a/instana/text_propagator.py b/instana/text_propagator.py index db7c6ba5..8a5d548c 100644 --- a/instana/text_propagator.py +++ b/instana/text_propagator.py @@ -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: @@ -43,3 +45,4 @@ def extract(self, carrier): # noqa except Exception as e: log.debug("extract error: ", str(e)) + return SpanContext() diff --git a/instana/util.py b/instana/util.py index 64fdc905..8716bd8f 100644 --- a/instana/util.py +++ b/instana/util.py @@ -3,6 +3,7 @@ import time import struct import binascii +from instana import log import sys if sys.version_info.major is 2: @@ -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 """ @@ -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 diff --git a/tests/test_id_management.py b/tests/test_id_management.py index c74d981d..225b26bd 100644 --- a/tests/test_id_management.py +++ b/tests/test_id_management.py @@ -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(): @@ -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) diff --git a/tests/test_opentracing.py b/tests/test_opentracing.py new file mode 100644 index 00000000..33c91e85 --- /dev/null +++ b/tests/test_opentracing.py @@ -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')