From 021ef8cd0d33a5f0d8a112435d7bdd13619d88ab Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Thu, 25 Jul 2024 10:23:38 -0700 Subject: [PATCH] [Instrumentation.Owin] Add IConfiguration support (#1973) --- .../CHANGELOG.md | 4 +++ .../OwinInstrumentationEventSource.cs | 25 +++++++--------- .../OpenTelemetry.Instrumentation.Owin.csproj | 3 ++ .../OwinInstrumentationOptions.cs | 23 ++++++++------- .../TracerProviderBuilderExtensions.cs | 21 +++++++++++--- .../DiagnosticsMiddlewareTests.cs | 29 ++++++++++++++++--- 6 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Owin/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Owin/CHANGELOG.md index 9681f46fc4..b9f8971ff3 100644 --- a/src/OpenTelemetry.Instrumentation.Owin/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Owin/CHANGELOG.md @@ -5,6 +5,10 @@ * Updated OpenTelemetry core component version(s) to `1.9.0`. ([#1888](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1888)) +* Updated registration extension code to retrieve environment variables through + `IConfiguration`. + ([#1973](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1973)) + ## 1.0.0-rc.6 Released 2024-Apr-19 diff --git a/src/OpenTelemetry.Instrumentation.Owin/Implementation/OwinInstrumentationEventSource.cs b/src/OpenTelemetry.Instrumentation.Owin/Implementation/OwinInstrumentationEventSource.cs index b3fd0a062e..774cd1fa10 100644 --- a/src/OpenTelemetry.Instrumentation.Owin/Implementation/OwinInstrumentationEventSource.cs +++ b/src/OpenTelemetry.Instrumentation.Owin/Implementation/OwinInstrumentationEventSource.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 using System.Diagnostics.Tracing; +using Microsoft.Extensions.Configuration; using OpenTelemetry.Internal; namespace OpenTelemetry.Instrumentation.Owin; @@ -10,7 +11,7 @@ namespace OpenTelemetry.Instrumentation.Owin; /// EventSource events emitted from the project. /// [EventSource(Name = "OpenTelemetry-Instrumentation-Owin")] -internal sealed class OwinInstrumentationEventSource : EventSource +internal sealed class OwinInstrumentationEventSource : EventSource, IConfigurationExtensionsLogger { public static OwinInstrumentationEventSource Log { get; } = new OwinInstrumentationEventSource(); @@ -44,25 +45,21 @@ public void EnrichmentException(Exception exception) } } - [NonEvent] - public void FailedToReadEnvironmentVariable(string envVarName, Exception ex) - { - if (this.IsEnabled(EventLevel.Error, EventKeywords.All)) - { - this.FailedToReadEnvironmentVariable(envVarName, ex.ToInvariantString()); - } - } - [Event(EventIds.EnrichmentException, Message = "Enrichment threw exception. Exception {0}.", Level = EventLevel.Error)] public void EnrichmentException(string exception) { this.WriteEvent(EventIds.EnrichmentException, exception); } - [Event(EventIds.FailedToReadEnvironmentVariable, Message = "Failed to read environment variable='{0}': {1}", Level = EventLevel.Error)] - public void FailedToReadEnvironmentVariable(string envVarName, string exception) + [Event(EventIds.InvalidConfigurationValue, Message = "Configuration key '{0}' has an invalid value: '{1}'", Level = EventLevel.Warning)] + public void InvalidConfigurationValue(string key, string value) + { + this.WriteEvent(EventIds.InvalidConfigurationValue, key, value); + } + + void IConfigurationExtensionsLogger.LogInvalidConfigurationValue(string key, string value) { - this.WriteEvent(4, envVarName, exception); + this.InvalidConfigurationValue(key, value); } private class EventIds @@ -70,6 +67,6 @@ private class EventIds public const int RequestIsFilteredOut = 1; public const int RequestFilterException = 2; public const int EnrichmentException = 3; - public const int FailedToReadEnvironmentVariable = 4; + public const int InvalidConfigurationValue = 4; } } diff --git a/src/OpenTelemetry.Instrumentation.Owin/OpenTelemetry.Instrumentation.Owin.csproj b/src/OpenTelemetry.Instrumentation.Owin/OpenTelemetry.Instrumentation.Owin.csproj index b2aae6f9b7..de9b7691d5 100644 --- a/src/OpenTelemetry.Instrumentation.Owin/OpenTelemetry.Instrumentation.Owin.csproj +++ b/src/OpenTelemetry.Instrumentation.Owin/OpenTelemetry.Instrumentation.Owin.csproj @@ -14,8 +14,11 @@ + + + diff --git a/src/OpenTelemetry.Instrumentation.Owin/OwinInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.Owin/OwinInstrumentationOptions.cs index d48269a8d7..9535e932de 100644 --- a/src/OpenTelemetry.Instrumentation.Owin/OwinInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.Owin/OwinInstrumentationOptions.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 using System.Diagnostics; +using Microsoft.Extensions.Configuration; using Microsoft.Owin; namespace OpenTelemetry.Instrumentation.Owin; @@ -17,18 +18,20 @@ public class OwinInstrumentationOptions /// Initializes a new instance of the class. /// public OwinInstrumentationOptions() + : this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) { - try - { - var disableQueryRedaction = Environment.GetEnvironmentVariable(DisableQueryRedactionEnvVar); - if (disableQueryRedaction != null && disableQueryRedaction.Equals("true", StringComparison.OrdinalIgnoreCase)) - { - this.DisableUrlQueryRedaction = true; - } - } - catch (Exception ex) + } + + internal OwinInstrumentationOptions(IConfiguration configuration) + { + Debug.Assert(configuration != null, "configuration was null"); + + if (configuration!.TryGetBoolValue( + OwinInstrumentationEventSource.Log, + DisableQueryRedactionEnvVar, + out var disableUrlQueryRedaction)) { - OwinInstrumentationEventSource.Log.FailedToReadEnvironmentVariable(DisableQueryRedactionEnvVar, ex); + this.DisableUrlQueryRedaction = disableUrlQueryRedaction; } } diff --git a/src/OpenTelemetry.Instrumentation.Owin/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.Owin/TracerProviderBuilderExtensions.cs index 62dc361cce..26a18725c7 100644 --- a/src/OpenTelemetry.Instrumentation.Owin/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.Owin/TracerProviderBuilderExtensions.cs @@ -1,6 +1,8 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using OpenTelemetry.Instrumentation.Owin; using OpenTelemetry.Internal; @@ -31,11 +33,22 @@ public static TracerProviderBuilder AddOwinInstrumentation( { Guard.ThrowIfNull(builder); - var owinOptions = new OwinInstrumentationOptions(); - configure?.Invoke(owinOptions); + return builder.ConfigureServices(services => + { + if (configure != null) + { + services.Configure(configure); + } - OwinInstrumentationActivitySource.Options = owinOptions; + services.RegisterOptionsFactory( + configuration => new OwinInstrumentationOptions(configuration)); - return builder.AddSource(OwinInstrumentationActivitySource.ActivitySourceName); + services.ConfigureOpenTelemetryTracerProvider((sp, builder) => + { + OwinInstrumentationActivitySource.Options = sp.GetRequiredService>().Get(name: null); + + builder.AddSource(OwinInstrumentationActivitySource.ActivitySourceName); + }); + }); } } diff --git a/test/OpenTelemetry.Instrumentation.Owin.Tests/DiagnosticsMiddlewareTests.cs b/test/OpenTelemetry.Instrumentation.Owin.Tests/DiagnosticsMiddlewareTests.cs index ded5c55532..2222fecfc7 100644 --- a/test/OpenTelemetry.Instrumentation.Owin.Tests/DiagnosticsMiddlewareTests.cs +++ b/test/OpenTelemetry.Instrumentation.Owin.Tests/DiagnosticsMiddlewareTests.cs @@ -4,6 +4,8 @@ using System.Diagnostics; using System.Net.Http; using System.Web.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Owin; using Microsoft.Owin.Hosting; using OpenTelemetry.Instrumentation.Owin.Implementation; @@ -277,13 +279,18 @@ Owin has finished to inspect the activity status. */ } [Theory] - [InlineData("path?a=b&c=d", "path?a=Redacted&c=Redacted", false)] - [InlineData("path?a=b&c=d", "path?a=b&c=d", true)] - public async Task QueryParametersAreRedacted(string actualPath, string expectedPath, bool disableQueryRedaction) + [InlineData("path?a=b&c=d", "path?a=Redacted&c=Redacted", false, false)] + [InlineData("path?a=b&c=d", "path?a=b&c=d", true, false)] + [InlineData("path?a=b&c=d", "path?a=b&c=d", false, true)] + public async Task QueryParametersAreRedacted( + string actualPath, + string expectedPath, + bool disableQueryRedactionUsingEnvVar, + bool disableQueryRedactionUsingConfiguration) { try { - if (disableQueryRedaction) + if (disableQueryRedactionUsingEnvVar) { Environment.SetEnvironmentVariable("OTEL_DOTNET_EXPERIMENTAL_OWIN_DISABLE_URL_QUERY_REDACTION", "true"); } @@ -291,6 +298,20 @@ public async Task QueryParametersAreRedacted(string actualPath, string expectedP List stoppedActivities = new List(); var builder = Sdk.CreateTracerProviderBuilder() + .ConfigureServices(services => + { + if (disableQueryRedactionUsingConfiguration) + { + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary() + { + ["OTEL_DOTNET_EXPERIMENTAL_OWIN_DISABLE_URL_QUERY_REDACTION"] = "true", + }) + .Build(); + + services.AddSingleton(config); + } + }) .AddInMemoryExporter(stoppedActivities) .AddOwinInstrumentation() .Build();