Skip to content

Commit 1acee9a

Browse files
authored
Add OT tests - Tracer Validations; Add more exception handling (#62)
Add OT tests - Tracer Validations; Add more exception handling
1 parent 7279ef3 commit 1acee9a

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

instana/text_propagator.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212

1313
class TextPropagator():
1414
"""
15-
A Propagator for Format.TEXT_MAP.
16-
Does not support Baggage and Sampling
15+
A Propagator for TEXT_MAP.
1716
"""
1817

1918
def inject(self, span_context, carrier):
20-
carrier[field_name_trace_id] = '{0:x}'.format(span_context.trace_id)
21-
carrier[field_name_span_id] = '{0:x}'.format(span_context.span_id)
22-
if span_context.baggage is not None:
23-
for k in span_context.baggage:
24-
carrier[prefix_baggage+k] = span_context.baggage[k]
19+
try:
20+
carrier[field_name_trace_id] = '{0:x}'.format(span_context.trace_id)
21+
carrier[field_name_span_id] = '{0:x}'.format(span_context.span_id)
22+
if span_context.baggage is not None:
23+
for k in span_context.baggage:
24+
carrier[prefix_baggage+k] = span_context.baggage[k]
25+
except Exception as e:
26+
log.debug("inject error: ", str(e))
2527

2628
def extract(self, carrier): # noqa
2729
try:
@@ -43,3 +45,4 @@ def extract(self, carrier): # noqa
4345

4446
except Exception as e:
4547
log.debug("extract error: ", str(e))
48+
return SpanContext()

instana/util.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import struct
55
import binascii
6+
from instana import log
67

78
import sys
89
if sys.version_info.major is 2:
@@ -13,6 +14,8 @@
1314
_rnd = random.Random()
1415
_current_pid = 0
1516

17+
BAD_ID_LONG = 3135097598 # Bad Cafe in base 10
18+
BAD_ID_HEADER = "BADDCAFE" # Bad Cafe
1619

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

31-
if not isinstance(id, int):
32-
return ""
34+
try:
35+
if not isinstance(id, int):
36+
return BAD_ID_HEADER
3337

34-
byteString = struct.pack('>q', id)
35-
return str(binascii.hexlify(byteString).decode('UTF-8').lstrip('0'))
38+
byteString = struct.pack('>q', id)
39+
return str(binascii.hexlify(byteString).decode('UTF-8').lstrip('0'))
40+
except Exception as e:
41+
log.debug(e)
42+
return BAD_ID_HEADER
3643

3744

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

4148
if not isinstance(header, string_types):
42-
return 0
43-
44-
# Pad the header to 16 chars
45-
header = header.zfill(16)
46-
r = binascii.unhexlify(header)
47-
return struct.unpack('>q', r)[0]
49+
return BAD_ID_LONG
50+
51+
try:
52+
# Test that header is truly a hexadecimal value before we try to convert
53+
int(header, 16)
54+
55+
# Pad the header to 16 chars
56+
header = header.zfill(16)
57+
r = binascii.unhexlify(header)
58+
return struct.unpack('>q', r)[0]
59+
except ValueError:
60+
return BAD_ID_LONG

tests/test_id_management.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,21 @@ def test_id_to_header_conversion_with_bogus_id():
9696

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

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

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

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

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

115115

116116
def test_header_to_id_conversion():
@@ -125,12 +125,12 @@ def test_header_to_id_conversion():
125125
def test_header_to_id_conversion_with_bogus_header():
126126
# Bogus nil arg
127127
bogus_result = instana.util.header_to_id(None)
128-
assert_equals(0, bogus_result)
128+
assert_equals(instana.util.BAD_ID_LONG, bogus_result)
129129

130130
# Bogus Integer arg
131131
bogus_result = instana.util.header_to_id(1234)
132-
assert_equals(0, bogus_result)
132+
assert_equals(instana.util.BAD_ID_LONG, bogus_result)
133133

134134
# Bogus Array arg
135135
bogus_result = instana.util.header_to_id([1234])
136-
assert_equals(0, bogus_result)
136+
assert_equals(instana.util.BAD_ID_LONG, bogus_result)

tests/test_opentracing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from opentracing.harness.api_check import APICompatibilityCheckMixin
2+
from instana.tracer import InstanaTracer
3+
from nose.plugins.skip import SkipTest
4+
5+
6+
class TestInstanaTracer(InstanaTracer, APICompatibilityCheckMixin):
7+
def tracer(self):
8+
return self
9+
10+
def test_binary_propagation(self):
11+
raise SkipTest('Binary format is not supported')
12+
13+
def test_mandatory_formats(self):
14+
raise SkipTest('Binary format is not supported')

0 commit comments

Comments
 (0)