From cda3c88a3f4bf396524ad366d824545316e00899 Mon Sep 17 00:00:00 2001 From: nimanikoo Date: Sat, 8 Mar 2025 22:41:36 +0330 Subject: [PATCH 1/7] Refactor code to use Span for header parsing, eliminating Split method and reducing allocations. OtlpExporterOptionsExtensions.cs --- .../OtlpExporterOptionsExtensions.cs | 66 +++++++++++-------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index 2feb4d1955..960b31c6d4 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -49,42 +49,52 @@ public static Channel CreateChannel(this OtlpExporterOptions options) public static Metadata GetMetadataFromHeaders(this OtlpExporterOptions options) => options.GetHeaders((m, k, v) => m.Add(k, v)); #endif - public static THeaders GetHeaders(this OtlpExporterOptions options, Action addHeader) - where THeaders : new() + public static THeaders GetHeaders(this OtlpExporterOptions options, Action addHeader) + where THeaders : new() +{ + var optionHeaders = options.Headers; + var headers = new THeaders(); + if (!string.IsNullOrEmpty(optionHeaders)) { - var optionHeaders = options.Headers; - var headers = new THeaders(); - if (!string.IsNullOrEmpty(optionHeaders)) - { - // According to the specification, URL-encoded headers must be supported. - optionHeaders = Uri.UnescapeDataString(optionHeaders); + optionHeaders = Uri.UnescapeDataString(optionHeaders); + ReadOnlySpan headersSpan = optionHeaders.AsSpan(); - Array.ForEach( - optionHeaders.Split(','), - (pair) => - { - // Specify the maximum number of substrings to return to 2 - // This treats everything that follows the first `=` in the string as the value to be added for the metadata key - var keyValueData = pair.Split(['='], 2); - if (keyValueData.Length != 2) - { - throw new ArgumentException("Headers provided in an invalid format."); - } + while (!headersSpan.IsEmpty) + { + int commaIndex = headersSpan.IndexOf(','); + ReadOnlySpan pair; + if (commaIndex == -1) + { + pair = headersSpan; + headersSpan = ReadOnlySpan.Empty; + } + else + { + pair = headersSpan.Slice(0, commaIndex); + headersSpan = headersSpan.Slice(commaIndex + 1); + } - var key = keyValueData[0].Trim(); - var value = keyValueData[1].Trim(); - addHeader(headers, key, value); - }); - } + int equalIndex = pair.IndexOf('='); + if (equalIndex == -1) + { + throw new ArgumentException("Headers provided in an invalid format."); + } - foreach (var header in OtlpExporterOptions.StandardHeaders) - { - addHeader(headers, header.Key, header.Value); + var key = pair.Slice(0, equalIndex).ToString().Trim(); + var value = pair.Slice(equalIndex + 1).ToString().Trim(); + addHeader(headers, key, value); } + } - return headers; + foreach (var header in OtlpExporterOptions.StandardHeaders) + { + addHeader(headers, header.Key, header.Value); } + return headers; +} + + public static OtlpExporterTransmissionHandler GetExportTransmissionHandler(this OtlpExporterOptions options, ExperimentalOptions experimentalOptions, OtlpSignalType otlpSignalType) { var exportClient = GetExportClient(options, otlpSignalType); From 65e074411f2c70f5296176d66a361a1a91299b57 Mon Sep 17 00:00:00 2001 From: nimanikoo Date: Sun, 9 Mar 2025 12:32:29 +0330 Subject: [PATCH 2/7] Fix formatting issues in OtlpExporterOptionsExtensions.cs - Ensure consistent indentation to comply with StyleCop rule SA1137. - Remove multiple consecutive blank lines to comply with StyleCop rule SA1507. --- .../OtlpExporterOptionsExtensions.cs | 71 ++++++++++--------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index 960b31c6d4..ccac849cb8 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -49,51 +49,52 @@ public static Channel CreateChannel(this OtlpExporterOptions options) public static Metadata GetMetadataFromHeaders(this OtlpExporterOptions options) => options.GetHeaders((m, k, v) => m.Add(k, v)); #endif - public static THeaders GetHeaders(this OtlpExporterOptions options, Action addHeader) - where THeaders : new() -{ - var optionHeaders = options.Headers; - var headers = new THeaders(); - if (!string.IsNullOrEmpty(optionHeaders)) + public static THeaders GetHeaders(this OtlpExporterOptions options, Action addHeader) + where THeaders : new() { - optionHeaders = Uri.UnescapeDataString(optionHeaders); - ReadOnlySpan headersSpan = optionHeaders.AsSpan(); - - while (!headersSpan.IsEmpty) + var optionHeaders = options.Headers; + var headers = new THeaders(); + if (!string.IsNullOrEmpty(optionHeaders)) { - int commaIndex = headersSpan.IndexOf(','); - ReadOnlySpan pair; - if (commaIndex == -1) - { - pair = headersSpan; - headersSpan = ReadOnlySpan.Empty; - } - else - { - pair = headersSpan.Slice(0, commaIndex); - headersSpan = headersSpan.Slice(commaIndex + 1); - } + // According to the specification, URL-encoded headers must be supported. + optionHeaders = Uri.UnescapeDataString(optionHeaders); + ReadOnlySpan headersSpan = optionHeaders.AsSpan(); - int equalIndex = pair.IndexOf('='); - if (equalIndex == -1) + while (!headersSpan.IsEmpty) { - throw new ArgumentException("Headers provided in an invalid format."); + int commaIndex = headersSpan.IndexOf(','); + ReadOnlySpan pair; + if (commaIndex == -1) + { + pair = headersSpan; + headersSpan = ReadOnlySpan.Empty; + } + else + { + pair = headersSpan.Slice(0, commaIndex); + headersSpan = headersSpan.Slice(commaIndex + 1); + } + + int equalIndex = pair.IndexOf('='); + if (equalIndex == -1) + { + throw new ArgumentException("Headers provided in an invalid format."); + } + + var key = pair.Slice(0, equalIndex).ToString().Trim(); + var value = pair.Slice(equalIndex + 1).ToString().Trim(); + addHeader(headers, key, value); } + } - var key = pair.Slice(0, equalIndex).ToString().Trim(); - var value = pair.Slice(equalIndex + 1).ToString().Trim(); - addHeader(headers, key, value); + foreach (var header in OtlpExporterOptions.StandardHeaders) + { + addHeader(headers, header.Key, header.Value); } - } - foreach (var header in OtlpExporterOptions.StandardHeaders) - { - addHeader(headers, header.Key, header.Value); + return headers; } - return headers; -} - public static OtlpExporterTransmissionHandler GetExportTransmissionHandler(this OtlpExporterOptions options, ExperimentalOptions experimentalOptions, OtlpSignalType otlpSignalType) { From e490141a32c0fa1735ffb3b9043b81e8d4f63d41 Mon Sep 17 00:00:00 2001 From: nimanikoo Date: Sun, 9 Mar 2025 13:24:28 +0330 Subject: [PATCH 3/7] Fixup indents --- .../OtlpExporterOptionsExtensions.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index ccac849cb8..db77cd0646 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -74,28 +74,23 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac pair = headersSpan.Slice(0, commaIndex); headersSpan = headersSpan.Slice(commaIndex + 1); } - int equalIndex = pair.IndexOf('='); if (equalIndex == -1) { throw new ArgumentException("Headers provided in an invalid format."); } - var key = pair.Slice(0, equalIndex).ToString().Trim(); var value = pair.Slice(equalIndex + 1).ToString().Trim(); addHeader(headers, key, value); } } - foreach (var header in OtlpExporterOptions.StandardHeaders) { addHeader(headers, header.Key, header.Value); } - return headers; } - public static OtlpExporterTransmissionHandler GetExportTransmissionHandler(this OtlpExporterOptions options, ExperimentalOptions experimentalOptions, OtlpSignalType otlpSignalType) { var exportClient = GetExportClient(options, otlpSignalType); From 511c8cbd5100cfe2833fcb4e813ecf15347f242b Mon Sep 17 00:00:00 2001 From: nimanikoo Date: Sun, 9 Mar 2025 20:51:40 +0330 Subject: [PATCH 4/7] Fix SA1513 --- .../OtlpExporterOptionsExtensions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index db77cd0646..5915c67a6b 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -74,11 +74,13 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac pair = headersSpan.Slice(0, commaIndex); headersSpan = headersSpan.Slice(commaIndex + 1); } + int equalIndex = pair.IndexOf('='); if (equalIndex == -1) { throw new ArgumentException("Headers provided in an invalid format."); } + var key = pair.Slice(0, equalIndex).ToString().Trim(); var value = pair.Slice(equalIndex + 1).ToString().Trim(); addHeader(headers, key, value); @@ -88,6 +90,7 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac { addHeader(headers, header.Key, header.Value); } + return headers; } From 496228bf77513d6faa568e1008ce2ac302dbd666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Tue, 11 Mar 2025 15:22:16 +0100 Subject: [PATCH 5/7] Fix build issues --- .../OtlpExporterOptionsExtensions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index 5915c67a6b..ea94a644ab 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -67,7 +67,7 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac if (commaIndex == -1) { pair = headersSpan; - headersSpan = ReadOnlySpan.Empty; + headersSpan = []; } else { @@ -86,6 +86,7 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac addHeader(headers, key, value); } } + foreach (var header in OtlpExporterOptions.StandardHeaders) { addHeader(headers, header.Key, header.Value); From d73cc1559d61718b16b3b43ae8c3a423032440a3 Mon Sep 17 00:00:00 2001 From: nima nikoonazar Date: Tue, 11 Mar 2025 21:29:32 +0330 Subject: [PATCH 6/7] Update src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Piotr Kiełkowicz --- .../OtlpExporterOptionsExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index ea94a644ab..e2de83e4b3 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -81,8 +81,8 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac throw new ArgumentException("Headers provided in an invalid format."); } - var key = pair.Slice(0, equalIndex).ToString().Trim(); - var value = pair.Slice(equalIndex + 1).ToString().Trim(); + var key = pair.Slice(0, equalIndex).Trim().ToString(); + var value = pair.Slice(equalIndex + 1).Trim().ToString(); addHeader(headers, key, value); } } From bb61cd6c999d88106b315db520567f21630328ea Mon Sep 17 00:00:00 2001 From: nima nikoonazar Date: Wed, 12 Mar 2025 00:41:37 +0330 Subject: [PATCH 7/7] Update OtlpExporterOptionsExtensions.cs Co-authored-by: xiang17 --- .../OtlpExporterOptionsExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index e2de83e4b3..8a2956939d 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -67,7 +67,7 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac if (commaIndex == -1) { pair = headersSpan; - headersSpan = []; + headersSpan = ReadOnlySpan.Empty; } else {