Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle socket exception in create_endpoint #39

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion py_zipkin/thrift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ def create_endpoint(port=0, service_name='unknown', host=None):
:returns: zipkin Endpoint object
"""
if host is None:
host = socket.gethostbyname(socket.gethostname())
try:
host = socket.gethostbyname(socket.gethostname())
except socket.gaierror:
host = '127.0.0.1'
# Convert ip address to network byte order
ipv4 = struct.unpack('!i', socket.inet_aton(host))[0]
# Zipkin passes unsigned values in signed types because Thrift has no
Expand Down
15 changes: 15 additions & 0 deletions tests/thrift/thrift_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mock
import pytest
import socket

from py_zipkin import thrift

Expand Down Expand Up @@ -117,6 +118,20 @@ def test_copy_endpoint_with_new_service_name(gethostbyname):
assert endpoint.ipv4 == 0


@mock.patch('socket.gethostbyname', autospec=True)
def test_create_endpoint_defaults_localhost(gethostbyname):
gethostbyname.side_effect = socket.gaierror

endpoint = thrift.create_endpoint(
port=8080,
service_name='foo',
)
assert endpoint.service_name == 'foo'
assert endpoint.port == 8080
# An IP address of 127.0.0.1 unpacks to 2130706433
assert endpoint.ipv4 == 2130706433


def test_create_annotation():
ann = thrift.create_annotation('foo', 'bar', 'baz')
assert ('foo', 'bar', 'baz') == (ann.timestamp, ann.value, ann.host)
Expand Down