Reputation: 91666
According to the docs, under "Configure sampling settings", we can configure adaptive sampling and also include or exclude certain types from sampling:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, TelemetryConfiguration configuration)
{
var builder = configuration.DefaultTelemetrySink.TelemetryProcessorChainBuilder;
// For older versions of the Application Insights SDK, use the following line instead:
// var builder = configuration.TelemetryProcessorChainBuilder;
// Using adaptive sampling
builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5);
// Alternately, the following configures adaptive sampling with 5 items per second, and also excludes DependencyTelemetry from being subject to sampling.
// builder.UseAdaptiveSampling(maxTelemetryItemsPerSecond:5, excludedTypes: "Dependency");
// If you have other telemetry processors:
builder.Use((next) => new AnotherProcessor(next));
builder.Build();
// ...
}
However, is there a way to do the same thing if we're configuring our application through WebHostBuilder.Configure()
and passing in a delegate that takes an IApplicationBuilder
? In this situation, we wouldn't be able to obtain the TelemetryConfiguration
through DI.
I need to do something similar as the above, but I've only got the IApplicationBuilder
interface. From the docs, it seems like I should use TelemetryConfiguration.Active.DefaultTelemetrySink.TelemetryProcessorChainBuilder
, but TelemetryConfiguration.Active
says it's obsolete on .NET Core and also says it loads the instance from ApplicationInsights.config, which I don't even have. I'm creating an entire AppInsights client from code.
I suppose another way to do it would be to create my own TelemetryProcessor that does sampling exactly the way I want, but this seems like overkill. Basically I'd like to alter the configuration of the current AdaptiveSamplingTelemetryProcessor
in the pipeline.
Upvotes: 1
Views: 766
Reputation: 93173
The IApplicationBuilder
interface has an ApplicationServices
property, which you can use to resolve TelemetryConfiguration
. Here's an example of how to use it:
webHostBuilder.Configure(applicationBuilder =>
{
var telemetryConfiguration = applicationBuilder.ApplicationServices
.GetRequiredService<TelemetryConfiguration>();
// ...
});
This TelemetryConfiguration
instance, which is registered as a singleton, is the same instance you're given when using Startup.Configure
.
Upvotes: 1