Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentry tracing middleware should be added automatically #2202

Closed
mattjohnsonpint opened this issue Feb 28, 2023 · 3 comments · Fixed by #2602
Closed

Sentry tracing middleware should be added automatically #2202

mattjohnsonpint opened this issue Feb 28, 2023 · 3 comments · Fixed by #2602
Assignees
Labels
Feature New feature or request

Comments

@mattjohnsonpint
Copy link
Contributor

mattjohnsonpint commented Feb 28, 2023

Problem Statement

In an ASP.NET Core app, enabling Sentry tracing currently requires two parts:

  • Enabling tracing in general, using EnableTracing, TracesSampleRate, or TracesSampler
  • Enabling automatic instrumentation, using app.UseSentryTracing() to add the ASP.NET Core tracing middleware

It's easy to forget adding one step or the other. It's also easy to place the middleware at the wrong location, since it has to be after UseRouting and before UseEndpoints.

Solution Brainstorm

If tracing is enabled, we can add the tracing middleware automatically at the correct location. We could then deprecate UseSentryTracing.

There's a detailed example of how to do this in the following blog posts:

  1. https://andrewlock.net/inserting-middleware-between-userouting-and-useendpoints-as-a-library-author-part-1/
  2. https://andrewlock.net/inserting-middleware-between-userouting-and-useendpoints-as-a-library-author-part-2/

Alternative solution

Andrew's solution is quite ingenious... a simpler partial solution would be to create an extension that people would use instead of UseRouting:

    /// <summary>
    /// Adds Sentry's tracing middleware to the pipeline.
    /// Call this instead of <c>builder.UseRouting()</c>.
    /// </summary>
    public static IApplicationBuilder UseSentryTraceRouting(this IApplicationBuilder builder)
    {
        builder.UseRouting();
        builder.UseMiddleware<SentryTracingMiddleware>();
        return builder;
    }

And then when people are wiring up their middleware they could just do this (see commented code... which is what gets replaced):

    .Configure(app =>
    {
        // app.UseRouting();
        // app.UseSentryTracing();
        app.UseSentryTraceRouting();

        app.UseEndpoints(routes =>
        {
            routes.Map("/person/{id}", async ctx =>
            {
                var id = ctx.GetRouteValue("id") as string;
                await ctx.Response.WriteAsync($"Person #{id}");
            });
        });
    });

This doesn't solve the problem of also requiring EnableTracing, TracesSampleRate, or TracesSampler to be set on the options however, so Tracing still needs to be configured in two locations with this solution.

@mattjohnsonpint
Copy link
Contributor Author

We think this is an important item, because it helps customers fall into the pit of success with setting up Sentry for ASP.NET Core.

@mattjohnsonpint
Copy link
Contributor Author

... since it has to be after UseRouting and before UseEndpoints.

Before starting anything, we should validate that. The last few aspnet core apps I've written don't even have those, so I'm not sure exactly which set of middlewares need to run before / after ours and why. I believe it has much to do with getting the correct transaction name based on the route, but this needs more investigation.

Thanks

@mattjohnsonpint
Copy link
Contributor Author

We may need to rethink this in the context of opentelemetry...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature or request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants