Skip to content

Commit

Permalink
[Instrumentation.AspNetCore, Instrumentation.Http] Revert support for…
Browse files Browse the repository at this point in the history
… OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS (#1754)
  • Loading branch information
Kielek authored May 15, 2024
1 parent cf0258d commit 137e43b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class HttpInListener : IDisposable
{
private readonly HttpRequestRouteHelper routeHelper = new();
private readonly AspNetTraceInstrumentationOptions options;
private readonly RequestDataHelper requestDataHelper = new();
private readonly RequestDataHelper requestDataHelper = new(configureByHttpKnownMethodsEnvironmentalVariable: true);

public HttpInListener(AspNetTraceInstrumentationOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation;
internal sealed class HttpInMetricsListener : IDisposable
{
private readonly HttpRequestRouteHelper routeHelper = new();
private readonly RequestDataHelper requestDataHelper = new();
private readonly RequestDataHelper requestDataHelper = new(configureByHttpKnownMethodsEnvironmentalVariable: true);
private readonly Histogram<double> httpServerDuration;
private readonly AspNetMetricsInstrumentationOptions options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation;
internal static class TelemetryHelper
{
public static readonly object[] BoxedStatusCodes = InitializeBoxedStatusCodes();
internal static readonly RequestDataHelper RequestDataHelper = new();
internal static readonly RequestDataHelper RequestDataHelper = new(configureByHttpKnownMethodsEnvironmentalVariable: false);

public static object GetBoxedStatusCode(int statusCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenTelemetry.Instrumentation.Http.Implementation;
/// </summary>
internal static class HttpTagHelper
{
internal static readonly RequestDataHelper RequestDataHelper = new();
internal static readonly RequestDataHelper RequestDataHelper = new(configureByHttpKnownMethodsEnvironmentalVariable: false);

/// <summary>
/// Gets the OpenTelemetry standard uri tag value for a span based on its request <see cref="Uri"/>.
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/RequestDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ internal sealed class RequestDataHelper
private readonly Dictionary<string, string> knownHttpMethods;
#endif

public RequestDataHelper()
public RequestDataHelper(bool configureByHttpKnownMethodsEnvironmentalVariable)
{
var suppliedKnownMethods = Environment.GetEnvironmentVariable(KnownHttpMethodsEnvironmentVariable)
?.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries);
var suppliedKnownMethods = configureByHttpKnownMethodsEnvironmentalVariable ? Environment.GetEnvironmentVariable(KnownHttpMethodsEnvironmentVariable)
?.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries) : null;
var knownMethodSet = suppliedKnownMethods?.Length > 0
? suppliedKnownMethods.ToDictionary(x => x, x => x, StringComparer.OrdinalIgnoreCase)
: new(StringComparer.OrdinalIgnoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RequestDataHelperTests : IDisposable
[InlineData("invalid", "_OTHER")]
public void MethodMappingWorksForKnownMethods(string method, string expected)
{
var requestHelper = new RequestDataHelper();
var requestHelper = new RequestDataHelper(configureByHttpKnownMethodsEnvironmentalVariable: true);
var actual = requestHelper.GetNormalizedHttpMethod(method);
Assert.Equal(expected, actual);
}
Expand All @@ -55,11 +55,20 @@ public void MethodMappingWorksForKnownMethods(string method, string expected)
public void MethodMappingWorksForEnvironmentVariables(string method, string expected)
{
Environment.SetEnvironmentVariable("OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS", "GET,POST");
var requestHelper = new RequestDataHelper();
var requestHelper = new RequestDataHelper(configureByHttpKnownMethodsEnvironmentalVariable: true);
var actual = requestHelper.GetNormalizedHttpMethod(method);
Assert.Equal(expected, actual);
}

[Fact]
public void MethodMappingWorksIfEnvironmentalVariableConfigurationIsDisabled()
{
Environment.SetEnvironmentVariable("OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS", "GET,POST");
var requestHelper = new RequestDataHelper(configureByHttpKnownMethodsEnvironmentalVariable: false);
var actual = requestHelper.GetNormalizedHttpMethod("CONNECT");
Assert.Equal("CONNECT", actual);
}

[Theory]
[InlineData("HTTP/1.1", "1.1")]
[InlineData("HTTP/2", "2")]
Expand Down

0 comments on commit 137e43b

Please sign in to comment.