diff --git a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs index 9845c0e0b77..b992048d338 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs @@ -47,11 +47,9 @@ public JaegerExporterOptions() { if (EnvironmentVariableHelper.LoadString(OTelProtocolEnvVarKey, out string protocolEnvVar)) { - var protocol = protocolEnvVar.ToJaegerExportProtocol(); - - if (protocol.HasValue) + if (JaegerExporterProtocolParser.TryParse(protocolEnvVar, out var protocol)) { - this.Protocol = protocol.Value; + this.Protocol = protocol; } else { diff --git a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterProtocolParser.cs similarity index 51% rename from src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptionsExtensions.cs rename to src/OpenTelemetry.Exporter.Jaeger/JaegerExporterProtocolParser.cs index 12f225e421d..52fb109350f 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.Jaeger/JaegerExporterProtocolParser.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,13 +16,21 @@ namespace OpenTelemetry.Exporter; -internal static class JaegerExporterOptionsExtensions +internal static class JaegerExporterProtocolParser { - public static JaegerExportProtocol? ToJaegerExportProtocol(this string protocol) => - protocol?.Trim() switch + public static bool TryParse(string value, out JaegerExportProtocol result) + { + switch (value?.Trim()) { - "udp/thrift.compact" => JaegerExportProtocol.UdpCompactThrift, - "http/thrift.binary" => JaegerExportProtocol.HttpBinaryThrift, - _ => null, - }; + case "udp/thrift.compact": + result = JaegerExportProtocol.UdpCompactThrift; + return true; + case "http/thrift.binary": + result = JaegerExportProtocol.HttpBinaryThrift; + return true; + default: + result = default; + return false; + } + } } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExportProtocolParser.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExportProtocolParser.cs new file mode 100644 index 00000000000..23062ce458b --- /dev/null +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExportProtocolParser.cs @@ -0,0 +1,37 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +namespace OpenTelemetry.Exporter +{ + internal static class OtlpExportProtocolParser + { + public static bool TryParse(string value, out OtlpExportProtocol result) + { + switch (value?.Trim()) + { + case "grpc": + result = OtlpExportProtocol.Grpc; + return true; + case "http/protobuf": + result = OtlpExportProtocol.HttpProtobuf; + return true; + default: + result = default; + return false; + } + } + } +} diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs index 020f57ee577..936da811fa4 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs @@ -69,10 +69,9 @@ public OtlpExporterOptions() if (EnvironmentVariableHelper.LoadString(ProtocolEnvVarName, out string protocolEnvVar)) { - var protocol = protocolEnvVar.ToOtlpExportProtocol(); - if (protocol.HasValue) + if (OtlpExportProtocolParser.TryParse(protocolEnvVar, out var protocol)) { - this.Protocol = protocol.Value; + this.Protocol = protocol; } else { diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs index 9d99caf1714..43a6c3a997b 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptionsExtensions.cs @@ -121,14 +121,6 @@ public static THeaders GetHeaders(this OtlpExporterOptions options, Ac _ => throw new NotSupportedException($"Protocol {options.Protocol} is not supported."), }; - public static OtlpExportProtocol? ToOtlpExportProtocol(this string protocol) => - protocol.Trim() switch - { - "grpc" => OtlpExportProtocol.Grpc, - "http/protobuf" => OtlpExportProtocol.HttpProtobuf, - _ => null, - }; - public static void TryEnableIHttpClientFactoryIntegration(this OtlpExporterOptions options, IServiceProvider serviceProvider, string httpClientName) { if (serviceProvider != null diff --git a/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterOptionsExtensionsTests.cs b/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterProtocolParserTests.cs similarity index 54% rename from test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterOptionsExtensionsTests.cs rename to test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterProtocolParserTests.cs index 0dfca9cc3cd..c75a52d674a 100644 --- a/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterOptionsExtensionsTests.cs +++ b/test/OpenTelemetry.Exporter.Jaeger.Tests/JaegerExporterProtocolParserTests.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,16 +18,17 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests; -public class JaegerExporterOptionsExtensionsTests +public class JaegerExporterProtocolParserTests { [Theory] - [InlineData("udp/thrift.compact", JaegerExportProtocol.UdpCompactThrift)] - [InlineData("http/thrift.binary", JaegerExportProtocol.HttpBinaryThrift)] - [InlineData("unsupported", null)] - public void ToJaegerExportProtocol_Protocol_MapsToCorrectValue(string protocol, JaegerExportProtocol? expectedExportProtocol) + [InlineData("udp/thrift.compact", true, JaegerExportProtocol.UdpCompactThrift)] + [InlineData("http/thrift.binary", true, JaegerExportProtocol.HttpBinaryThrift)] + [InlineData("unsupported", false, default(JaegerExportProtocol))] + public void TryParse_Protocol_MapsToCorrectValue(string protocol, bool expectedResult, JaegerExportProtocol expectedExportProtocol) { - var exportProtocol = protocol.ToJaegerExportProtocol(); + var result = JaegerExporterProtocolParser.TryParse(protocol, out var exportProtocol); Assert.Equal(expectedExportProtocol, exportProtocol); + Assert.Equal(expectedResult, result); } } diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExportProtocolParserTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExportProtocolParserTests.cs new file mode 100644 index 00000000000..baec4253e73 --- /dev/null +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExportProtocolParserTests.cs @@ -0,0 +1,35 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using Xunit; + +namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests +{ + public class OtlpExportProtocolParserTests : Http2UnencryptedSupportTests + { + [Theory] + [InlineData("grpc", true, OtlpExportProtocol.Grpc)] + [InlineData("http/protobuf", true, OtlpExportProtocol.HttpProtobuf)] + [InlineData("unsupported", false, default(OtlpExportProtocol))] + public void TryParse_Protocol_MapsToCorrectValue(string protocol, bool expectedResult, OtlpExportProtocol expectedExportProtocol) + { + var result = OtlpExportProtocolParser.TryParse(protocol, out var exportProtocol); + + Assert.Equal(expectedExportProtocol, exportProtocol); + Assert.Equal(expectedResult, result); + } + } +} diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsExtensionsTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsExtensionsTests.cs index 27864f9c74f..4b4dd25fd53 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsExtensionsTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsExtensionsTests.cs @@ -145,17 +145,6 @@ public void GetTraceExportClient_UnsupportedProtocol_Throws() Assert.Throws(() => options.GetTraceExportClient()); } - [Theory] - [InlineData("grpc", OtlpExportProtocol.Grpc)] - [InlineData("http/protobuf", OtlpExportProtocol.HttpProtobuf)] - [InlineData("unsupported", null)] - public void ToOtlpExportProtocol_Protocol_MapsToCorrectValue(string protocol, OtlpExportProtocol? expectedExportProtocol) - { - var exportProtocol = protocol.ToOtlpExportProtocol(); - - Assert.Equal(expectedExportProtocol, exportProtocol); - } - [Theory] [InlineData("http://test:8888", "http://test:8888/v1/traces")] [InlineData("http://test:8888/", "http://test:8888/v1/traces")]