Refactor string extensions (#3306)

This commit is contained in:
Piotr Kiełkowicz 2022-05-25 20:13:40 +02:00 committed by GitHub
parent 292b41667c
commit 8aa1778b43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 100 additions and 41 deletions

View File

@ -47,11 +47,9 @@ namespace OpenTelemetry.Exporter
{
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
{

View File

@ -1,4 +1,4 @@
// <copyright file="JaegerExporterOptionsExtensions.cs" company="OpenTelemetry Authors">
// <copyright file="JaegerExporterProtocolParser.cs" company="OpenTelemetry Authors">
// 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;
}
}
}

View File

@ -0,0 +1,37 @@
// <copyright file="OtlpExportProtocolParser.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
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;
}
}
}
}

View File

@ -69,10 +69,9 @@ namespace OpenTelemetry.Exporter
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
{

View File

@ -121,14 +121,6 @@ namespace OpenTelemetry.Exporter
_ => 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

View File

@ -1,4 +1,4 @@
// <copyright file="JaegerExporterOptionsExtensionsTests.cs" company="OpenTelemetry Authors">
// <copyright file="JaegerExporterProtocolParserTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
@ -18,16 +18,17 @@ using Xunit;
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);
}
}

View File

@ -0,0 +1,35 @@
// <copyright file="OtlpExportProtocolParserTests.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
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);
}
}
}

View File

@ -145,17 +145,6 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
Assert.Throws<NotSupportedException>(() => 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")]