Skip to content

Commit

Permalink
Merge pull request #4 from instana/py3_support
Browse files Browse the repository at this point in the history
Add python 3 compatibility
  • Loading branch information
pglombardo authored Jul 10, 2017
2 parents 6bedf12 + c12e7c6 commit 40e6636
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: python
python:
- "2.7"
- "3.6"
install: "pip install -r requirements.txt"
script: nosetests -v
11 changes: 8 additions & 3 deletions instana/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import opentracing.ext.tags as ext
import socket
import instana.span as sd
import Queue
import time
import os

import sys
if sys.version_info.major is 2:
import Queue as queue
else:
import queue


class InstanaRecorder(SpanRecorder):
sensor = None
registered_spans = ("django", "memcache", "rpc-client", "rpc-server")
entry_kind = ["entry", "server", "consumer"]
exit_kind = ["exit", "client", "producer"]
queue = Queue.Queue()
queue = queue.Queue()

def __init__(self, sensor):
super(InstanaRecorder, self).__init__()
Expand Down Expand Up @@ -46,7 +51,7 @@ def queued_spans(self):
while True:
try:
s = self.queue.get(False)
except Queue.Empty:
except queue.Empty:
break
else:
spans.append(s)
Expand Down
10 changes: 8 additions & 2 deletions instana/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import struct
import binascii

import sys
if sys.version_info.major is 2:
string_types = basestring
else:
string_types = str

_rnd = random.Random()
_current_pid = 0

Expand All @@ -26,13 +32,13 @@ def id_to_header(id):
return ""

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


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

if not isinstance(header, basestring):
if not isinstance(header, string_types):
return 0

# Pad the header to 16 chars
Expand Down
16 changes: 11 additions & 5 deletions tests/test_id_management.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import instana.util
import string
from nose.tools import assert_equals
import sys

if sys.version_info.major is 2:
string_types = basestring
else:
string_types = str


def test_id_generation():
Expand Down Expand Up @@ -72,15 +78,15 @@ def test_id_to_header_conversion():
converted_id = instana.util.id_to_header(original_id)

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

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

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


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

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

# 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, basestring)
assert isinstance(converted_id, string_types)
assert converted_id == ''

# 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, basestring)
assert isinstance(converted_id, string_types)
assert converted_id == ''


Expand Down

0 comments on commit 40e6636

Please sign in to comment.