Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.

Commit 90ddf76

Browse files
SEJeffyurishkuro
authored andcommitted
Allow specifying hostname and ip via tags (#167)
* Allow specifying the hostname via a tag This is useful for applications running inside containers where the hostname is autogenerated and generally useless. In kubernetes, the downward api can be used to pass in the node name actually running the container. Fixes #66 Signed-off-by: Jeff Schroeder <[email protected]> * Catch the `socket.error` exception when trying to get the hostname This is the base exception for all errors in the socket library. Explicit is better than implicit! Signed-off-by: Jeff Schroeder <[email protected]> * Don't attempt to lookup the ip address if specified via a tag This matches the functionality of jaeger-client-java after jaegertracing/jaeger-client-java#371 was merged. Refs: #166 Signed-off-by: Jeff Schroeder <[email protected]> * Remove ipv4_to_int since jaeger converts tags to strings Signed-off-by: Jeff Schroeder <[email protected]>
1 parent 34364d0 commit 90ddf76

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

jaeger_client/thrift.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
# limitations under the License.
1414

1515
import six
16-
import socket
17-
import struct
1816

1917
import jaeger_client.thrift_gen.jaeger.ttypes as ttypes
2018
import jaeger_client.thrift_gen.sampling.SamplingManager as sampling_manager
@@ -28,17 +26,6 @@
2826
long = int
2927

3028

31-
def ipv4_to_int(ipv4):
32-
if ipv4 == 'localhost':
33-
ipv4 = '127.0.0.1'
34-
elif ipv4 == '::1':
35-
ipv4 = '127.0.0.1'
36-
try:
37-
return struct.unpack('!i', socket.inet_aton(ipv4))[0]
38-
except:
39-
return 0
40-
41-
4229
def id_to_int(big_id):
4330
if big_id is None:
4431
return None

jaeger_client/tracer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from .codecs import TextCodec, ZipkinCodec, ZipkinSpanFormat, BinaryCodec
3030
from .span import Span, SAMPLED_FLAG, DEBUG_FLAG
3131
from .span_context import SpanContext
32-
from .thrift import ipv4_to_int
3332
from .metrics import Metrics, LegacyMetricsFactory
3433
from .utils import local_ip
3534

@@ -53,7 +52,6 @@ def __init__(
5352
self.service_name = service_name
5453
self.reporter = reporter
5554
self.sampler = sampler
56-
self.ip_address = ipv4_to_int(local_ip())
5755
self.metrics_factory = metrics_factory or LegacyMetricsFactory(metrics or Metrics())
5856
self.metrics = TracerMetrics(self.metrics_factory)
5957
self.random = random.Random(time.time() * (os.getpid() or 1))
@@ -80,16 +78,19 @@ def __init__(
8078
self.codecs.update(extra_codecs)
8179
self.tags = {
8280
constants.JAEGER_VERSION_TAG_KEY: constants.JAEGER_CLIENT_VERSION,
83-
constants.JAEGER_IP_TAG_KEY: self.ip_address,
8481
}
8582
if tags:
8683
self.tags.update(tags)
87-
# noinspection PyBroadException
88-
try:
89-
hostname = socket.gethostname()
90-
self.tags[constants.JAEGER_HOSTNAME_TAG_KEY] = hostname
91-
except:
92-
logger.exception('Unable to determine host name')
84+
85+
if self.tags.get(constants.JAEGER_IP_TAG_KEY) is None:
86+
self.tags[constants.JAEGER_IP_TAG_KEY] = local_ip()
87+
88+
if self.tags.get(constants.JAEGER_HOSTNAME_TAG_KEY) is None:
89+
try:
90+
hostname = socket.gethostname()
91+
self.tags[constants.JAEGER_HOSTNAME_TAG_KEY] = hostname
92+
except socket.error:
93+
logger.exception('Unable to determine host name')
9394

9495
self.reporter.set_process(
9596
service_name=self.service_name,

tests/test_thrift.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
from thrift.transport.TTransport import TMemoryBuffer
2525

2626

27-
def test_ipv4_to_int():
28-
base = thrift.ipv4_to_int('127.0.0.1')
29-
assert thrift.ipv4_to_int('localhost') == base
30-
assert thrift.ipv4_to_int('::1') == base
31-
assert thrift.ipv4_to_int('a:b:1') == 0
32-
33-
3427
def test_submit_batch(tracer):
3528
span = tracer.start_span("test-span")
3629
span.set_tag('bender', 'is great')

tests/test_tracer.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import mock
1616
import random
1717
import six
18+
import socket
1819

1920
import pytest
2021
import tornado.httputil
@@ -200,7 +201,7 @@ def test_tracer_tags_no_hostname():
200201
from jaeger_client.tracer import logger
201202
with mock.patch.object(logger, 'exception') as mock_log:
202203
with mock.patch('socket.gethostname',
203-
side_effect=['host', ValueError()]):
204+
side_effect=['host', socket.timeout()]):
204205
Tracer(service_name='x', reporter=reporter, sampler=sampler)
205206
assert mock_log.call_count == 1
206207

@@ -260,3 +261,31 @@ def test_tracer_override_codecs():
260261
"Extra codec not found"
261262
assert tracer.codecs[Format.BINARY] == "overridden_binary_codec",\
262263
"Binary format codec not overridden"
264+
265+
266+
def test_tracer_hostname_tag():
267+
reporter = mock.MagicMock()
268+
sampler = ConstSampler(True)
269+
tracer = Tracer(
270+
service_name='x',
271+
tags={c.JAEGER_HOSTNAME_TAG_KEY: 'jaeger-client-app.local'},
272+
reporter=reporter,
273+
sampler=sampler,
274+
)
275+
276+
assert tracer.tags[c.JAEGER_HOSTNAME_TAG_KEY] == \
277+
'jaeger-client-app.local'
278+
279+
280+
def test_tracer_ip_tag():
281+
reporter = mock.MagicMock()
282+
sampler = ConstSampler(True)
283+
tracer = Tracer(
284+
service_name='x',
285+
tags={c.JAEGER_IP_TAG_KEY: '192.0.2.3'},
286+
reporter=reporter,
287+
sampler=sampler,
288+
)
289+
290+
assert tracer.tags[c.JAEGER_IP_TAG_KEY] == \
291+
'192.0.2.3'

0 commit comments

Comments
 (0)