File: TUDPTransport.py

package info (click to toggle)
python-jaeger-client 4.8.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 920 kB
  • sloc: python: 5,656; makefile: 93; sh: 26; awk: 16
file content (69 lines) | stat: -rw-r--r-- 2,130 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from thrift.transport.TTransport import TTransportBase
import socket


logger = logging.getLogger('jaeger_tracing')


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 = 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(
            buf,
            (self.transport_host, self.transport_port)
        )

    def isOpen(self):
        """
        isOpen for UDP is always true (there is no connection) as long
        as we have a sock
        """
        return self.transport_sock is not None

    def close(self):
        self.transport_sock.close()
        self.transport_sock = None