Skip to content

Commit

Permalink
[Instrumentation.Owin] Add IConfiguration support (open-telemetry#1973)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored and ezhang6811 committed Aug 8, 2024
1 parent 56b82fa commit 021ef8c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 32 deletions.
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Instrumentation.Owin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,7 +11,7 @@ namespace OpenTelemetry.Instrumentation.Owin;
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Instrumentation-Owin")]
internal sealed class OwinInstrumentationEventSource : EventSource
internal sealed class OwinInstrumentationEventSource : EventSource, IConfigurationExtensionsLogger
{
public static OwinInstrumentationEventSource Log { get; } = new OwinInstrumentationEventSource();

Expand Down Expand Up @@ -44,32 +45,28 @@ 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
{
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

<ItemGroup>
<Compile Include="$(RepoRoot)\src\Shared\AssemblyVersionExtensions.cs" Link="Includes\AssemblyVersionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Configuration\*.cs" Link="Includes\Configuration\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\Shared\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\Shared\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Options\*.cs" Link="Includes\Options\%(Filename).cs" />
<Compile Include="$(RepoRoot)\src\Shared\RedactionHelper.cs" Link="Includes\RedactionHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SemanticConventions.cs" Link="Includes\SemanticConventions.cs"/>
<Compile Include="$(RepoRoot)\src\Shared\SpanHelper.cs" Link="Includes\SpanHelper.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Owin;

namespace OpenTelemetry.Instrumentation.Owin;
Expand All @@ -17,18 +18,20 @@ public class OwinInstrumentationOptions
/// Initializes a new instance of the <see cref="OwinInstrumentationOptions"/> class.
/// </summary>
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;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<IOptionsMonitor<OwinInstrumentationOptions>>().Get(name: null);

builder.AddSource(OwinInstrumentationActivitySource.ActivitySourceName);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -277,20 +279,39 @@ 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");
}

List<Activity> stoppedActivities = new List<Activity>();

var builder = Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
{
if (disableQueryRedactionUsingConfiguration)
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>()
{
["OTEL_DOTNET_EXPERIMENTAL_OWIN_DISABLE_URL_QUERY_REDACTION"] = "true",
})
.Build();

services.AddSingleton<IConfiguration>(config);
}
})
.AddInMemoryExporter(stoppedActivities)
.AddOwinInstrumentation()
.Build();
Expand Down

0 comments on commit 021ef8c

Please sign in to comment.