|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Buffers.Binary; |
| 5 | +using System.Diagnostics; |
| 6 | +using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation; |
| 7 | +using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Serializer; |
| 8 | +using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission; |
| 9 | +using OpenTelemetry.Logs; |
| 10 | +using OpenTelemetry.Resources; |
| 11 | + |
| 12 | +namespace OpenTelemetry.Exporter; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Exporter consuming <see cref="LogRecord"/> and exporting the data using |
| 16 | +/// the OpenTelemetry protocol (OTLP). |
| 17 | +/// </summary> |
| 18 | +internal sealed class ProtobufOtlpLogExporter : BaseExporter<LogRecord> |
| 19 | +{ |
| 20 | + private readonly SdkLimitOptions sdkLimitOptions; |
| 21 | + private readonly ExperimentalOptions experimentalOptions; |
| 22 | + private readonly ProtobufOtlpExporterTransmissionHandler transmissionHandler; |
| 23 | + private readonly int startWritePosition; |
| 24 | + |
| 25 | + private Resource? resource; |
| 26 | + |
| 27 | + // Initial buffer size set to ~732KB. |
| 28 | + // This choice allows us to gradually grow the buffer while targeting a final capacity of around 100 MB, |
| 29 | + // by the 7th doubling to maintain efficient allocation without frequent resizing. |
| 30 | + private byte[] buffer = new byte[750000]; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the <see cref="ProtobufOtlpLogExporter"/> class. |
| 34 | + /// </summary> |
| 35 | + /// <param name="options">Configuration options for the exporter.</param> |
| 36 | + public ProtobufOtlpLogExporter(OtlpExporterOptions options) |
| 37 | + : this(options, sdkLimitOptions: new(), experimentalOptions: new(), transmissionHandler: null) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Initializes a new instance of the <see cref="ProtobufOtlpLogExporter"/> class. |
| 43 | + /// </summary> |
| 44 | + /// <param name="exporterOptions"><see cref="OtlpExporterOptions"/>.</param> |
| 45 | + /// <param name="sdkLimitOptions"><see cref="SdkLimitOptions"/>.</param> |
| 46 | + /// <param name="experimentalOptions"><see cref="ExperimentalOptions"/>.</param> |
| 47 | + /// <param name="transmissionHandler"><see cref="OtlpExporterTransmissionHandler{T}"/>.</param> |
| 48 | + internal ProtobufOtlpLogExporter( |
| 49 | + OtlpExporterOptions exporterOptions, |
| 50 | + SdkLimitOptions sdkLimitOptions, |
| 51 | + ExperimentalOptions experimentalOptions, |
| 52 | + ProtobufOtlpExporterTransmissionHandler? transmissionHandler = null) |
| 53 | + { |
| 54 | + Debug.Assert(exporterOptions != null, "exporterOptions was null"); |
| 55 | + Debug.Assert(sdkLimitOptions != null, "sdkLimitOptions was null"); |
| 56 | + Debug.Assert(experimentalOptions != null, "experimentalOptions was null"); |
| 57 | + |
| 58 | + this.experimentalOptions = experimentalOptions!; |
| 59 | + this.sdkLimitOptions = sdkLimitOptions!; |
| 60 | + this.startWritePosition = exporterOptions!.Protocol == OtlpExportProtocol.Grpc ? 5 : 0; |
| 61 | + this.transmissionHandler = transmissionHandler ?? exporterOptions!.GetProtobufExportTransmissionHandler(experimentalOptions!); |
| 62 | + } |
| 63 | + |
| 64 | + internal Resource Resource => this.resource ??= this.ParentProvider.GetResource(); |
| 65 | + |
| 66 | + /// <inheritdoc/> |
| 67 | + public override ExportResult Export(in Batch<LogRecord> logRecordBatch) |
| 68 | + { |
| 69 | + // Prevents the exporter's gRPC and HTTP operations from being instrumented. |
| 70 | + using var scope = SuppressInstrumentationScope.Begin(); |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + int writePosition = ProtobufOtlpLogSerializer.WriteLogsData(this.buffer, this.startWritePosition, this.sdkLimitOptions, this.experimentalOptions, this.Resource, logRecordBatch); |
| 75 | + |
| 76 | + if (this.startWritePosition == 5) |
| 77 | + { |
| 78 | + // Grpc payload consists of 3 parts |
| 79 | + // byte 0 - Specifying if the payload is compressed. |
| 80 | + // 1-4 byte - Specifies the length of payload in big endian format. |
| 81 | + // 5 and above - Protobuf serialized data. |
| 82 | + Span<byte> data = new Span<byte>(this.buffer, 1, 4); |
| 83 | + var dataLength = writePosition - 5; |
| 84 | + BinaryPrimitives.WriteUInt32BigEndian(data, (uint)dataLength); |
| 85 | + } |
| 86 | + |
| 87 | + if (!this.transmissionHandler.TrySubmitRequest(this.buffer, writePosition)) |
| 88 | + { |
| 89 | + return ExportResult.Failure; |
| 90 | + } |
| 91 | + } |
| 92 | + catch (IndexOutOfRangeException) |
| 93 | + { |
| 94 | + if (!this.IncreaseBufferSize()) |
| 95 | + { |
| 96 | + throw; |
| 97 | + } |
| 98 | + } |
| 99 | + catch (Exception ex) |
| 100 | + { |
| 101 | + OpenTelemetryProtocolExporterEventSource.Log.ExportMethodException(ex); |
| 102 | + return ExportResult.Failure; |
| 103 | + } |
| 104 | + |
| 105 | + return ExportResult.Success; |
| 106 | + } |
| 107 | + |
| 108 | + /// <inheritdoc /> |
| 109 | + protected override bool OnShutdown(int timeoutMilliseconds) => this.transmissionHandler?.Shutdown(timeoutMilliseconds) ?? true; |
| 110 | + |
| 111 | + // TODO: Consider moving this to a shared utility class. |
| 112 | + private bool IncreaseBufferSize() |
| 113 | + { |
| 114 | + var newBufferSize = this.buffer.Length * 2; |
| 115 | + |
| 116 | + if (newBufferSize > 100 * 1024 * 1024) |
| 117 | + { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + var newBuffer = new byte[newBufferSize]; |
| 122 | + this.buffer.CopyTo(newBuffer, 0); |
| 123 | + this.buffer = newBuffer; |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | +} |
0 commit comments