|
| 1 | +# Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +# under the Apache License Version 2.0. |
| 3 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +# Copyright 2021 Datadog, Inc. |
| 5 | + |
| 6 | + |
| 7 | +import os |
| 8 | +import logging |
| 9 | + |
| 10 | +from concurrent.futures import as_completed |
| 11 | +from requests_futures.sessions import FuturesSession |
| 12 | +from logs.helpers import compress_logs |
| 13 | +from logs.exceptions import ScrubbingException |
| 14 | + |
| 15 | +from settings import ( |
| 16 | + DD_USE_COMPRESSION, |
| 17 | + DD_COMPRESSION_LEVEL, |
| 18 | + DD_MAX_WORKERS, |
| 19 | + DD_FORWARDER_VERSION, |
| 20 | +) |
| 21 | + |
| 22 | +logger = logging.getLogger() |
| 23 | +logger.setLevel(logging.getLevelName(os.environ.get("DD_LOG_LEVEL", "INFO").upper())) |
| 24 | + |
| 25 | + |
| 26 | +class DatadogHTTPClient(object): |
| 27 | + """ |
| 28 | + Client that sends a batch of logs over HTTP. |
| 29 | + """ |
| 30 | + |
| 31 | + _POST = "POST" |
| 32 | + if DD_USE_COMPRESSION: |
| 33 | + _HEADERS = {"Content-type": "application/json", "Content-Encoding": "gzip"} |
| 34 | + else: |
| 35 | + _HEADERS = {"Content-type": "application/json"} |
| 36 | + |
| 37 | + _HEADERS["DD-EVP-ORIGIN"] = "aws_forwarder" |
| 38 | + _HEADERS["DD-EVP-ORIGIN-VERSION"] = DD_FORWARDER_VERSION |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, host, port, no_ssl, skip_ssl_validation, api_key, scrubber, timeout=10 |
| 42 | + ): |
| 43 | + self._HEADERS.update({"DD-API-KEY": api_key}) |
| 44 | + protocol = "http" if no_ssl else "https" |
| 45 | + self._url = "{}://{}:{}/api/v2/logs".format(protocol, host, port) |
| 46 | + self._scrubber = scrubber |
| 47 | + self._timeout = timeout |
| 48 | + self._session = None |
| 49 | + self._ssl_validation = not skip_ssl_validation |
| 50 | + self._futures = [] |
| 51 | + if logger.isEnabledFor(logging.DEBUG): |
| 52 | + logger.debug( |
| 53 | + f"Initialized http client for logs intake: " |
| 54 | + f"<host: {host}, port: {port}, url: {self._url}, no_ssl: {no_ssl}, " |
| 55 | + f"skip_ssl_validation: {skip_ssl_validation}, timeout: {timeout}>" |
| 56 | + ) |
| 57 | + |
| 58 | + def _connect(self): |
| 59 | + self._session = FuturesSession(max_workers=DD_MAX_WORKERS) |
| 60 | + self._session.headers.update(self._HEADERS) |
| 61 | + |
| 62 | + def _close(self): |
| 63 | + # Resolve all the futures and log exceptions if any |
| 64 | + for future in as_completed(self._futures): |
| 65 | + try: |
| 66 | + future.result() |
| 67 | + except Exception: |
| 68 | + logger.exception("Exception while forwarding logs") |
| 69 | + |
| 70 | + self._session.close() |
| 71 | + |
| 72 | + def send(self, logs): |
| 73 | + """ |
| 74 | + Sends a batch of log, only retry on server and network errors. |
| 75 | + """ |
| 76 | + try: |
| 77 | + data = self._scrubber.scrub("[{}]".format(",".join(logs))) |
| 78 | + except ScrubbingException: |
| 79 | + raise Exception("could not scrub the payload") |
| 80 | + if DD_USE_COMPRESSION: |
| 81 | + data = compress_logs(data, DD_COMPRESSION_LEVEL) |
| 82 | + |
| 83 | + # FuturesSession returns immediately with a future object |
| 84 | + future = self._session.post( |
| 85 | + self._url, data, timeout=self._timeout, verify=self._ssl_validation |
| 86 | + ) |
| 87 | + self._futures.append(future) |
| 88 | + |
| 89 | + def __enter__(self): |
| 90 | + self._connect() |
| 91 | + return self |
| 92 | + |
| 93 | + def __exit__(self, ex_type, ex_value, traceback): |
| 94 | + self._close() |
0 commit comments