Skip to content

Commit

Permalink
[Instrumentation.AspNet] Add IConfiguration support (open-telemetry#1976
Browse files Browse the repository at this point in the history
)
  • Loading branch information
CodeBlanch authored and ezhang6811 committed Aug 8, 2024
1 parent 021ef8c commit 2e3db0f
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Web;
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Instrumentation.AspNet.Implementation;

namespace OpenTelemetry.Instrumentation.AspNet;
Expand All @@ -18,18 +19,20 @@ public class AspNetTraceInstrumentationOptions
/// Initializes a new instance of the <see cref="AspNetTraceInstrumentationOptions"/> class.
/// </summary>
public AspNetTraceInstrumentationOptions()
: 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 AspNetTraceInstrumentationOptions(IConfiguration configuration)
{
Debug.Assert(configuration != null, "configuration was null");

if (configuration!.TryGetBoolValue(
AspNetInstrumentationEventSource.Log,
DisableQueryRedactionEnvVar,
out var disableUrlQueryRedaction))
{
AspNetInstrumentationEventSource.Log.FailedToReadEnvironmentVariable(DisableQueryRedactionEnvVar, ex);
this.DisableUrlQueryRedaction = disableUrlQueryRedaction;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNet/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Updated registration extension code to retrieve environment variables through
`IConfiguration`.
([#1976](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1976))

## 1.9.0-beta.1

Released 2024-Jun-18
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.AspNet.Implementation;
Expand All @@ -10,7 +11,7 @@ namespace OpenTelemetry.Instrumentation.AspNet.Implementation;
/// EventSource events emitted from the project.
/// </summary>
[EventSource(Name = "OpenTelemetry-Instrumentation-AspNet")]
internal sealed class AspNetInstrumentationEventSource : EventSource
internal sealed class AspNetInstrumentationEventSource : EventSource, IConfigurationExtensionsLogger
{
public static AspNetInstrumentationEventSource Log = new();

Expand All @@ -32,15 +33,6 @@ public void EnrichmentException(string eventName, Exception ex)
}
}

[NonEvent]
public void FailedToReadEnvironmentVariable(string envVarName, Exception ex)
{
if (this.IsEnabled(EventLevel.Error, EventKeywords.All))
{
this.EnrichmentException(envVarName, ex.ToInvariantString());
}
}

[Event(1, Message = "Request is filtered out and will not be collected. Operation='{0}'", Level = EventLevel.Verbose)]
public void RequestIsFilteredOut(string operationName)
{
Expand All @@ -59,9 +51,14 @@ public void EnrichmentException(string eventName, string exception)
this.WriteEvent(3, eventName, exception);
}

[Event(4, Message = "Failed to read environment variable='{0}': {1}", Level = EventLevel.Error)]
public void FailedToReadEnvironmentVariable(string envVarName, string exception)
[Event(4, Message = "Configuration key '{0}' has an invalid value: '{1}'", Level = EventLevel.Warning)]
public void InvalidConfigurationValue(string key, string value)
{
this.WriteEvent(4, key, value);
}

void IConfigurationExtensionsLogger.LogInvalidConfigurationValue(string key, string value)
{
this.WriteEvent(4, envVarName, exception);
this.InvalidConfigurationValue(key, value);
}
}
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.AspNet;
using OpenTelemetry.Internal;

Expand Down Expand Up @@ -31,10 +33,20 @@ public static MeterProviderBuilder AddAspNetInstrumentation(
{
Guard.ThrowIfNull(builder);

var options = new AspNetMetricsInstrumentationOptions();
configure?.Invoke(options);
return builder.ConfigureServices(services =>
{
if (configure != null)
{
services.Configure(configure);
}

builder.AddMeter(AspNetMetrics.InstrumentationName);
return builder.AddInstrumentation(() => new AspNetMetrics(options));
services.ConfigureOpenTelemetryMeterProvider((sp, builder) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<AspNetMetricsInstrumentationOptions>>().Get(name: null);

builder.AddInstrumentation(() => new AspNetMetrics(options));
builder.AddMeter(AspNetMetrics.InstrumentationName);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,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\PropertyFetcher.AOT.cs" Link="Includes\PropertyFetcher.AOT.cs" />
<Compile Include="$(RepoRoot)\src\Shared\RequestDataHelper.cs" Link="Includes\RequestDataHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\RedactionHelper.cs" Link="Includes\RedactionHelper.cs" />
Expand All @@ -33,4 +36,10 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule\OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsConfigurationPkgVer)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="$(MicrosoftExtensionsOptionsPkgVer)" />
<PackageReference Include="OpenTelemetry.Api.ProviderBuilderExtensions" Version="$(OpenTelemetryCoreLatestVersion)" />
</ItemGroup>

</Project>
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.AspNet;
using OpenTelemetry.Internal;

Expand Down Expand Up @@ -31,12 +33,23 @@ public static TracerProviderBuilder AddAspNetInstrumentation(
{
Guard.ThrowIfNull(builder);

var aspnetOptions = new AspNetTraceInstrumentationOptions();
configure?.Invoke(aspnetOptions);
return builder.ConfigureServices(services =>
{
if (configure != null)
{
services.Configure(configure);
}

builder.AddInstrumentation(() => new AspNetInstrumentation(aspnetOptions));
builder.AddSource(TelemetryHttpModule.AspNetSourceName);
services.RegisterOptionsFactory(
configuration => new AspNetTraceInstrumentationOptions(configuration));

return builder;
services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
{
var options = sp.GetRequiredService<IOptionsMonitor<AspNetTraceInstrumentationOptions>>().Get(name: null);

builder.AddInstrumentation(() => new AspNetInstrumentation(options));
builder.AddSource(TelemetryHttpModule.AspNetSourceName);
});
});
}
}
Loading

0 comments on commit 2e3db0f

Please sign in to comment.