Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 05bcb4a

Browse files
author
Alexander
committedMay 2, 2017
Fixed most tests
1 parent 1373dcd commit 05bcb4a

12 files changed

+41
-27
lines changed
 

‎jaeger_client/codecs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def inject(self, span_context, carrier):
7676
carrier['%s%s' % (self.baggage_prefix, key)] = encoded_value
7777

7878
def extract(self, carrier):
79-
if not hasattr(carrier, 'iteritems'):
79+
if not isinstance(carrier, dict):
8080
raise InvalidCarrierException('carrier not a collection')
8181
trace_id, span_id, parent_id, flags = None, None, None, None
8282
baggage = None

‎jaeger_client/constants.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
DEFAULT_FLUSH_INTERVAL = 1
3434

3535
# Name of the HTTP header used to encode trace ID
36-
TRACE_ID_HEADER = b'uber-trace-id'
36+
TRACE_ID_HEADER = 'uber-trace-id'
3737

3838
# Prefix for HTTP headers used to record baggage items
39-
BAGGAGE_HEADER_PREFIX = b'uberctx-'
39+
BAGGAGE_HEADER_PREFIX = 'uberctx-'
4040

4141
# The name of HTTP header or a TextMap carrier key which, if found in the
4242
# carrier, forces the trace to be sampled as "debug" trace. The value of the

‎jaeger_client/sampler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def update(self, lower_bound, rate):
238238

239239
def __str__(self):
240240
return 'GuaranteedThroughputProbabilisticSampler(%s, %s, %s)' \
241-
% (self.operation, self.rate, self.lower_bound)
241+
% (self.operation, self.rate, round(float(self.lower_bound), 14))
242242

243243

244244
class AdaptiveSampler(Sampler):
@@ -314,7 +314,7 @@ def close(self):
314314

315315
def __str__(self):
316316
return 'AdaptiveSampler(%s, %s, %s)' \
317-
% (self.default_sampling_probability, self.lower_bound,
317+
% (self.default_sampling_probability, round(float(self.lower_bound), 14),
318318
self.max_operations)
319319

320320

‎jaeger_client/thrift.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import socket
2323
import struct
24+
import sys
2425

2526
import jaeger_client.thrift_gen.zipkincore.ZipkinCollector as zipkin_collector
2627
import jaeger_client.thrift_gen.sampling.SamplingManager as sampling_manager
@@ -35,6 +36,10 @@
3536
_max_unsigned_id = (1 << 64)
3637

3738

39+
def str_to_binary(value):
40+
return value if sys.version_info[0] == 2 else value.encode('utf-8')
41+
42+
3843
def ipv4_to_int(ipv4):
3944
if ipv4 == 'localhost':
4045
ipv4 = '127.0.0.1'
@@ -62,6 +67,12 @@ def port_to_int(port):
6267
def id_to_int(big_id):
6368
# zipkincore.thrift defines ID fields as i64, which is signed,
6469
# therefore we convert large IDs (> 2^63) to negative longs
70+
71+
# In Python 2, expression None > 1 is legal and has a value of False
72+
# In Python 3, this expression is illegal - so we need to have an additional check
73+
if big_id is None:
74+
return None
75+
6576
if big_id > _max_signed_id:
6677
big_id -= _max_unsigned_id
6778
return big_id
@@ -79,8 +90,9 @@ def make_endpoint(ipv4, port, service_name):
7990
def make_string_tag(key, value):
8091
if len(value) > 256:
8192
value = value[:256]
93+
8294
return zipkin_collector.BinaryAnnotation(
83-
key, value, zipkin_collector.AnnotationType.STRING)
95+
key, str_to_binary(value), zipkin_collector.AnnotationType.STRING)
8496

8597

8698
def make_peer_address_tag(key, host):
@@ -91,7 +103,7 @@ def make_peer_address_tag(key, host):
91103
:param host:
92104
"""
93105
return zipkin_collector.BinaryAnnotation(
94-
key, '0x01', zipkin_collector.AnnotationType.BOOL, host)
106+
key, str_to_binary('0x01'), zipkin_collector.AnnotationType.BOOL, host)
95107

96108

97109
def make_local_component_tag(component_name, endpoint):
@@ -101,7 +113,7 @@ def make_local_component_tag(component_name, endpoint):
101113
:param endpoint:
102114
"""
103115
return zipkin_collector.BinaryAnnotation(
104-
key=LOCAL_COMPONENT, value=component_name,
116+
key=LOCAL_COMPONENT, value=str_to_binary(component_name),
105117
annotation_type=zipkin_collector.AnnotationType.STRING,
106118
host=endpoint)
107119

‎jaeger_client/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from builtins import str
1+
from builtins import bytes
22
from builtins import range
33
from builtins import object
44
# Copyright (c) 2016 Uber Technologies, Inc.
@@ -77,7 +77,7 @@ def local_ip():
7777
if ip.startswith('127.'):
7878
# Check eth0, eth1, eth2, en0, ...
7979
interfaces = [
80-
i + str(n) for i in ('eth', 'en', 'wlan') for n in range(3)
80+
i + bytes(n) for i in (b'eth', b'en', b'wlan') for n in range(3)
8181
] # :(
8282
for interface in interfaces:
8383
try:
@@ -91,6 +91,7 @@ def local_ip():
9191
def interface_ip(interface):
9292
"""Determine the IP assigned to us by the given network interface."""
9393
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
94+
9495
return socket.inet_ntoa(
9596
fcntl.ioctl(
9697
sock.fileno(), 0x8915, struct.pack('256s', interface[:15])

‎setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'Programming Language :: Python :: 2.7',
3636
],
3737
install_requires=[
38+
'future',
3839
'futures',
3940
'threadloop>=1,<2',
4041
# we want thrift>=0.9.2.post1,<0.9.3, but we let the users pin to that

‎tests/test_TUDPTransport.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_constructor_nonblocking(self):
3939
assert t.transport_sock.gettimeout() == 0
4040

4141
def test_write(self):
42-
self.t.write('hello')
42+
self.t.write(b'hello')
4343

4444
def test_isopen_when_open(self):
4545
assert self.t.isOpen() == True
@@ -52,4 +52,4 @@ def test_close(self):
5252
self.t.close()
5353
with self.assertRaises(Exception):
5454
# Something bad should happen if we send on a closed socket..
55-
self.t.write('hello')
55+
self.t.write(b'hello')

‎tests/test_codecs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,4 @@ def test_debug_id():
324324
assert span.is_sampled()
325325
tags = [t for t in span.tags if t.key == debug_header]
326326
assert len(tags) == 1
327-
assert tags[0].value == 'Coraline'
327+
assert tags[0].value == b'Coraline'

‎tests/test_local_agent_net.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from jaeger_client.local_agent_net import LocalAgentSender
77
from jaeger_client.config import DEFAULT_REPORTING_PORT
88

9-
test_strategy = """
9+
test_strategy = b"""
1010
{
1111
"strategyType":0,
1212
"probabilisticSampling":

‎tests/test_sampler.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_guaranteed_throughput_probabilistic_sampler():
164164
assert tags == get_tags('lowerbound', 0.5)
165165
sampled, _ = sampler.is_sampled(MAX_INT+10)
166166
assert not sampled
167-
assert '%s' % sampler == 'GuaranteedThroughputProbabilisticSampler(op, 0.5, 2)'
167+
assert '%s' % sampler == 'GuaranteedThroughputProbabilisticSampler(op, 0.5, 2.0)'
168168

169169
sampler.update(3, 0.51)
170170
sampled, tags = sampler.is_sampled(MAX_INT-10)
@@ -174,7 +174,7 @@ def test_guaranteed_throughput_probabilistic_sampler():
174174
assert sampled
175175
assert tags == get_tags('lowerbound', 0.51)
176176

177-
assert '%s' % sampler == 'GuaranteedThroughputProbabilisticSampler(op, 0.51, 3)'
177+
assert '%s' % sampler == 'GuaranteedThroughputProbabilisticSampler(op, 0.51, 3.0)'
178178
sampler.close()
179179

180180

@@ -212,7 +212,7 @@ def test_adaptive_sampler():
212212
assert tags == get_tags('probabilistic', 0.51)
213213
sampled, _ = sampler.is_sampled(MAX_INT+(old_div(MAX_INT,4)), "new_op_2")
214214
assert not sampled
215-
assert '%s' % sampler == 'AdaptiveSampler(0.51, 3, 2)'
215+
assert '%s' % sampler == 'AdaptiveSampler(0.51, 3.0, 2)'
216216

217217
# Update the strategies
218218
strategies = {
@@ -245,7 +245,7 @@ def test_adaptive_sampler():
245245
sampled, tags = sampler.is_sampled(MAX_INT-10, 'new_op_3')
246246
assert sampled
247247
assert tags == get_tags('probabilistic', 0.53)
248-
assert '%s' % sampler == 'AdaptiveSampler(0.52, 4, 2)'
248+
assert '%s' % sampler == 'AdaptiveSampler(0.52, 4.0, 2)'
249249

250250
sampler.close()
251251

@@ -271,12 +271,12 @@ def test_adaptive_sampler_default_values():
271271
}
272272
]
273273
})
274-
assert '%s' % adaptive_sampler == 'AdaptiveSampler(0.001, 4, 2)'
274+
assert '%s' % adaptive_sampler == 'AdaptiveSampler(0.001, 4.0, 2)'
275275

