|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import socket |
| 4 | +import errno |
| 5 | +import re |
| 6 | +from threading import Lock |
| 7 | + |
| 8 | + |
| 9 | +MIN_SEND_BUFFER_SIZE = 32 * 1024 |
| 10 | +log = logging.getLogger("datadog_lambda.dogstatsd") |
| 11 | + |
| 12 | + |
| 13 | +class DogStatsd(object): |
| 14 | + def __init__(self): |
| 15 | + self._socket_lock = Lock() |
| 16 | + self.socket_path = None |
| 17 | + self.host = "localhost" |
| 18 | + self.port = 8125 |
| 19 | + self.socket = None |
| 20 | + self.encoding = "utf-8" |
| 21 | + |
| 22 | + def get_socket(self, telemetry=False): |
| 23 | + """ |
| 24 | + Return a connected socket. |
| 25 | +
|
| 26 | + Note: connect the socket before assigning it to the class instance to |
| 27 | + avoid bad thread race conditions. |
| 28 | + """ |
| 29 | + with self._socket_lock: |
| 30 | + self.socket = self._get_udp_socket( |
| 31 | + self.host, |
| 32 | + self.port, |
| 33 | + ) |
| 34 | + return self.socket |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def _ensure_min_send_buffer_size(cls, sock, min_size=MIN_SEND_BUFFER_SIZE): |
| 38 | + # Increase the receiving buffer size where needed (e.g. MacOS has 4k RX |
| 39 | + # buffers which is half of the max packet size that the client will send. |
| 40 | + if os.name == "posix": |
| 41 | + try: |
| 42 | + recv_buff_size = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF) |
| 43 | + if recv_buff_size <= min_size: |
| 44 | + sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, min_size) |
| 45 | + log.debug("Socket send buffer increased to %dkb", min_size / 1024) |
| 46 | + finally: |
| 47 | + pass |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def _get_udp_socket(cls, host, port): |
| 51 | + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 52 | + sock.setblocking(0) |
| 53 | + cls._ensure_min_send_buffer_size(sock) |
| 54 | + sock.connect((host, port)) |
| 55 | + |
| 56 | + return sock |
| 57 | + |
| 58 | + def distribution(self, metric, value, tags=None): |
| 59 | + """ |
| 60 | + Send a global distribution value, optionally setting tags. |
| 61 | +
|
| 62 | + >>> statsd.distribution("uploaded.file.size", 1445) |
| 63 | + >>> statsd.distribution("album.photo.count", 26, tags=["gender:female"]) |
| 64 | + """ |
| 65 | + self._report(metric, "d", value, tags) |
| 66 | + |
| 67 | + def close_socket(self): |
| 68 | + """ |
| 69 | + Closes connected socket if connected. |
| 70 | + """ |
| 71 | + with self._socket_lock: |
| 72 | + if self.socket: |
| 73 | + try: |
| 74 | + self.socket.close() |
| 75 | + except OSError as e: |
| 76 | + log.error("Unexpected error: %s", str(e)) |
| 77 | + self.socket = None |
| 78 | + |
| 79 | + def normalize_tags(self, tag_list): |
| 80 | + TAG_INVALID_CHARS_RE = re.compile(r"[^\w\d_\-:/\.]", re.UNICODE) |
| 81 | + TAG_INVALID_CHARS_SUBS = "_" |
| 82 | + return [ |
| 83 | + re.sub(TAG_INVALID_CHARS_RE, TAG_INVALID_CHARS_SUBS, tag) |
| 84 | + for tag in tag_list |
| 85 | + ] |
| 86 | + |
| 87 | + def _serialize_metric(self, metric, metric_type, value, tags): |
| 88 | + # Create/format the metric packet |
| 89 | + return "%s:%s|%s%s" % ( |
| 90 | + metric, |
| 91 | + value, |
| 92 | + metric_type, |
| 93 | + ("|#" + ",".join(self.normalize_tags(tags))) if tags else "", |
| 94 | + ) |
| 95 | + |
| 96 | + def _report(self, metric, metric_type, value, tags): |
| 97 | + if value is None: |
| 98 | + return |
| 99 | + |
| 100 | + payload = self._serialize_metric(metric, metric_type, value, tags) |
| 101 | + |
| 102 | + # Send it |
| 103 | + self._send_to_server(payload) |
| 104 | + |
| 105 | + def _send_to_server(self, packet): |
| 106 | + try: |
| 107 | + mysocket = self.socket or self.get_socket() |
| 108 | + mysocket.send(packet.encode(self.encoding)) |
| 109 | + return True |
| 110 | + except socket.timeout: |
| 111 | + # dogstatsd is overflowing, drop the packets (mimicks the UDP behaviour) |
| 112 | + pass |
| 113 | + except (socket.herror, socket.gaierror) as socket_err: |
| 114 | + log.warning( |
| 115 | + "Error submitting packet: %s, dropping the packet and closing the socket", |
| 116 | + socket_err, |
| 117 | + ) |
| 118 | + self.close_socket() |
| 119 | + except socket.error as socket_err: |
| 120 | + if socket_err.errno == errno.EAGAIN: |
| 121 | + log.debug( |
| 122 | + "Socket send would block: %s, dropping the packet", socket_err |
| 123 | + ) |
| 124 | + elif socket_err.errno == errno.ENOBUFS: |
| 125 | + log.debug("Socket buffer full: %s, dropping the packet", socket_err) |
| 126 | + elif socket_err.errno == errno.EMSGSIZE: |
| 127 | + log.debug( |
| 128 | + "Packet size too big (size: %d): %s, dropping the packet", |
| 129 | + len(packet.encode(self.encoding)), |
| 130 | + socket_err, |
| 131 | + ) |
| 132 | + else: |
| 133 | + log.warning( |
| 134 | + "Error submitting packet: %s, dropping the packet and closing the socket", |
| 135 | + socket_err, |
| 136 | + ) |
| 137 | + self.close_socket() |
| 138 | + except Exception as e: |
| 139 | + log.error("Unexpected error: %s", str(e)) |
| 140 | + return False |
| 141 | + |
| 142 | + |
| 143 | +statsd = DogStatsd() |
0 commit comments