Skip to content

Commit

Permalink
.NET Core 3: Add Server-Timing trailer
Browse files Browse the repository at this point in the history
This adds an option (default off) to enable Server-Timing headers for requests. It's a trailer here so requires Kestrel/feature support. If this works well, we can look at adding a netcoreapp2.2 build for MiniProfiler.AspNetCore* which should also support the headers.

I think this works, but it's hard to verify because of an https:// issue in dotnet/aspnetcore#8952 which breaks Chrome and Firefox that won't be resolved until preview 7. Edge can connect (due to never fixing their TLS leniency...), but not show the trailers. Figures :)
  • Loading branch information
Nick Craver committed Jul 7, 2019
1 parent 8d0f969 commit a71fc6b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions samples/Samples.AspNetCore3/Startup.cs
Expand Up @@ -76,6 +76,9 @@ public void ConfigureServices(IServiceCollection services)
// Optionally disable "Connection Open()", "Connection Close()" (and async variants).
//options.TrackConnectionOpenClose = false;
// Enabled sending the Server-Timing header on responses
options.EnableServerTimingHeader = true;
options.IgnoredPaths.Add("/lib");
options.IgnoredPaths.Add("/css");
options.IgnoredPaths.Add("/js");
Expand Down
17 changes: 17 additions & 0 deletions src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs
Expand Up @@ -89,6 +89,16 @@ public async Task Invoke(HttpContext context)
{
await SetHeadersAndState(context, mp).ConfigureAwait(false);
}

#if NETCOREAPP3_0
var appendServerTimingHeader = Options.EnableServerTimingHeader && context.Response.SupportsTrailers();
if (appendServerTimingHeader)
{
context.Response.DeclareTrailer("Server-Timing");
appendServerTimingHeader = true;
}
#endif

// Execute the pipe
#pragma warning disable RCS1090 // Call 'ConfigureAwait(false)'.
await _next(context);
Expand All @@ -97,6 +107,13 @@ public async Task Invoke(HttpContext context)
EnsureName(mp, context);
// Stop (and record)
await mp.StopAsync().ConfigureAwait(false);

#if NETCOREAPP3_0 // TODO: Evaluate if this works after http/2 local support in preview 7, maybe backport to netcoreapp2.2
if (appendServerTimingHeader && mp != null)
{
context.Response.AppendTrailer("Server-Timing", mp.GetServerTimingHeader());
}
#endif
}
else
{
Expand Down
7 changes: 7 additions & 0 deletions src/MiniProfiler.AspNetCore/MiniProfilerOptions.cs
Expand Up @@ -40,5 +40,12 @@ public class MiniProfilerOptions : MiniProfilerBaseOptions
/// Function to provide the unique user ID based on the request, to store MiniProfiler IDs user
/// </summary>
public Func<HttpRequest, string> UserIdProvider { get; set; } = request => request.HttpContext.Connection.RemoteIpAddress?.ToString();

#if NETCOREAPP3_0
/// <summary>
/// Whether to add a Server-Timing header after profiling a request. Only supported in .NET Core 3.0 and higher.
/// </summary>
public bool EnableServerTimingHeader { get; set; }
#endif
}
}

0 comments on commit a71fc6b

Please sign in to comment.