|
| 1 | +import socket |
| 2 | +from sentry_sdk import Hub |
| 3 | +from sentry_sdk._types import MYPY |
| 4 | +from sentry_sdk.consts import OP |
| 5 | +from sentry_sdk.integrations import Integration |
| 6 | + |
| 7 | +if MYPY: |
| 8 | + from socket import AddressFamily, SocketKind |
| 9 | + from typing import Tuple, Optional, Union, List |
| 10 | + |
| 11 | +__all__ = ["SocketIntegration"] |
| 12 | + |
| 13 | + |
| 14 | +class SocketIntegration(Integration): |
| 15 | + identifier = "socket" |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def setup_once(): |
| 19 | + # type: () -> None |
| 20 | + """ |
| 21 | + patches two of the most used functions of socket: create_connection and getaddrinfo(dns resolver) |
| 22 | + """ |
| 23 | + _patch_create_connection() |
| 24 | + _patch_getaddrinfo() |
| 25 | + |
| 26 | + |
| 27 | +def _get_span_description(host, port): |
| 28 | + # type: (Union[bytes, str, None], Union[str, int, None]) -> str |
| 29 | + |
| 30 | + try: |
| 31 | + host = host.decode() # type: ignore |
| 32 | + except (UnicodeDecodeError, AttributeError): |
| 33 | + pass |
| 34 | + |
| 35 | + description = "%s:%s" % (host, port) # type: ignore |
| 36 | + |
| 37 | + return description |
| 38 | + |
| 39 | + |
| 40 | +def _patch_create_connection(): |
| 41 | + # type: () -> None |
| 42 | + real_create_connection = socket.create_connection |
| 43 | + |
| 44 | + def create_connection( |
| 45 | + address, |
| 46 | + timeout=socket._GLOBAL_DEFAULT_TIMEOUT, # type: ignore |
| 47 | + source_address=None, |
| 48 | + ): |
| 49 | + # type: (Tuple[Optional[str], int], Optional[float], Optional[Tuple[Union[bytearray, bytes, str], int]])-> socket.socket |
| 50 | + hub = Hub.current |
| 51 | + if hub.get_integration(SocketIntegration) is None: |
| 52 | + return real_create_connection( |
| 53 | + address=address, timeout=timeout, source_address=source_address |
| 54 | + ) |
| 55 | + |
| 56 | + with hub.start_span( |
| 57 | + op=OP.SOCKET_CONNECTION, |
| 58 | + description=_get_span_description(address[0], address[1]), |
| 59 | + ) as span: |
| 60 | + span.set_data("address", address) |
| 61 | + span.set_data("timeout", timeout) |
| 62 | + span.set_data("source_address", source_address) |
| 63 | + |
| 64 | + return real_create_connection( |
| 65 | + address=address, timeout=timeout, source_address=source_address |
| 66 | + ) |
| 67 | + |
| 68 | + socket.create_connection = create_connection |
| 69 | + |
| 70 | + |
| 71 | +def _patch_getaddrinfo(): |
| 72 | + # type: () -> None |
| 73 | + real_getaddrinfo = socket.getaddrinfo |
| 74 | + |
| 75 | + def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0): |
| 76 | + # type: (Union[bytes, str, None], Union[str, int, None], int, int, int, int) -> List[Tuple[AddressFamily, SocketKind, int, str, Union[Tuple[str, int], Tuple[str, int, int, int]]]] |
| 77 | + hub = Hub.current |
| 78 | + if hub.get_integration(SocketIntegration) is None: |
| 79 | + return real_getaddrinfo(host, port, family, type, proto, flags) |
| 80 | + |
| 81 | + with hub.start_span( |
| 82 | + op=OP.SOCKET_DNS, description=_get_span_description(host, port) |
| 83 | + ) as span: |
| 84 | + span.set_data("host", host) |
| 85 | + span.set_data("port", port) |
| 86 | + |
| 87 | + return real_getaddrinfo(host, port, family, type, proto, flags) |
| 88 | + |
| 89 | + socket.getaddrinfo = getaddrinfo |
0 commit comments