Skip to content

Commit 40e6636

Browse files
authored
Merge pull request #4 from instana/py3_support
Add python 3 compatibility
2 parents 6bedf12 + c12e7c6 commit 40e6636

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: python
22
python:
33
- "2.7"
4+
- "3.6"
45
install: "pip install -r requirements.txt"
56
script: nosetests -v

instana/recorder.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
import opentracing.ext.tags as ext
55
import socket
66
import instana.span as sd
7-
import Queue
87
import time
98
import os
109

10+
import sys
11+
if sys.version_info.major is 2:
12+
import Queue as queue
13+
else:
14+
import queue
15+
1116

1217
class InstanaRecorder(SpanRecorder):
1318
sensor = None
1419
registered_spans = ("django", "memcache", "rpc-client", "rpc-server")
1520
entry_kind = ["entry", "server", "consumer"]
1621
exit_kind = ["exit", "client", "producer"]
17-
queue = Queue.Queue()
22+
queue = queue.Queue()
1823

1924
def __init__(self, sensor):
2025
super(InstanaRecorder, self).__init__()
@@ -46,7 +51,7 @@ def queued_spans(self):
4651
while True:
4752
try:
4853
s = self.queue.get(False)
49-
except Queue.Empty:
54+
except queue.Empty:
5055
break
5156
else:
5257
spans.append(s)

instana/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import struct
55
import binascii
66

7+
import sys
8+
if sys.version_info.major is 2:
9+
string_types = basestring
10+
else:
11+
string_types = str
12+
713
_rnd = random.Random()
814
_current_pid = 0
915

@@ -26,13 +32,13 @@ def id_to_header(id):
2632
return ""
2733

2834
byteString = struct.pack('>q', id)
29-
return binascii.hexlify(byteString).lstrip('0')
35+
return binascii.hexlify(byteString).decode('UTF-8').lstrip('0')
3036

3137

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

35-
if not isinstance(header, basestring):
41+
if not isinstance(header, string_types):
3642
return 0
3743

3844
# Pad the header to 16 chars

tests/test_id_management.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import instana.util
22
import string
33
from nose.tools import assert_equals
4+
import sys
5+
6+
if sys.version_info.major is 2:
7+
string_types = basestring
8+
else:
9+
string_types = str
410

511

612
def test_id_generation():
@@ -72,15 +78,15 @@ def test_id_to_header_conversion():
7278
converted_id = instana.util.id_to_header(original_id)
7379

7480
# Assert that it is a string and there are no non-hex characters
75-
assert isinstance(converted_id, basestring)
81+
assert isinstance(converted_id, string_types)
7682
assert all(c in string.hexdigits for c in converted_id)
7783

7884
# Test passing a standard Integer ID as a String
7985
original_id = instana.util.generate_id()
8086
converted_id = instana.util.id_to_header(original_id)
8187

8288
# Assert that it is a string and there are no non-hex characters
83-
assert isinstance(converted_id, basestring)
89+
assert isinstance(converted_id, string_types)
8490
assert all(c in string.hexdigits for c in converted_id)
8591

8692

@@ -89,21 +95,21 @@ def test_id_to_header_conversion_with_bogus_id():
8995
converted_id = instana.util.id_to_header('')
9096

9197
# Assert that it is a string and there are no non-hex characters
92-
assert isinstance(converted_id, basestring)
98+
assert isinstance(converted_id, string_types)
9399
assert converted_id == ''
94100

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

98104
# Assert that it is a string and there are no non-hex characters
99-
assert isinstance(converted_id, basestring)
105+
assert isinstance(converted_id, string_types)
100106
assert converted_id == ''
101107

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

105111
# Assert that it is a string and there are no non-hex characters
106-
assert isinstance(converted_id, basestring)
112+
assert isinstance(converted_id, string_types)
107113
assert converted_id == ''
108114

109115

0 commit comments

Comments
 (0)