Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[otlp] Remove AppContext verification when using grpc + insecure endpoints #5486

Merged
merged 6 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions examples/Console/TestLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ internal static object Run(LogsOptions options)
*
*/

// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel when calling an insecure gRPC service.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

var protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;

if (options.Protocol.Trim().ToLower().Equals("grpc"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

* `OtlpExporter` will no longer throw an exception (even on .NET Core 3.1)
when the `System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport`
`AppContext` switch is NOT set AND using `OtlpExportProtocol.Grpc`
to send to an insecure ("http") endpoint.
`System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport`
is not required to be set [when using .NET 5 or newer](https://learn.microsoft.com/aspnet/core/grpc/troubleshoot?view=aspnetcore-8.0#call-insecure-grpc-services-with-net-core-client).
([#5486](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5486))

## 1.8.0-rc.1

Released 2024-Mar-27
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
Guard.ThrowIfNull(options);
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);

ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options);

this.Endpoint = new UriBuilder(options.Endpoint).Uri;
this.Headers = options.GetMetadataFromHeaders();
this.TimeoutMilliseconds = options.TimeoutMilliseconds;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -278,33 +278,6 @@ public void LogExportResultIsSuccess(OtlpExportProtocol protocol, string endpoin
}
}

[Trait("CategoryName", "CollectorIntegrationTests")]
[SkipUnlessEnvVarFoundFact(CollectorHostnameEnvVarName)]
public void ConstructingGrpcExporterFailsWhenHttp2UnencryptedSupportIsDisabledForNetcoreapp31()
{
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel/HttpClient when calling an insecure gRPC service.
// We want to fail fast so we are disabling it
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);

var exporterOptions = new OtlpExporterOptions
{
Endpoint = new Uri($"http://{CollectorHostname}:4317"),
};

var exception = Record.Exception(() => new OtlpTraceExporter(exporterOptions));

if (Environment.Version.Major == 3)
{
Assert.NotNull(exception);
}
else
{
Assert.Null(exception);
}
}

private sealed class OpenTelemetryEventListener : EventListener
{
private readonly ITestOutputHelper outputHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

public class OtlpExportProtocolParserTests : Http2UnencryptedSupportTests
public class OtlpExportProtocolParserTests
{
[Theory]
[InlineData("grpc", true, OtlpExportProtocol.Grpc)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

public class OtlpExporterOptionsExtensionsTests : Http2UnencryptedSupportTests
public class OtlpExporterOptionsExtensionsTests
{
[Theory]
[InlineData("key=value", new string[] { "key" }, new string[] { "value" })]
Expand Down Expand Up @@ -94,14 +94,6 @@ public void GetHeaders_NoOptionHeaders_ReturnsStandardHeaders(string optionHeade
[InlineData(OtlpExportProtocol.HttpProtobuf, typeof(OtlpHttpTraceExportClient))]
public void GetTraceExportClient_SupportedProtocol_ReturnsCorrectExportClient(OtlpExportProtocol protocol, Type expectedExportClientType)
{
if (protocol == OtlpExportProtocol.Grpc && Environment.Version.Major == 3)
{
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}

var options = new OtlpExporterOptions
{
Protocol = protocol,
Expand All @@ -112,32 +104,6 @@ public void GetTraceExportClient_SupportedProtocol_ReturnsCorrectExportClient(Ot
Assert.Equal(expectedExportClientType, exportClient.GetType());
}

[Fact]
public void GetTraceExportClient_GetClientForGrpcWithoutUnencryptedFlag_ThrowsException()
{
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);

var options = new OtlpExporterOptions
{
Protocol = OtlpExportProtocol.Grpc,
};

var exception = Record.Exception(() => options.GetTraceExportClient());

if (Environment.Version.Major == 3)
{
Assert.NotNull(exception);
Assert.IsType<InvalidOperationException>(exception);
}
else
{
Assert.Null(exception);
}
}

[Fact]
public void GetTraceExportClient_UnsupportedProtocol_Throws()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

public class OtlpLogExporterTests : Http2UnencryptedSupportTests
public class OtlpLogExporterTests
{
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();

Expand Down Expand Up @@ -111,14 +111,6 @@ public void UserHttpFactoryCalledWhenUsingHttpProtobuf()
[Fact]
public void AddOtlpExporterSetsDefaultBatchExportProcessor()
{
if (Environment.Version.Major == 3)
{
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}

var loggerProvider = Sdk.CreateLoggerProviderBuilder()
.AddOtlpExporter()
.Build();
Expand Down Expand Up @@ -152,7 +144,6 @@ public void AddOtlpLogExporterReceivesAttributesWithParseStateValueSetToFalse()
{
bool optionsValidated = false;

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var logRecords = new List<LogRecord>();
using var loggerFactory = LoggerFactory.Create(builder =>
{
Expand Down Expand Up @@ -185,7 +176,6 @@ public void AddOtlpLogExporterReceivesAttributesWithParseStateValueSetToFalse()
[InlineData(false)]
public void AddOtlpLogExporterParseStateValueCanBeTurnedOff(bool parseState)
{
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var logRecords = new List<LogRecord>();
using var loggerFactory = LoggerFactory.Create(builder =>
{
Expand Down Expand Up @@ -231,7 +221,6 @@ public void AddOtlpLogExporterParseStateValueCanBeTurnedOffHosting(bool parseSta
{
var logRecords = new List<LogRecord>();

AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var hostBuilder = new HostBuilder();
hostBuilder.ConfigureLogging(logging => logging
.AddOpenTelemetry(options => options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

[Collection("EnvVars")]
public class OtlpMetricsExporterTests : Http2UnencryptedSupportTests
public class OtlpMetricsExporterTests : IDisposable
{
private static readonly KeyValuePair<string, object>[] KeyValues = new KeyValuePair<string, object>[]
{
Expand All @@ -35,14 +35,6 @@ public OtlpMetricsExporterTests()
[Fact]
public void TestAddOtlpExporter_SetsCorrectMetricReaderDefaults()
{
if (Environment.Version.Major == 3)
{
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}

var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddOtlpExporter()
.Build();
Expand Down Expand Up @@ -916,11 +908,10 @@ void AssertExemplars<T>(T value, Metric metric)
}
}

protected override void Dispose(bool disposing)
public void Dispose()
{
OtlpSpecConfigDefinitionTests.ClearEnvVars();

base.Dispose(disposing);
GC.SuppressFinalize(this);
}

private static void VerifyExemplars<T>(long? longValue, double? doubleValue, bool enableExemplars, Func<T, OtlpMetrics.Exemplar> getExemplarFunc, T state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

[Collection("xUnitCollectionPreventingTestsThatDependOnSdkConfigurationFromRunningInParallel")]
public class OtlpTraceExporterTests : Http2UnencryptedSupportTests
public class OtlpTraceExporterTests
{
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();

Expand Down Expand Up @@ -581,14 +581,6 @@ public void ToOtlpSpanPeerServiceTest()
[Fact]
public void UseOpenTelemetryProtocolActivityExporterWithCustomActivityProcessor()
{
if (Environment.Version.Major == 3)
{
// Adding the OtlpExporter creates a GrpcChannel.
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
}

const string ActivitySourceName = "otlp.test";
TestActivityProcessor testActivityProcessor = new TestActivityProcessor();

Expand Down