Skip to content

Commit

Permalink
AspNetCore instrumentation options to be configurable from DI (#1997)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Apr 20, 2021
1 parent 13866c0 commit f65b650
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
14 changes: 14 additions & 0 deletions examples/AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Expand Down Expand Up @@ -95,6 +96,19 @@ public void ConfigureServices(IServiceCollection services)
.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;
}
}
Expand Down
3 changes: 3 additions & 0 deletions examples/AspNetCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"Otlp": {
"ServiceName": "otlp-test",
"Endpoint": "http://localhost:4317"
},
"AspNetCoreInstrumentation": {
"RecordException": "true"
}
}
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
23 changes: 23 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,25 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation(
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);
}
}
}
5 changes: 3 additions & 2 deletions src/OpenTelemetry/Internal/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -34,7 +35,7 @@ internal static class ServiceProviderExtensions
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().
Expand Down

0 comments on commit f65b650

Please sign in to comment.