276276
sampled, tags = adaptive_sampler.is_sampled(0, 'new_op')
277277
assert sampled
278278
assert tags == get_tags('probabilistic', 0.002)
279-
assert '%s' % adaptive_sampler.samplers['new_op'] == 'GuaranteedThroughputProbabilisticSampler(new_op, 0.002, 4)'
279+
assert '%s' % adaptive_sampler.samplers['new_op'] == 'GuaranteedThroughputProbabilisticSampler(new_op, 0.002, 4.0)'
280280

281281
sampled, tags = adaptive_sampler.is_sampled(0, 'op')
282282
assert sampled
@@ -398,7 +398,7 @@ def test_sampling_request_callback():
398398
return_value.result = lambda *args: \
399399
type('obj', (object,), {'body': adaptive_sampling_strategy})()
400400
sampler._sampling_request_callback(return_value)
401-
assert '%s' % sampler.sampler == 'AdaptiveSampler(0.001, 2, 10)', 'sampler should have changed to adaptive'
401+
assert '%s' % sampler.sampler == 'AdaptiveSampler(0.001, 2.0, 10)', 'sampler should have changed to adaptive'
402402
prev_sampler = sampler.sampler
403403

404404
sampler._sampling_request_callback(return_value)
@@ -555,7 +555,7 @@ def test_update_sampler_adaptive_sampler():
555555
}
556556

557557
remote_sampler._update_sampler(response)
558-
assert '%s' % remote_sampler.sampler == 'AdaptiveSampler(0.001, 2, 10)'
558+
assert '%s' % remote_sampler.sampler == 'AdaptiveSampler(0.001, 2.0, 10)'
559559

560560
new_response = {
561561
"strategyType":1,
@@ -576,7 +576,7 @@ def test_update_sampler_adaptive_sampler():
576576
}
577577

578578
remote_sampler._update_sampler(new_response)
579-
assert '%s' % remote_sampler.sampler == 'AdaptiveSampler(0.51, 3, 10)'
579+
assert '%s' % remote_sampler.sampler == 'AdaptiveSampler(0.51, 3.0, 10)'
580580

581581
remote_sampler._update_sampler({"strategyType":0,"probabilisticSampling":{"samplingRate":0.004}})
582582
assert '%s' % remote_sampler.sampler == 'ProbabilisticSampler(0.004)', \

‎tests/test_thrift.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222

23-
from io import StringIO
23+
from io import BytesIO
2424

2525
import jaeger_client.thrift_gen.zipkincore.ZipkinCollector as zipkin_collector
2626
import jaeger_client.thrift_gen.sampling.SamplingManager as sampling_manager
@@ -79,7 +79,7 @@ def now_reading(self):
7979
it's one or the other (really? yes.). This will convert
8080
us from write-able to read-able.
8181
"""
82-
self._buffer = StringIO(self.getvalue())
82+
self._buffer = BytesIO(self.getvalue())
8383

8484
spans = thrift.make_zipkin_spans([span])
8585

‎tests/test_tracer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from jaeger_client import ConstSampler, Tracer
3030
from jaeger_client import constants as c
3131
from jaeger_client.thrift_gen.zipkincore import constants as g
32-
from jaeger_client.thrift import add_zipkin_annotations
32+
from jaeger_client.thrift import add_zipkin_annotations, str_to_binary
3333

3434

3535
def log_exists(span, value):
@@ -236,7 +236,7 @@ def test_tracer_tags_on_root_span(span_type, expected_tags):
236236
assert found_tag is not None, 'test (%s): expecting tag %s' % (
237237
span_type, key
238238
)
239-
assert found_tag.value == value, \
239+
assert found_tag.value == str_to_binary(value), \
240240
'test (%s): expecting tag %s=%s' % (span_type, key, value)
241241

242242

0 commit comments

Comments
 (0)
This repository has been archived.