Skip to content

Commit ec08f6c

Browse files
committed
Adjust our tracer to work fine with Thrift under Python 2/3.
1. Add an additional pass over our Thrift generated files to have code that is compatible with both 2 and 3. This requires the six module. 2. Require thrift >= 0.10.0, as this is the first release that supports both 2 and 3. 3. Update our util function to coerce strings, as the thrift Python library expects us to give it strings in its default type (ascii under 2, utf8 under 3).
1 parent 79855c1 commit ec08f6c

File tree

8 files changed

+86
-50
lines changed

8 files changed

+86
-50
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ test: build
5757
# the command is run within the LightStep development environment (i.e. the
5858
# LIGHTSTEP_HOME environment variable is set).
5959
thrift:
60-
docker run -v "$(PWD)/lightstep:/out" -v "$(LIGHTSTEP_HOME)/go/src/crouton:/data" --rm thrift:0.9.2 \
60+
docker run -v "$(PWD)/lightstep:/out" -v "$(LIGHTSTEP_HOME)/go/src/crouton:/data" --rm thrift:0.10.0 \
6161
thrift -r --gen py -out /out /data/crouton.thrift
62+
python-modernize -w lightstep/crouton/
6263
rm -rf lightstep/crouton/ReportingService-remote

lightstep/crouton/ReportingService.py

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lightstep/crouton/ttypes.py

+31-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lightstep/util.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" Utility functions
22
"""
33
import random
4+
import sys
45
import time
56
import math
67
from . import constants
@@ -52,15 +53,38 @@ def _merge_dicts(*dict_args):
5253
result.update(dictionary)
5354
return result if result else None
5455

55-
def _coerce_str(str_or_unicode):
56-
if isinstance(str_or_unicode, bytes):
57-
return str_or_unicode
58-
else:
56+
if sys.version_info[0] == 2:
57+
58+
# Coerce to ascii (bytes) under Python 2.
59+
def _coerce_str(val):
60+
return _coerce_to_bytes(val)
61+
else:
62+
63+
# Coerce to utf-8 under Python 3.
64+
def _coerce_str(val):
65+
return _coerce_to_unicode(val)
66+
67+
def _coerce_to_bytes(val):
68+
if isinstance(val, bytes):
69+
return val
70+
try:
71+
return val.encode('utf-8', 'replace')
72+
except Exception:
5973
try:
60-
return str_or_unicode.encode('utf-8', 'replace')
74+
return bytes(val)
6175
except Exception:
62-
try:
63-
return bytes(str_or_unicode)
64-
except Exception:
65-
# Never let these errors bubble up
66-
return '(encoding error)'
76+
# Never let these errors bubble up
77+
return '(encoding error)'
78+
79+
def _coerce_to_unicode(val):
80+
if isinstance(val, str):
81+
return val
82+
try:
83+
return val.decode('utf-8')
84+
except Exception:
85+
try:
86+
return str(val)
87+
except Exception:
88+
# Never let these errors bubble up
89+
return '(encoding error)'
90+

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
long_description='',
88
author='LightStep',
99
license='',
10-
install_requires=['thrift==0.9.2',
10+
install_requires=['thrift==0.10.0',
1111
'jsonpickle',
1212
'six',
1313
'basictracer>=2.2,<2.3'],
1414
tests_require=['pytest',
1515
'sphinx',
1616
'sphinx-epytext'],
17-
1817
classifiers=[
1918
'Operating System :: OS Independent',
2019
'Programming Language :: Python :: 2',

tests/recorder_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def check_spans(self, spans):
126126
"""Checks spans' name.
127127
"""
128128
for i, span in enumerate(spans):
129-
name = six.b(str(i)) # name *must* be in bytes/utf-8
130-
self.assertEqual(span.span_name, name)
129+
self.assertEqual(span.span_name, str(i))
131130

132131
def dummy_basic_span(self, recorder, i):
133132
return BasicSpan(

0 commit comments

Comments
 (0)