From cc0290ae0bdaf3af5818a72c5ec2adff16182370 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 10 Jul 2017 14:11:09 +0200 Subject: [PATCH 1/3] Support py2 + py3 queue (versus Queue) --- instana/recorder.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/instana/recorder.py b/instana/recorder.py index 91c0d59d..c9dcb8b9 100644 --- a/instana/recorder.py +++ b/instana/recorder.py @@ -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__() @@ -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) From 4ac69f41b0a01d69f5b649ba8ed5476ef23f2838 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 10 Jul 2017 14:24:59 +0200 Subject: [PATCH 2/3] Decode bytestring to utf8; Add py3 basestring equivalent --- instana/util.py | 10 ++++++++-- tests/test_id_management.py | 16 +++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/instana/util.py b/instana/util.py index 8eb6ff5e..0b98ef53 100644 --- a/instana/util.py +++ b/instana/util.py @@ -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 @@ -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 diff --git a/tests/test_id_management.py b/tests/test_id_management.py index 0c2877be..c74d981d 100644 --- a/tests/test_id_management.py +++ b/tests/test_id_management.py @@ -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(): @@ -72,7 +78,7 @@ 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 @@ -80,7 +86,7 @@ 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) @@ -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 == '' From c12e7c6ec8abc0654d73efb4a74883de06bb3e74 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 10 Jul 2017 14:25:24 +0200 Subject: [PATCH 3/3] Add Python 3.6 to test matrix --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index f6162109..333155a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: python python: - "2.7" + - "3.6" install: "pip install -r requirements.txt" script: nosetests -v