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

Commit

Permalink
Add ipv6 support (#339)
Browse files Browse the repository at this point in the history
* Add ipv6 support
Signed-off-by: Alexander Vasin <[email protected]>

* Increase test coverage

Signed-off-by: Yuri Shkuro <[email protected]>

Co-authored-by: Yuri Shkuro <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
3 people authored Sep 14, 2021
1 parent 1341016 commit aed18e8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
25 changes: 20 additions & 5 deletions jaeger_client/TUDPTransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from thrift.transport.TTransport import TTransportBase
import socket


logger = logging.getLogger('jaeger_tracing')


Expand All @@ -25,16 +26,30 @@ class TUDPTransport(TTransportBase, object):
TUDPTransport implements just enough of the tornado transport interface
to work for blindly sending UDP packets.
"""

DEFAULT_SOCKET_FAMILY = socket.AF_INET

def __init__(self, host, port, blocking=False):
self.transport_host = host
self.transport_port = port
self.transport_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if blocking:
blocking = 1
else:
blocking = 0

self.transport_sock = self._create_socket()
self.transport_sock.setblocking(blocking)

def _create_socket(self) -> socket.socket:
family, type, proto = (self.DEFAULT_SOCKET_FAMILY, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

try:
addrinfo = socket.getaddrinfo(
self.transport_host, self.transport_port, type=socket.SOCK_DGRAM
)
if addrinfo:
family, type, proto, *_ = addrinfo[0]
except socket.gaierror:
pass

return socket.socket(family, type, proto)

def write(self, buf):
"""Raw write to the UDP socket."""
return self.transport_sock.sendto(
Expand Down
38 changes: 38 additions & 0 deletions tests/test_TUDPTransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import socket
import unittest
from unittest import mock

import pytest

from jaeger_client.TUDPTransport import TUDPTransport

Expand Down Expand Up @@ -44,3 +48,37 @@ def test_close(self):
with self.assertRaises(Exception):
# Something bad should happen if we send on a closed socket..
self.t.write(b'hello')


def test_created_socket_default_family():
transport = TUDPTransport('unknown-host', 12345)
sock = transport._create_socket()
assert sock.family == TUDPTransport.DEFAULT_SOCKET_FAMILY


@pytest.mark.parametrize('addrinfo,expected_family', (
(
(
socket.AddressFamily.AF_INET6, socket.SocketKind.SOCK_DGRAM,
17, '', ('aced:a11:7e57', 12345, 0, 0)
),
socket.AF_INET6
),
(
(
socket.AddressFamily.AF_INET, socket.SocketKind.SOCK_DGRAM,
17, '', ('127.0.0.1', 12345)
),
socket.AF_INET
),
(
None,
TUDPTransport.DEFAULT_SOCKET_FAMILY
)
))
def test_created_socket_specified_family(addrinfo, expected_family):
return_value = [addrinfo] if addrinfo else []
with mock.patch('socket.getaddrinfo', return_value=return_value):
transport = TUDPTransport('ipv6-host', 12345)
sock = transport._create_socket()
assert sock.family == expected_family

0 comments on commit aed18e8

Please sign in to comment.