-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure Sentry tracing middleware automatically (#2602)
* Added SentryTracingStartupFilter so SDK users no longer need to call UseSentryTracing() with ASP.NET Core * Removed UseSentryTracing from samples * Don't register Tracing middleware automatically if OpenTelemetry has been configured * Update CHANGELOG.md
- Loading branch information
1 parent
b6e63c3
commit 9d52929
Showing
13 changed files
with
117 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=instrumenter/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Sentry.AspNetCore; | ||
|
||
/// <summary> | ||
/// Delegates/wraps the inner builder, so that we can intercept calls to <see cref="Use"/> and add our middleware | ||
/// </summary> | ||
internal class SentryTracingBuilder : IApplicationBuilder | ||
{ | ||
private const string EndpointRouteBuilder = "__EndpointRouteBuilder"; | ||
public SentryTracingBuilder(IApplicationBuilder inner) | ||
{ | ||
InnerBuilder = inner; | ||
} | ||
|
||
private IApplicationBuilder InnerBuilder { get; } | ||
|
||
/// <inheritdoc /> | ||
public IServiceProvider ApplicationServices | ||
{ | ||
get => InnerBuilder.ApplicationServices; | ||
set => InnerBuilder.ApplicationServices = value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IDictionary<string, object?> Properties => InnerBuilder.Properties; | ||
|
||
/// <inheritdoc /> | ||
public IFeatureCollection ServerFeatures => InnerBuilder.ServerFeatures; | ||
|
||
/// <inheritdoc /> | ||
public RequestDelegate Build() => InnerBuilder.Build(); | ||
|
||
/// <inheritdoc /> | ||
public IApplicationBuilder New() => new SentryTracingBuilder(InnerBuilder.New()); | ||
|
||
/// <inheritdoc /> | ||
public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware) | ||
{ | ||
// UseRouting sets a property on the builder that we use to detect when UseRouting has been added and we | ||
// register SentryTracing immediately afterwards | ||
// https://github.com/dotnet/aspnetcore/blob/8eaf4b51f73ae2b0ed079e4f8253afb53e96b703/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs#L58-L62 | ||
if (Properties.ContainsKey(EndpointRouteBuilder) && this.ShouldRegisterSentryTracing()) | ||
{ | ||
var options = InnerBuilder.ApplicationServices.GetService<IOptions<SentryAspNetCoreOptions>>(); | ||
var instrumenter = options?.Value.Instrumenter ?? Instrumenter.Sentry; | ||
if (instrumenter == Instrumenter.Sentry) | ||
{ | ||
return InnerBuilder.Use(middleware).UseSentryTracing(); | ||
} | ||
this.StoreInstrumenter(instrumenter); // Saves us from having to resolve the options to make this check again | ||
} | ||
|
||
return InnerBuilder.Use(middleware); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace Sentry.AspNetCore; | ||
|
||
internal class SentryTracingStartupFilter: IStartupFilter | ||
{ | ||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
{ | ||
return builder => | ||
{ | ||
var wrappedBuilder = new SentryTracingBuilder(builder); | ||
next(wrappedBuilder); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters