|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import gzip |
| 16 | +import logging |
| 17 | +import zlib |
| 18 | +from io import BytesIO |
| 19 | +from os import environ |
| 20 | +from typing import Dict, Optional, Sequence |
| 21 | +from time import sleep |
| 22 | + |
| 23 | +import requests |
| 24 | +from backoff import expo |
| 25 | + |
| 26 | +from opentelemetry.sdk.environment_variables import ( |
| 27 | + OTEL_EXPORTER_OTLP_CERTIFICATE, |
| 28 | + OTEL_EXPORTER_OTLP_COMPRESSION, |
| 29 | + OTEL_EXPORTER_OTLP_ENDPOINT, |
| 30 | + OTEL_EXPORTER_OTLP_HEADERS, |
| 31 | + OTEL_EXPORTER_OTLP_TIMEOUT, |
| 32 | +) |
| 33 | +from opentelemetry.sdk._logs.export import ( |
| 34 | + LogExporter, |
| 35 | + LogExportResult, |
| 36 | + LogData, |
| 37 | +) |
| 38 | +from opentelemetry.exporter.otlp.proto.http import Compression |
| 39 | +from opentelemetry.exporter.otlp.proto.http._log_exporter.encoder import ( |
| 40 | + _ProtobufEncoder, |
| 41 | +) |
| 42 | +from opentelemetry.util.re import parse_headers |
| 43 | + |
| 44 | + |
| 45 | +_logger = logging.getLogger(__name__) |
| 46 | + |
| 47 | + |
| 48 | +DEFAULT_COMPRESSION = Compression.NoCompression |
| 49 | +DEFAULT_ENDPOINT = "http://localhost:4318/" |
| 50 | +DEFAULT_LOGS_EXPORT_PATH = "v1/logs" |
| 51 | +DEFAULT_TIMEOUT = 10 # in seconds |
| 52 | + |
| 53 | + |
| 54 | +class OTLPLogExporter(LogExporter): |
| 55 | + |
| 56 | + _MAX_RETRY_TIMEOUT = 64 |
| 57 | + |
| 58 | + def __init__( |
| 59 | + self, |
| 60 | + endpoint: Optional[str] = None, |
| 61 | + certificate_file: Optional[str] = None, |
| 62 | + headers: Optional[Dict[str, str]] = None, |
| 63 | + timeout: Optional[int] = None, |
| 64 | + compression: Optional[Compression] = None, |
| 65 | + ): |
| 66 | + self._endpoint = endpoint or _append_logs_path( |
| 67 | + environ.get(OTEL_EXPORTER_OTLP_ENDPOINT, DEFAULT_ENDPOINT) |
| 68 | + ) |
| 69 | + self._certificate_file = certificate_file or environ.get( |
| 70 | + OTEL_EXPORTER_OTLP_CERTIFICATE, True |
| 71 | + ) |
| 72 | + headers_string = environ.get(OTEL_EXPORTER_OTLP_HEADERS, "") |
| 73 | + self._headers = headers or parse_headers(headers_string) |
| 74 | + self._timeout = timeout or int( |
| 75 | + environ.get(OTEL_EXPORTER_OTLP_TIMEOUT, DEFAULT_TIMEOUT) |
| 76 | + ) |
| 77 | + self._compression = compression or _compression_from_env() |
| 78 | + self._session = requests.Session() |
| 79 | + self._session.headers.update(self._headers) |
| 80 | + self._session.headers.update( |
| 81 | + {"Content-Type": _ProtobufEncoder._CONTENT_TYPE} |
| 82 | + ) |
| 83 | + if self._compression is not Compression.NoCompression: |
| 84 | + self._session.headers.update( |
| 85 | + {"Content-Encoding": self._compression.value} |
| 86 | + ) |
| 87 | + self._shutdown = False |
| 88 | + |
| 89 | + def _export(self, serialized_data: str): |
| 90 | + data = serialized_data |
| 91 | + if self._compression == Compression.Gzip: |
| 92 | + gzip_data = BytesIO() |
| 93 | + with gzip.GzipFile(fileobj=gzip_data, mode="w") as gzip_stream: |
| 94 | + gzip_stream.write(serialized_data) |
| 95 | + data = gzip_data.getvalue() |
| 96 | + elif self._compression == Compression.Deflate: |
| 97 | + data = zlib.compress(bytes(serialized_data)) |
| 98 | + |
| 99 | + return self._session.post( |
| 100 | + url=self._endpoint, |
| 101 | + data=data, |
| 102 | + verify=self._certificate_file, |
| 103 | + timeout=self._timeout, |
| 104 | + ) |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + def _retryable(resp: requests.Response) -> bool: |
| 108 | + if resp.status_code == 408: |
| 109 | + return True |
| 110 | + if resp.status_code >= 500 and resp.status_code <= 599: |
| 111 | + return True |
| 112 | + return False |
| 113 | + |
| 114 | + def export(self, batch: Sequence[LogData]) -> LogExportResult: |
| 115 | + # After the call to Shutdown subsequent calls to Export are |
| 116 | + # not allowed and should return a Failure result. |
| 117 | + if self._shutdown: |
| 118 | + _logger.warning("Exporter already shutdown, ignoring batch") |
| 119 | + return LogExportResult.FAILURE |
| 120 | + |
| 121 | + serialized_data = _ProtobufEncoder.serialize(batch) |
| 122 | + |
| 123 | + for delay in expo(max_value=self._MAX_RETRY_TIMEOUT): |
| 124 | + |
| 125 | + if delay == self._MAX_RETRY_TIMEOUT: |
| 126 | + return LogExportResult.FAILURE |
| 127 | + |
| 128 | + resp = self._export(serialized_data) |
| 129 | + # pylint: disable=no-else-return |
| 130 | + if resp.status_code in (200, 202): |
| 131 | + return LogExportResult.SUCCESS |
| 132 | + elif self._retryable(resp): |
| 133 | + _logger.warning( |
| 134 | + "Transient error %s encountered while exporting logs batch, retrying in %ss.", |
| 135 | + resp.reason, |
| 136 | + delay, |
| 137 | + ) |
| 138 | + sleep(delay) |
| 139 | + continue |
| 140 | + else: |
| 141 | + _logger.error( |
| 142 | + "Failed to export logs batch code: %s, reason: %s", |
| 143 | + resp.status_code, |
| 144 | + resp.text, |
| 145 | + ) |
| 146 | + return LogExportResult.FAILURE |
| 147 | + return LogExportResult.FAILURE |
| 148 | + |
| 149 | + def shutdown(self): |
| 150 | + if self._shutdown: |
| 151 | + _logger.warning("Exporter already shutdown, ignoring call") |
| 152 | + return |
| 153 | + self._session.close() |
| 154 | + self._shutdown = True |
| 155 | + |
| 156 | + |
| 157 | +def _compression_from_env() -> Compression: |
| 158 | + compression = ( |
| 159 | + environ.get(OTEL_EXPORTER_OTLP_COMPRESSION, "none").lower().strip() |
| 160 | + ) |
| 161 | + return Compression(compression) |
| 162 | + |
| 163 | + |
| 164 | +def _append_logs_path(endpoint: str) -> str: |
| 165 | + if endpoint.endswith("/"): |
| 166 | + return endpoint + DEFAULT_LOGS_EXPORT_PATH |
| 167 | + return endpoint + f"/{DEFAULT_LOGS_EXPORT_PATH}" |
0 commit comments