Skip to content

Commit

Permalink
Catch the socket.error exception when trying to get the hostname
Browse files Browse the repository at this point in the history
This is the base exception for all errors in the socket library. Explicit
is better than implicit!

Signed-off-by: Jeff Schroeder <jeffschroeder@computer.org>
  • Loading branch information
SEJeff committed Apr 29, 2018
1 parent b5f6356 commit 93c26c8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
3 changes: 1 addition & 2 deletions jaeger_client/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ def __init__(
}
if tags:
self.tags.update(tags)
# noinspection PyBroadException

if self.tags.get(constants.JAEGER_HOSTNAME_TAG_KEY) is None:
try:
hostname = socket.gethostname()
self.tags[constants.JAEGER_HOSTNAME_TAG_KEY] = hostname
except:
except socket.error:
logger.exception('Unable to determine host name')

self.reporter.set_process(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import mock
import random
import six
import socket

import pytest
import tornado.httputil
Expand Down Expand Up @@ -200,7 +201,7 @@ def test_tracer_tags_no_hostname():
from jaeger_client.tracer import logger
with mock.patch.object(logger, 'exception') as mock_log:
with mock.patch('socket.gethostname',
side_effect=['host', ValueError()]):
side_effect=['host', socket.timeout()]):
Tracer(service_name='x', reporter=reporter, sampler=sampler)
assert mock_log.call_count == 1

Expand Down

0 comments on commit 93c26c8

Please sign in to comment.