Skip to content

Commit 2e30089

Browse files
KielekCodeBlanch
andauthored
[otlp] Remove AppContext verification when using grpc + insecure endpoints (#5486)
Co-authored-by: Mikel Blanchard <[email protected]>
1 parent 1374a7d commit 2e30089

File tree

12 files changed

+15
-229
lines changed

12 files changed

+15
-229
lines changed

examples/Console/TestLogs.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ internal static object Run(LogsOptions options)
4141
*
4242
*/
4343

44-
// Adding the OtlpExporter creates a GrpcChannel.
45-
// This switch must be set before creating a GrpcChannel when calling an insecure gRPC service.
46-
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
47-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
48-
4944
var protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
5045

5146
if (options.Protocol.Trim().ToLower().Equals("grpc"))

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Unreleased
44

5+
* `OtlpExporter` will no longer throw an exception (even on .NET Core 3.1)
6+
when the `System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport`
7+
`AppContext` switch is NOT set AND using `OtlpExportProtocol.Grpc`
8+
to send to an insecure ("http") endpoint.
9+
`System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport`
10+
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).
11+
([#5486](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5486))
12+
513
## 1.8.0-rc.1
614

715
Released 2024-Mar-27

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpGrpcExportClient.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
2020
Guard.ThrowIfNull(options);
2121
Guard.ThrowIfInvalidTimeout(options.TimeoutMilliseconds);
2222

23-
ExporterClientValidation.EnsureUnencryptedSupportIsEnabled(options);
24-
2523
this.Endpoint = new UriBuilder(options.Endpoint).Uri;
2624
this.Headers = options.GetMetadataFromHeaders();
2725
this.TimeoutMilliseconds = options.TimeoutMilliseconds;

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/ExporterClientValidation.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/ExporterClientValidationTests.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/Http2UnencryptedSupportTests.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/IntegrationTests.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -278,33 +278,6 @@ public void LogExportResultIsSuccess(OtlpExportProtocol protocol, string endpoin
278278
}
279279
}
280280

281-
[Trait("CategoryName", "CollectorIntegrationTests")]
282-
[SkipUnlessEnvVarFoundFact(CollectorHostnameEnvVarName)]
283-
public void ConstructingGrpcExporterFailsWhenHttp2UnencryptedSupportIsDisabledForNetcoreapp31()
284-
{
285-
// Adding the OtlpExporter creates a GrpcChannel.
286-
// This switch must be set before creating a GrpcChannel/HttpClient when calling an insecure gRPC service.
287-
// We want to fail fast so we are disabling it
288-
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
289-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
290-
291-
var exporterOptions = new OtlpExporterOptions
292-
{
293-
Endpoint = new Uri($"http://{CollectorHostname}:4317"),
294-
};
295-
296-
var exception = Record.Exception(() => new OtlpTraceExporter(exporterOptions));
297-
298-
if (Environment.Version.Major == 3)
299-
{
300-
Assert.NotNull(exception);
301-
}
302-
else
303-
{
304-
Assert.Null(exception);
305-
}
306-
}
307-
308281
private sealed class OpenTelemetryEventListener : EventListener
309282
{
310283
private readonly ITestOutputHelper outputHelper;

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExportProtocolParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
77

8-
public class OtlpExportProtocolParserTests : Http2UnencryptedSupportTests
8+
public class OtlpExportProtocolParserTests
99
{
1010
[Theory]
1111
[InlineData("grpc", true, OtlpExportProtocol.Grpc)]

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsExtensionsTests.cs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
1414

15-
public class OtlpExporterOptionsExtensionsTests : Http2UnencryptedSupportTests
15+
public class OtlpExporterOptionsExtensionsTests
1616
{
1717
[Theory]
1818
[InlineData("key=value", new string[] { "key" }, new string[] { "value" })]
@@ -94,14 +94,6 @@ public void GetHeaders_NoOptionHeaders_ReturnsStandardHeaders(string optionHeade
9494
[InlineData(OtlpExportProtocol.HttpProtobuf, typeof(OtlpHttpTraceExportClient))]
9595
public void GetTraceExportClient_SupportedProtocol_ReturnsCorrectExportClient(OtlpExportProtocol protocol, Type expectedExportClientType)
9696
{
97-
if (protocol == OtlpExportProtocol.Grpc && Environment.Version.Major == 3)
98-
{
99-
// Adding the OtlpExporter creates a GrpcChannel.
100-
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
101-
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
102-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
103-
}
104-
10597
var options = new OtlpExporterOptions
10698
{
10799
Protocol = protocol,
@@ -112,32 +104,6 @@ public void GetTraceExportClient_SupportedProtocol_ReturnsCorrectExportClient(Ot
112104
Assert.Equal(expectedExportClientType, exportClient.GetType());
113105
}
114106

115-
[Fact]
116-
public void GetTraceExportClient_GetClientForGrpcWithoutUnencryptedFlag_ThrowsException()
117-
{
118-
// Adding the OtlpExporter creates a GrpcChannel.
119-
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
120-
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
121-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
122-
123-
var options = new OtlpExporterOptions
124-
{
125-
Protocol = OtlpExportProtocol.Grpc,
126-
};
127-
128-
var exception = Record.Exception(() => options.GetTraceExportClient());
129-
130-
if (Environment.Version.Major == 3)
131-
{
132-
Assert.NotNull(exception);
133-
Assert.IsType<InvalidOperationException>(exception);
134-
}
135-
else
136-
{
137-
Assert.Null(exception);
138-
}
139-
}
140-
141107
[Fact]
142108
public void GetTraceExportClient_UnsupportedProtocol_Throws()
143109
{

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
2626

27-
public class OtlpLogExporterTests : Http2UnencryptedSupportTests
27+
public class OtlpLogExporterTests
2828
{
2929
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();
3030

@@ -111,14 +111,6 @@ public void UserHttpFactoryCalledWhenUsingHttpProtobuf()
111111
[Fact]
112112
public void AddOtlpExporterSetsDefaultBatchExportProcessor()
113113
{
114-
if (Environment.Version.Major == 3)
115-
{
116-
// Adding the OtlpExporter creates a GrpcChannel.
117-
// This switch must be set before creating a GrpcChannel when calling an insecure HTTP/2 endpoint.
118-
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
119-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
120-
}
121-
122114
var loggerProvider = Sdk.CreateLoggerProviderBuilder()
123115
.AddOtlpExporter()
124116
.Build();
@@ -152,7 +144,6 @@ public void AddOtlpLogExporterReceivesAttributesWithParseStateValueSetToFalse()
152144
{
153145
bool optionsValidated = false;
154146

155-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
156147
var logRecords = new List<LogRecord>();
157148
using var loggerFactory = LoggerFactory.Create(builder =>
158149
{
@@ -185,7 +176,6 @@ public void AddOtlpLogExporterReceivesAttributesWithParseStateValueSetToFalse()
185176
[InlineData(false)]
186177
public void AddOtlpLogExporterParseStateValueCanBeTurnedOff(bool parseState)
187178
{
188-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
189179
var logRecords = new List<LogRecord>();
190180
using var loggerFactory = LoggerFactory.Create(builder =>
191181
{
@@ -231,7 +221,6 @@ public void AddOtlpLogExporterParseStateValueCanBeTurnedOffHosting(bool parseSta
231221
{
232222
var logRecords = new List<LogRecord>();
233223

234-
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
235224
var hostBuilder = new HostBuilder();
236225
hostBuilder.ConfigureLogging(logging => logging
237226
.AddOpenTelemetry(options => options

0 commit comments

Comments
 (0)