|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +from typing import Optional, Sequence |
| 15 | +from grpc import ChannelCredentials, Compression |
| 16 | +from opentelemetry.exporter.otlp.proto.grpc.exporter import ( |
| 17 | + OTLPExporterMixin, |
| 18 | + _translate_key_values, |
| 19 | + get_resource_data, |
| 20 | + _translate_value, |
| 21 | +) |
| 22 | +from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import ( |
| 23 | + ExportLogsServiceRequest, |
| 24 | +) |
| 25 | +from opentelemetry.proto.collector.logs.v1.logs_service_pb2_grpc import ( |
| 26 | + LogsServiceStub, |
| 27 | +) |
| 28 | +from opentelemetry.proto.common.v1.common_pb2 import InstrumentationLibrary |
| 29 | +from opentelemetry.proto.logs.v1.logs_pb2 import ( |
| 30 | + InstrumentationLibraryLogs, |
| 31 | + ResourceLogs, |
| 32 | +) |
| 33 | +from opentelemetry.proto.logs.v1.logs_pb2 import LogRecord as PB2LogRecord |
| 34 | +from opentelemetry.sdk._logs import LogRecord as SDKLogRecord |
| 35 | +from opentelemetry.sdk._logs import LogData |
| 36 | +from opentelemetry.sdk._logs.export import LogExporter, LogExportResult |
| 37 | + |
| 38 | + |
| 39 | +class OTLPLogExporter( |
| 40 | + LogExporter, |
| 41 | + OTLPExporterMixin[SDKLogRecord, ExportLogsServiceRequest, LogExportResult], |
| 42 | +): |
| 43 | + |
| 44 | + _result = LogExportResult |
| 45 | + _stub = LogsServiceStub |
| 46 | + |
| 47 | + def __init__( |
| 48 | + self, |
| 49 | + endpoint: Optional[str] = None, |
| 50 | + insecure: Optional[bool] = None, |
| 51 | + credentials: Optional[ChannelCredentials] = None, |
| 52 | + headers: Optional[Sequence] = None, |
| 53 | + timeout: Optional[int] = None, |
| 54 | + compression: Optional[Compression] = None, |
| 55 | + ): |
| 56 | + super().__init__( |
| 57 | + **{ |
| 58 | + "endpoint": endpoint, |
| 59 | + "insecure": insecure, |
| 60 | + "credentials": credentials, |
| 61 | + "headers": headers, |
| 62 | + "timeout": timeout, |
| 63 | + "compression": compression, |
| 64 | + } |
| 65 | + ) |
| 66 | + |
| 67 | + def _translate_name(self, log_data: LogData) -> None: |
| 68 | + self._collector_log_kwargs["name"] = log_data.log_record.name |
| 69 | + |
| 70 | + def _translate_time(self, log_data: LogData) -> None: |
| 71 | + self._collector_log_kwargs[ |
| 72 | + "time_unix_nano" |
| 73 | + ] = log_data.log_record.timestamp |
| 74 | + |
| 75 | + def _translate_span_id(self, log_data: LogData) -> None: |
| 76 | + self._collector_log_kwargs[ |
| 77 | + "span_id" |
| 78 | + ] = log_data.log_record.span_id.to_bytes(8, "big") |
| 79 | + |
| 80 | + def _translate_trace_id(self, log_data: LogData) -> None: |
| 81 | + self._collector_log_kwargs[ |
| 82 | + "trace_id" |
| 83 | + ] = log_data.log_record.trace_id.to_bytes(16, "big") |
| 84 | + |
| 85 | + def _translate_trace_flags(self, log_data: LogData) -> None: |
| 86 | + self._collector_log_kwargs["flags"] = int( |
| 87 | + log_data.log_record.trace_flags |
| 88 | + ) |
| 89 | + |
| 90 | + def _translate_body(self, log_data: LogData): |
| 91 | + self._collector_log_kwargs["body"] = _translate_value( |
| 92 | + log_data.log_record.body |
| 93 | + ) |
| 94 | + |
| 95 | + def _translate_severity_text(self, log_data: LogData): |
| 96 | + self._collector_log_kwargs[ |
| 97 | + "severity_text" |
| 98 | + ] = log_data.log_record.severity_text |
| 99 | + |
| 100 | + def _translate_attributes(self, log_data: LogData) -> None: |
| 101 | + if log_data.log_record.attributes: |
| 102 | + self._collector_log_kwargs["attributes"] = [] |
| 103 | + for key, value in log_data.log_record.attributes.items(): |
| 104 | + try: |
| 105 | + self._collector_log_kwargs["attributes"].append( |
| 106 | + _translate_key_values(key, value) |
| 107 | + ) |
| 108 | + except Exception: # pylint: disable=broad-except |
| 109 | + pass |
| 110 | + |
| 111 | + def _translate_data( |
| 112 | + self, data: Sequence[LogData] |
| 113 | + ) -> ExportLogsServiceRequest: |
| 114 | + # pylint: disable=attribute-defined-outside-init |
| 115 | + |
| 116 | + sdk_resource_instrumentation_library_logs = {} |
| 117 | + |
| 118 | + for log_data in data: |
| 119 | + resource = log_data.log_record.resource |
| 120 | + |
| 121 | + instrumentation_library_logs_map = ( |
| 122 | + sdk_resource_instrumentation_library_logs.get(resource, {}) |
| 123 | + ) |
| 124 | + if not instrumentation_library_logs_map: |
| 125 | + sdk_resource_instrumentation_library_logs[ |
| 126 | + resource |
| 127 | + ] = instrumentation_library_logs_map |
| 128 | + |
| 129 | + instrumentation_library_logs = ( |
| 130 | + instrumentation_library_logs_map.get( |
| 131 | + log_data.instrumentation_info |
| 132 | + ) |
| 133 | + ) |
| 134 | + if not instrumentation_library_logs: |
| 135 | + if log_data.instrumentation_info is not None: |
| 136 | + instrumentation_library_logs_map[ |
| 137 | + log_data.instrumentation_info |
| 138 | + ] = InstrumentationLibraryLogs( |
| 139 | + instrumentation_library=InstrumentationLibrary( |
| 140 | + name=log_data.instrumentation_info.name, |
| 141 | + version=log_data.instrumentation_info.version, |
| 142 | + ) |
| 143 | + ) |
| 144 | + else: |
| 145 | + instrumentation_library_logs_map[ |
| 146 | + log_data.instrumentation_info |
| 147 | + ] = InstrumentationLibraryLogs() |
| 148 | + |
| 149 | + instrumentation_library_logs = ( |
| 150 | + instrumentation_library_logs_map.get( |
| 151 | + log_data.instrumentation_info |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | + self._collector_log_kwargs = {} |
| 156 | + |
| 157 | + self._translate_name(log_data) |
| 158 | + self._translate_time(log_data) |
| 159 | + self._translate_span_id(log_data) |
| 160 | + self._translate_trace_id(log_data) |
| 161 | + self._translate_trace_flags(log_data) |
| 162 | + self._translate_body(log_data) |
| 163 | + self._translate_severity_text(log_data) |
| 164 | + self._translate_attributes(log_data) |
| 165 | + |
| 166 | + self._collector_log_kwargs[ |
| 167 | + "severity_number" |
| 168 | + ] = log_data.log_record.severity_number.value |
| 169 | + |
| 170 | + instrumentation_library_logs.logs.append( |
| 171 | + PB2LogRecord(**self._collector_log_kwargs) |
| 172 | + ) |
| 173 | + |
| 174 | + return ExportLogsServiceRequest( |
| 175 | + resource_logs=get_resource_data( |
| 176 | + sdk_resource_instrumentation_library_logs, |
| 177 | + ResourceLogs, |
| 178 | + "logs", |
| 179 | + ) |
| 180 | + ) |
| 181 | + |
| 182 | + def export(self, batch: Sequence[LogData]) -> LogExportResult: |
| 183 | + return self._export(batch) |
| 184 | + |
| 185 | + def shutdown(self) -> None: |
| 186 | + pass |
0 commit comments