|
| 1 | +import os |
| 2 | +import logging |
| 3 | +import json |
| 4 | +import binascii |
| 5 | +import time |
| 6 | +import socket |
| 7 | + |
| 8 | +from datadog_lambda.constants import XrayDaemon, XraySubsegment, TraceContextSource |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +def get_xray_host_port(adress): |
| 14 | + if adress == "": |
| 15 | + logger.debug("X-Ray daemon env var not set, not sending sub-segment") |
| 16 | + return None |
| 17 | + parts = adress.split(":") |
| 18 | + if len(parts) <= 1: |
| 19 | + logger.debug("X-Ray daemon env var not set, not sending sub-segment") |
| 20 | + return None |
| 21 | + port = int(parts[1]) |
| 22 | + host = parts[0] |
| 23 | + return (host, port) |
| 24 | + |
| 25 | + |
| 26 | +def send(host_port_tuple, payload): |
| 27 | + sock = None |
| 28 | + try: |
| 29 | + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 30 | + sock.setblocking(0) |
| 31 | + sock.connect(host_port_tuple) |
| 32 | + sock.send(payload.encode("utf-8")) |
| 33 | + except Exception as e_send: |
| 34 | + logger.error("Error occurred submitting to xray daemon: %s", str(e_send)) |
| 35 | + try: |
| 36 | + sock.close() |
| 37 | + except Exception as e_close: |
| 38 | + logger.error("Error while closing the socket: %s", str(e_close)) |
| 39 | + |
| 40 | + |
| 41 | +def build_segment_payload(payload): |
| 42 | + if payload is None: |
| 43 | + return None |
| 44 | + return '{"format": "json", "version": 1}' + "\n" + payload |
| 45 | + |
| 46 | + |
| 47 | +def parse_xray_header(raw_trace_id): |
| 48 | + # Example: Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=1 |
| 49 | + logger.debug("Reading trace context from env var %s", raw_trace_id) |
| 50 | + if len(raw_trace_id) == 0: |
| 51 | + return None |
| 52 | + parts = raw_trace_id.split(";") |
| 53 | + if len(parts) != 3: |
| 54 | + return None |
| 55 | + root = parts[0].replace("Root=", "") |
| 56 | + parent = parts[1].replace("Parent=", "") |
| 57 | + sampled = parts[2].replace("Sampled=", "") |
| 58 | + if ( |
| 59 | + len(root) == len(parts[0]) |
| 60 | + or len(parent) == len(parts[1]) |
| 61 | + or len(sampled) == len(parts[2]) |
| 62 | + ): |
| 63 | + return None |
| 64 | + return { |
| 65 | + "parent_id": parent, |
| 66 | + "trace_id": root, |
| 67 | + "sampled": sampled, |
| 68 | + "source": TraceContextSource.XRAY, |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +def generate_random_id(): |
| 73 | + return binascii.b2a_hex(os.urandom(8)).decode("utf-8") |
| 74 | + |
| 75 | + |
| 76 | +def build_segment(context, key, metadata): |
| 77 | + |
| 78 | + segment = json.dumps( |
| 79 | + { |
| 80 | + "id": generate_random_id(), |
| 81 | + "trace_id": context["trace_id"], |
| 82 | + "parent_id": context["parent_id"], |
| 83 | + "name": XraySubsegment.NAME, |
| 84 | + "start_time": time.time(), |
| 85 | + "end_time": time.time(), |
| 86 | + "type": "subsegment", |
| 87 | + "metadata": { |
| 88 | + XraySubsegment.NAMESPACE: { |
| 89 | + key: metadata, |
| 90 | + } |
| 91 | + }, |
| 92 | + } |
| 93 | + ) |
| 94 | + return segment |
| 95 | + |
| 96 | + |
| 97 | +def send_segment(key, metadata): |
| 98 | + host_port_tuple = get_xray_host_port( |
| 99 | + os.environ.get(XrayDaemon.XRAY_DAEMON_ADDRESS, "") |
| 100 | + ) |
| 101 | + if host_port_tuple is None: |
| 102 | + return None |
| 103 | + context = parse_xray_header( |
| 104 | + os.environ.get(XrayDaemon.XRAY_TRACE_ID_HEADER_NAME, "") |
| 105 | + ) |
| 106 | + if context is None: |
| 107 | + logger.debug( |
| 108 | + "Failed to create segment since it was not possible to get trace context from header" |
| 109 | + ) |
| 110 | + return None |
| 111 | + segment = build_segment(context, key, metadata) |
| 112 | + segment_payload = build_segment_payload(segment) |
| 113 | + send(host_port_tuple, segment_payload) |
0 commit comments