-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentryTracingMiddlewareExtensions.cs
67 lines (60 loc) · 2.48 KB
/
SentryTracingMiddlewareExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Sentry.AspNetCore;
using Sentry.Internal.Extensions;
// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Builder;
/// <summary>
/// Extensions for enabling <see cref="SentryTracingMiddleware"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class SentryTracingMiddlewareExtensions
{
internal const string AlreadyRegisteredWarning = "Sentry tracing middleware was already registered. This call to UseSentryTracing is unnecessary.";
private const string UseSentryTracingKey = "__UseSentryTracing";
private const string ShouldRegisterKey = "__ShouldRegisterSentryTracing";
internal static bool IsSentryTracingRegistered(this IApplicationBuilder builder)
=> builder.Properties.ContainsKey(UseSentryTracingKey);
internal static void StoreRegistrationDecision(this IApplicationBuilder builder, bool shouldRegisterSentryTracing)
=> builder.Properties[ShouldRegisterKey] = shouldRegisterSentryTracing;
internal static bool ShouldRegisterSentryTracing(this IApplicationBuilder builder)
{
if (builder.Properties.ContainsKey(UseSentryTracingKey))
{
// It's already been registered
return false;
}
if (builder.Properties.TryGetTypedValue(ShouldRegisterKey, out bool shouldRegisterSentryTracing))
{
return shouldRegisterSentryTracing;
}
return true;
}
internal static IApplicationBuilder UseSentryTracingInternal(this IApplicationBuilder builder)
{
// Don't register twice
if (builder.IsSentryTracingRegistered())
{
return builder;
}
builder.Properties[UseSentryTracingKey] = true;
builder.UseMiddleware<SentryTracingMiddleware>();
return builder;
}
/// <summary>
/// Adds Sentry's tracing middleware to the pipeline.
/// Make sure to place this middleware after <c>UseRouting(...)</c>.
/// </summary>
public static IApplicationBuilder UseSentryTracing(this IApplicationBuilder builder)
{
if (!builder.IsSentryTracingRegistered())
{
return builder.UseSentryTracingInternal();
}
// Warn on multiple calls
var log = builder.ApplicationServices.GetService<ILoggerFactory>()
?.CreateLogger<SentryTracingMiddleware>();
log?.LogWarning(AlreadyRegisteredWarning);
return builder;
}
}