Skip to content

[ASP.NET Core] Option to prevent getting unviewed profilers #396

@hdt80

Description

@hdt80

Occasionally when starting a new profiler, the step named MiniProfiler Prep takes much longer than what is being profiled to complete. For example, a request to one of my API endpoints took 129.3ms to complete, with 127.50ms of that being MiniProfiler Prep (https://github.com/MiniProfiler/dotnet/blob/master/src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs#L77).

I think this is due to the call to SetHeadersAndState (https://github.com/MiniProfiler/dotnet/blob/master/src/MiniProfiler.AspNetCore/MiniProfilerMiddleware.cs#L142), which gets the IDs of the unviewed profiles from the underlying Storage, which I have set to an SqlServerStorage, and this query occasionally takes a while to execute. This adds a lot of time to perform an API request, which is a bit longer than I'd prefer.

There are two solutions I have thought of to work around this problem.

  1. Implement MiniProfilerOptions.ResultsAuthorize and implement middleware to handle authorization instead.

  2. Created a wrapper IAsyncStorage around SqlServerStorage.

    • The wrapper IAsyncStorage would internally use an SqlServerStorage for all methods (ex: public async Task SaveAsync(MiniProfiler p) => _InternalStorage.SaveAsync(p)), except for GetUnviewedIds, it would just return an empty List<Guid>, and not execute the SQL query.

While I don't see any issues with both of these methods (Haven't actually tried them yet), I would much prefer just having an option during .ConfigureServices to not get the past profilers. For example:

services.AddMiniProfiler(options => {
    options.GrabPastProfilers = false;
});

This would be used in MiniProfilerOptions.SetHeadersAndState:

From:

var profilerIds = (isAuthorized ? await Options.ExpireAndGetUnviewedAsync(current.User).ConfigureAwait(false) : null)
    ?? new List<Guid>(1);

To:

var profilerIds = (isAuthorized && Options.GrabPastProfilers ? await Options.ExpireAndGetUnviewedAsync(current.User).ConfigureAwait(false) : null)
    ?? new List<Guid>(1);

Thank you for taking the time to read this, let me know if you have any questions and I'll get back to you as soon as I can.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions