Skip to content

Commit 5ba1121

Browse files
Merge branch 'main' into feat/4.0.0
2 parents cf2d206 + 65a7bba commit 5ba1121

13 files changed

+182
-17
lines changed

.github/actions/environment/action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ runs:
3333
maui-android \
3434
${{ runner.os == 'macOS' && 'maui-ios maui-maccatalyst maui-windows' || '' }} \
3535
${{ runner.os == 'Windows' && 'maui-windows' || '' }} \
36-
--temp-dir "${{ runner.temp }}"
36+
--temp-dir "${{ runner.temp }}" \
37+
--skip-sign-check

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ There are some functional differences when publishing Native AOT:
165165
### Fixes
166166

167167
- `CaptureFailedRequests` and `FailedRequestStatusCodes` are now getting respected by the Cocoa SDK. This is relevant for MAUI apps where requests are getting handled natively. ([#2826](https://github.com/getsentry/sentry-dotnet/issues/2826))
168+
- Added `SentryOptions.AutoRegisterTracing` for users who need to control registration of Sentry's tracing middleware ([#2871](https://github.com/getsentry/sentry-dotnet/pull/2871))
168169

169170
### Dependencies
170171

@@ -174,6 +175,9 @@ There are some functional differences when publishing Native AOT:
174175
- Bump CLI from v2.21.2 to v2.21.5 ([#2811](https://github.com/getsentry/sentry-dotnet/pull/2811), [#2834](https://github.com/getsentry/sentry-dotnet/pull/2834), [#2851](https://github.com/getsentry/sentry-dotnet/pull/2851))
175176
- [changelog](https://github.com/getsentry/sentry-cli/blob/master/CHANGELOG.md#2215)
176177
- [diff](https://github.com/getsentry/sentry-cli/compare/2.21.2...2.21.5)
178+
- Bump Java SDK from v6.33.1 to v6.34.0 ([#2874](https://github.com/getsentry/sentry-dotnet/pull/2874))
179+
- [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#6340)
180+
- [diff](https://github.com/getsentry/sentry-java/compare/6.33.1...6.34.0)
177181

178182
## 3.41.0
179183

src/Sentry.AspNetCore/SentryAspNetCoreOptions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Builder;
12
using Microsoft.AspNetCore.Hosting;
23
using Microsoft.AspNetCore.Http;
34
using Sentry.Extensibility;
@@ -65,6 +66,21 @@ public class SentryAspNetCoreOptions : SentryLoggingOptions
6566
/// </remarks>
6667
public bool AdjustStandardEnvironmentNameCasing { get; set; } = true;
6768

69+
/// <summary>
70+
/// <para>
71+
/// When true (by default) Sentry automatically registers its tracing middleware immediately after
72+
/// `EndpointRoutingApplicationBuilderExtensions.UseRouting`.
73+
/// </para>
74+
/// <para>
75+
/// If you need to control when Sentry's tracing middleware is registered, you can set
76+
/// <see cref="AutoRegisterTracing"/> to false and call
77+
/// <see cref="SentryTracingMiddlewareExtensions.UseSentryTracing"/> yourself, sometime after calling
78+
/// `EndpointRoutingApplicationBuilderExtensions.UseRouting` and before calling
79+
/// `EndpointRoutingApplicationBuilderExtensions.UseEndpoints`.
80+
/// </para>
81+
/// </summary>
82+
public bool AutoRegisterTracing { get; set; } = true;
83+
6884
/// <summary>
6985
/// Creates a new instance of <see cref="SentryAspNetCoreOptions"/>.
7086
/// </summary>

src/Sentry.AspNetCore/SentryTracingBuilder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware
4949
{
5050
var options = InnerBuilder.ApplicationServices.GetService<IOptions<SentryAspNetCoreOptions>>();
5151
var instrumenter = options?.Value.Instrumenter ?? Instrumenter.Sentry;
52-
if (instrumenter == Instrumenter.Sentry)
52+
var autoRegisterTracing = options?.Value.AutoRegisterTracing ?? true;
53+
if (instrumenter == Instrumenter.Sentry && autoRegisterTracing)
5354
{
54-
InnerBuilder.Use(middleware).UseSentryTracing();
55+
InnerBuilder.Use(middleware).UseSentryTracingInternal();
5556
return this; // Make sure we return the same builder (not the inner builder), for chaining
5657
}
57-
this.StoreInstrumenter(instrumenter); // Saves us from having to resolve the options to make this check again
58+
this.StoreRegistrationDecision(false); // Saves us from having to resolve the options to make this check again
5859
}
5960

6061
InnerBuilder.Use(middleware);
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
13
using Sentry;
24
using Sentry.AspNetCore;
5+
using Sentry.Extensibility;
36
using Sentry.Internal.Extensions;
47

58
// ReSharper disable once CheckNamespace
@@ -11,32 +14,30 @@ namespace Microsoft.AspNetCore.Builder;
1114
[EditorBrowsable(EditorBrowsableState.Never)]
1215
public static class SentryTracingMiddlewareExtensions
1316
{
17+
internal const string AlreadyRegisteredWarning = "Sentry tracing middleware was already registered. This call to UseSentryTracing is unnecessary.";
1418
private const string UseSentryTracingKey = "__UseSentryTracing";
15-
private const string InstrumenterKey = "__SentryInstrumenter";
19+
private const string ShouldRegisterKey = "__ShouldRegisterSentryTracing";
1620

1721
internal static bool IsSentryTracingRegistered(this IApplicationBuilder builder)
1822
=> builder.Properties.ContainsKey(UseSentryTracingKey);
19-
internal static void StoreInstrumenter(this IApplicationBuilder builder, Instrumenter instrumenter)
20-
=> builder.Properties[InstrumenterKey] = instrumenter;
23+
internal static void StoreRegistrationDecision(this IApplicationBuilder builder, bool shouldRegisterSentryTracing)
24+
=> builder.Properties[ShouldRegisterKey] = shouldRegisterSentryTracing;
2125

2226
internal static bool ShouldRegisterSentryTracing(this IApplicationBuilder builder)
2327
{
2428
if (builder.Properties.ContainsKey(UseSentryTracingKey))
2529
{
30+
// It's already been registered
2631
return false;
2732
}
28-
if (builder.Properties.TryGetTypedValue(InstrumenterKey, out Instrumenter instrumenter))
33+
if (builder.Properties.TryGetTypedValue(ShouldRegisterKey, out bool shouldRegisterSentryTracing))
2934
{
30-
return instrumenter == Instrumenter.Sentry;
35+
return shouldRegisterSentryTracing;
3136
}
3237
return true;
3338
}
3439

35-
/// <summary>
36-
/// Adds Sentry's tracing middleware to the pipeline.
37-
/// Make sure to place this middleware after <c>UseRouting(...)</c>.
38-
/// </summary>
39-
public static IApplicationBuilder UseSentryTracing(this IApplicationBuilder builder)
40+
internal static IApplicationBuilder UseSentryTracingInternal(this IApplicationBuilder builder)
4041
{
4142
// Don't register twice
4243
if (builder.IsSentryTracingRegistered())
@@ -48,4 +49,21 @@ public static IApplicationBuilder UseSentryTracing(this IApplicationBuilder buil
4849
builder.UseMiddleware<SentryTracingMiddleware>();
4950
return builder;
5051
}
52+
53+
/// <summary>
54+
/// Adds Sentry's tracing middleware to the pipeline.
55+
/// Make sure to place this middleware after <c>UseRouting(...)</c>.
56+
/// </summary>
57+
public static IApplicationBuilder UseSentryTracing(this IApplicationBuilder builder)
58+
{
59+
if (!builder.IsSentryTracingRegistered())
60+
{
61+
return builder.UseSentryTracingInternal();
62+
}
63+
// Warn on multiple calls
64+
var log = builder.ApplicationServices.GetService<ILoggerFactory>()
65+
?.CreateLogger<SentryTracingMiddleware>();
66+
log?.LogWarning(AlreadyRegisteredWarning);
67+
return builder;
68+
}
5169
}

src/Sentry.Bindings.Android/Sentry.Bindings.Android.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>net7.0-android</TargetFramework>
44
<!-- BG8605 and BG8606 happen because there's a missing androidx.lifecycle dependency, but we don't need it here. (The native Android Sentry SDK will use it if it exists.) -->
55
<NoWarn>$(NoWarn);BG8605;BG8606</NoWarn>
6-
<SentryAndroidSdkVersion>6.33.1</SentryAndroidSdkVersion>
6+
<SentryAndroidSdkVersion>6.34.0</SentryAndroidSdkVersion>
77
<SentryAndroidSdkDirectory>$(BaseIntermediateOutputPath)sdks\Sentry\Android\$(SentryAndroidSdkVersion)\</SentryAndroidSdkDirectory>
88
<Description>.NET Bindings for the Sentry Android SDK</Description>
99
</PropertyGroup>

src/Sentry/Internal/Hub.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ public SentryId CaptureEvent(SentryEvent evt, Scope? scope = null, Hint? hint =
421421
actualScope.LastEventId = id;
422422
actualScope.SessionUpdate = null;
423423

424-
if (evt.HasTerminalException())
424+
if (evt.HasTerminalException() && actualScope.Transaction is { } transaction)
425425
{
426426
// Event contains a terminal exception -> finish any current transaction as aborted
427427
// Do this *after* the event was captured, so that the event is still linked to the transaction.
428428
_options.LogDebug("Ending transaction as Aborted, due to unhandled exception.");
429-
actualScope.Transaction?.Finish(SpanStatus.Aborted);
429+
transaction.Finish(SpanStatus.Aborted);
430430
}
431431

432432
return id;

test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace Sentry.AspNetCore
5353
{
5454
public SentryAspNetCoreOptions() { }
5555
public bool AdjustStandardEnvironmentNameCasing { get; set; }
56+
public bool AutoRegisterTracing { get; set; }
5657
public bool FlushOnCompletedRequest { get; set; }
5758
public bool IncludeActivityData { get; set; }
5859
public Sentry.Extensibility.RequestSize MaxRequestBodySize { get; set; }

test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace Sentry.AspNetCore
5353
{
5454
public SentryAspNetCoreOptions() { }
5555
public bool AdjustStandardEnvironmentNameCasing { get; set; }
56+
public bool AutoRegisterTracing { get; set; }
5657
public bool FlushOnCompletedRequest { get; set; }
5758
public bool IncludeActivityData { get; set; }
5859
public Sentry.Extensibility.RequestSize MaxRequestBodySize { get; set; }

test/Sentry.AspNetCore.Tests/ApiApprovalTests.Run.DotNet8_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace Sentry.AspNetCore
5353
{
5454
public SentryAspNetCoreOptions() { }
5555
public bool AdjustStandardEnvironmentNameCasing { get; set; }
56+
public bool AutoRegisterTracing { get; set; }
5657
public bool FlushOnCompletedRequest { get; set; }
5758
public bool IncludeActivityData { get; set; }
5859
public Sentry.Extensibility.RequestSize MaxRequestBodySize { get; set; }

0 commit comments

Comments
 (0)