We use New Relic for monitoring ASP.NET Core web applications. In one web service, we want to ignore requests for static files; there are a large number of static file requests (that are served quickly), which artificially lowers the average response time and error rate, and they’re unnecessary noise in New Relic.
One way would be to install the New Relic API NuGet package, then explicitly ignore the transaction:
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
if (context.File.Name == "file-to-ignore.txt")
NewRelic.Api.Agent.NewRelic.IgnoreTransaction();
}
});
Another way would be to use custom instrumentation. This is an XML file that specifies method names to match and ignore.
My first approach ignored StaticFileMiddleware.Invoke
. Unfortunately, the static file middleware
will be called on every request (if you put UseStaticFiles
before UseMvc
), so this method will always be invoked and every request
will be ignored.
I tried to refine this by ignoring a method that’s only called when a static file is served:
<match assemblyName="Microsoft.AspNetCore.StaticFiles" className="Microsoft.AspNetCore.StaticFiles.StaticFileContext">
<exactMethodMatcher methodName="SendAsync" />
<exactMethodMatcher methodName="SendStatusAsync" />
</match>
However, the static file requests were still being tracked. Inspecting the New Relic log file revealed this line:
[Error] 2020-01-07 21:51:50 Skipping sequential layout method: (Module: C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\2.2.6\Microsoft.AspNetCore.StaticFiles.dll, AppDomain: clrhost)[Microsoft.AspNetCore.StaticFiles]Microsoft.AspNetCore.StaticFiles.StaticFileContext.SendStatusAsync(System.Int32)
“Sequential layout” made me think of the LayoutKind
enum.
When a coworker pointed out that StaticFileContext
is a struct
,
I made the assumption that New Relic can’t profile methods belonging to a struct, at least when [StructLayout(LayoutKind.Sequential)]
is applied, which is the C#
compiler default. (I don’t know why it has this limitation. Searching for the log message above provided no results; maybe it will now at least return this blog post?)
So I next searched for a method (on a class) that is only invoked when StaticFileContext
sends a static file, and found LoggerExtensions
.
I finally settled on this custom instrumentation to ignore HEAD
requests that are handled by StaticFileMiddleware
:
<instrumentation>
<tracerFactory name="NewRelic.Agent.Core.Tracer.Factories.IgnoreTransactionTracerFactory">
<match assemblyName="Microsoft.AspNetCore.StaticFiles" className="Microsoft.AspNetCore.StaticFiles.LoggerExtensions">
<exactMethodMatcher methodName="Handled" /> <!-- .NET Core 3.0 -->
<exactMethodMatcher methodName="LogHandled" /> <!-- .NET Core 2.2.6 -->
</match>
</tracerFactory>
</instrumentation>
Use with caution: there is no logging that a request is being ignored due to custom instrumentation, so you can (and I did!) waste a lot of time tracking down missing requests that are due to this. The code-based approach at the top of this post is likely to be better in most situations.
Posted by Bradley Grainger on January 08, 2020