AspNetCore instrumentation options to be configurable from DI (#1997)

This commit is contained in:
Cijo Thomas 2021-04-19 22:31:28 -07:00 committed by GitHub
parent 13866c025b
commit f65b650805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 7 deletions

View File

@ -24,6 +24,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
@ -95,6 +96,19 @@ namespace Examples.AspNetCore
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddConsoleExporter());
// For options which can be bound from IConfiguration.
services.Configure<AspNetCoreInstrumentationOptions>(this.Configuration.GetSection("AspNetCoreInstrumentation"));
// For options which can be configured from code only.
services.Configure<AspNetCoreInstrumentationOptions>(options =>
{
options.Filter = (req) =>
{
return req.Request.Host != null;
};
});
break;
}
}

View File

@ -20,5 +20,8 @@
"Otlp": {
"ServiceName": "otlp-test",
"Endpoint": "http://localhost:4317"
},
"AspNetCoreInstrumentation": {
"RecordException": "true"
}
}

View File

@ -2,6 +2,10 @@
## Unreleased
* When using OpenTelemetry.Extensions.Hosting you can now bind
`AspNetCoreInstrumentationOptions` from DI.
([#1997](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1997))
## 1.0.0-rc3
Released 2021-Mar-19

View File

@ -9,6 +9,7 @@
<ItemGroup>
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\GrpcTagHelper.cs" Link="Includes\GrpcTagHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.GrpcNetClient\StatusCanonicalCode.cs" Link="Includes\StatusCanonicalCode.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ServiceProviderExtensions.cs" Link="Includes\ServiceProviderExtensions.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -57,6 +57,29 @@ This instrumentation can be configured to change the default behavior by using
`AspNetCoreInstrumentationOptions`, which allows adding [`Filter`](#filter),
[`Enrich`](#enrich) as explained below.
// TODO: This section could be refined.
When used with [`OpenTelemetry.Extensions.Hosting`](../OpenTelemetry.Extensions.Hosting/README.md),
all configurations to `AspNetCoreInstrumentationOptions` can be done in the `ConfigureServices`
method of you applications `Startup` class as shown below.
```csharp
// Configure
services.Configure<AspNetCoreInstrumentationOptions>(options =>
{
options.Filter = (req) =>
{
// only collect telemetry about HTTP GET requests
return httpContext.Request.Method.Equals("GET");
};
});
services.AddOpenTelemetryTracing(
(builder) => builder
.AddAspNetCoreInstrumentation()
.AddJaegerExporter()
);
```
### Filter
This instrumentation by default collects all the incoming http requests. It

View File

@ -40,14 +40,25 @@ namespace OpenTelemetry.Trace
throw new ArgumentNullException(nameof(builder));
}
var aspnetCoreOptions = new AspNetCoreInstrumentationOptions();
configureAspNetCoreInstrumentationOptions?.Invoke(aspnetCoreOptions);
builder.AddInstrumentation(() => new AspNetCoreInstrumentation(aspnetCoreOptions));
if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder)
{
return deferredTracerProviderBuilder.Configure((sp, builder) =>
{
AddAspNetCoreInstrumentation(builder, sp.GetOptions<AspNetCoreInstrumentationOptions>(), configureAspNetCoreInstrumentationOptions);
});
}
return AddAspNetCoreInstrumentation(builder, new AspNetCoreInstrumentationOptions(), configureAspNetCoreInstrumentationOptions);
}
private static TracerProviderBuilder AddAspNetCoreInstrumentation(TracerProviderBuilder builder, AspNetCoreInstrumentationOptions options, Action<AspNetCoreInstrumentationOptions> configure = null)
{
configure?.Invoke(options);
var instrumentation = new AspNetCoreInstrumentation(options);
builder.AddSource(HttpInListener.ActivitySourceName);
builder.AddLegacySource(HttpInListener.ActivityOperationName); // for the activities created by AspNetCore
builder.AddLegacySource(HttpInListener.ActivityNameByHttpInListener); // for the sibling activities created by the instrumentation library
return builder;
return builder.AddInstrumentation(() => instrumentation);
}
}
}

View File

@ -14,7 +14,8 @@
// limitations under the License.
// </copyright>
#if NET461 || NETSTANDARD2_0
#if NET461 || NETSTANDARD2_0 || NETSTANDARD2_1
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
#endif
@ -34,7 +35,7 @@ namespace System
public static T GetOptions<T>(this IServiceProvider serviceProvider)
where T : class, new()
{
#if NET461 || NETSTANDARD2_0
#if NET461 || NETSTANDARD2_0 || NETSTANDARD2_1
IOptions<T> options = (IOptions<T>)serviceProvider.GetService(typeof(IOptions<T>));
// Note: options could be null if user never invoked services.AddOptions().