Skip to content

Commit 2c60e69

Browse files
feat!: don't trace 301, 305-399 or 401-404 responses by default (#5632)
1 parent 15b78ac commit 2c60e69

4 files changed

Lines changed: 130 additions & 12 deletions

File tree

‎src/Sentry/SentryOptions.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,9 +949,15 @@ public IDiagnosticLogger? DiagnosticLogger
949949

950950
/// <summary>
951951
/// <para>Transactions will be dropped if the HTTP Response status code matches any of the configured ranges.</para>
952-
/// <para>Defaults to an empty collection (all transactions are captured regardless of status code).</para>
952+
/// <para>Defaults to 301, 305-399 and 401-404. 302 and 303 are traced, as they commonly redirect after a successful form POST.</para>
953+
/// <para>Clear the collection to capture transactions regardless of status code.</para>
953954
/// </summary>
954-
public IList<HttpStatusCodeRange> TraceIgnoreStatusCodes { get; set; } = [];
955+
public IList<HttpStatusCodeRange> TraceIgnoreStatusCodes { get; set; } = new List<HttpStatusCodeRange>
956+
{
957+
301,
958+
(305, 399),
959+
(401, 404)
960+
};
955961

956962
// The default failed request target list will match anything, but adding to the list should clear that.
957963
private Lazy<IList<StringOrRegex>> _failedRequestTargets = new(() =>

‎test/Sentry.AspNetCore.Tests/IntegrationMockedBackgroundWorker.cs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,42 @@ public async Task Environment_BothOnOptionsAndEnvVar_ValueFromOption()
338338
.Environment == expected
339339
));
340340
}
341+
342+
[Fact]
343+
public async Task NotFoundRequest_ByDefault_NoTransactionCaptured()
344+
{
345+
Configure = o => o.TracesSampleRate = 1;
346+
ConfigureApp = app => app.UseSentryTracing();
347+
348+
Build();
349+
_ = await HttpClient.GetAsync("/");
350+
_ = await HttpClient.GetAsync("/missing");
351+
352+
_ = Worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(e => HasTransaction(e, "GET /")));
353+
_ = Worker.DidNotReceive().EnqueueEnvelope(Arg.Is<Envelope>(e => HasTransaction(e, "GET /missing")));
354+
}
355+
356+
[Fact]
357+
public async Task NotFoundRequest_TraceIgnoreStatusCodesCleared_TransactionCaptured()
358+
{
359+
Configure = o =>
360+
{
361+
o.TracesSampleRate = 1;
362+
o.TraceIgnoreStatusCodes.Clear();
363+
};
364+
ConfigureApp = app => app.UseSentryTracing();
365+
366+
Build();
367+
_ = await HttpClient.GetAsync("/missing");
368+
369+
_ = Worker.Received(1).EnqueueEnvelope(Arg.Is<Envelope>(e => HasTransaction(e, "GET /missing")));
370+
}
371+
372+
private static bool HasTransaction(Envelope envelope, string name) =>
373+
envelope.Items
374+
.Select(i => i.Payload)
375+
.OfType<JsonSerializable>()
376+
.Select(i => i.Source)
377+
.OfType<SentryTransaction>()
378+
.Any(t => t.Name == name);
341379
}

‎test/Sentry.Tests/BindableSentryOptionsTests.cs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,42 @@ public void ApplyTo_SetsOptionsFromConfig()
2626
// Assert
2727
AssertContainsExpectedPropertyValues(actual);
2828
}
29+
30+
[Fact]
31+
public void ApplyTo_TraceIgnoreStatusCodesNotConfigured_KeepsDefault()
32+
{
33+
// Arrange
34+
var actual = new SentryOptions();
35+
var bindable = new BindableSentryOptions();
36+
37+
// Act
38+
new ConfigurationBuilder().Build().Bind(bindable);
39+
bindable.ApplyTo(actual);
40+
41+
// Assert
42+
actual.TraceIgnoreStatusCodes.Should().Equal(new SentryOptions().TraceIgnoreStatusCodes);
43+
}
44+
45+
[Fact]
46+
public void ApplyTo_TraceIgnoreStatusCodesConfigured_ReplacesDefault()
47+
{
48+
// Arrange
49+
var actual = new SentryOptions();
50+
var bindable = new BindableSentryOptions();
51+
var config = new ConfigurationBuilder()
52+
.AddInMemoryCollection(new Dictionary<string, string>
53+
{
54+
["TraceIgnoreStatusCodes:0"] = "404",
55+
["TraceIgnoreStatusCodes:1"] = "500"
56+
})
57+
.Build();
58+
59+
// Act
60+
config.Bind(bindable);
61+
bindable.ApplyTo(actual);
62+
63+
// Assert
64+
actual.TraceIgnoreStatusCodes.Should().Equal(new HttpStatusCodeRange(404), new HttpStatusCodeRange(500));
65+
}
2966
}
3067
#endif

‎test/Sentry.Tests/TraceIgnoreStatusCodeTransactionProcessorTests.cs‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ namespace Sentry.Tests;
44

55
public class TraceIgnoreStatusCodeTransactionProcessorTests
66
{
7-
private static SentryOptions OptionsWithIgnoredCodes(params HttpStatusCodeRange[] ranges)
8-
{
9-
var options = new SentryOptions();
10-
foreach (var range in ranges)
11-
{
12-
options.TraceIgnoreStatusCodes.Add(range);
13-
}
14-
return options;
15-
}
7+
private static SentryOptions OptionsWithIgnoredCodes(params HttpStatusCodeRange[] ranges) =>
8+
new() { TraceIgnoreStatusCodes = [.. ranges] };
169

1710
private static SentryTransaction TransactionWithStatusCode(int statusCode)
1811
{
@@ -21,11 +14,55 @@ private static SentryTransaction TransactionWithStatusCode(int statusCode)
2114
return transaction;
2215
}
2316

17+
[Theory]
18+
[InlineData(301)]
19+
[InlineData(305)]
20+
[InlineData(307)]
21+
[InlineData(308)]
22+
[InlineData(399)]
23+
[InlineData(401)]
24+
[InlineData(403)]
25+
[InlineData(404)]
26+
public void Process_DefaultOptions_DropsIgnoredStatusCode(int statusCode)
27+
{
28+
// Arrange
29+
var processor = new TraceIgnoreStatusCodeTransactionProcessor(new SentryOptions());
30+
var transaction = TransactionWithStatusCode(statusCode);
31+
32+
// Act
33+
var result = processor.Process(transaction);
34+
35+
// Assert
36+
result.Should().BeNull();
37+
}
38+
39+
[Theory]
40+
[InlineData(200)]
41+
[InlineData(300)]
42+
[InlineData(302)]
43+
[InlineData(303)]
44+
[InlineData(304)]
45+
[InlineData(400)]
46+
[InlineData(405)]
47+
[InlineData(500)]
48+
public void Process_DefaultOptions_ReturnsTransaction(int statusCode)
49+
{
50+
// Arrange
51+
var processor = new TraceIgnoreStatusCodeTransactionProcessor(new SentryOptions());
52+
var transaction = TransactionWithStatusCode(statusCode);
53+
54+
// Act
55+
var result = processor.Process(transaction);
56+
57+
// Assert
58+
result.Should().BeSameAs(transaction);
59+
}
60+
2461
[Fact]
2562
public void Process_EmptyIgnoreList_ReturnsTransaction()
2663
{
2764
// Arrange
28-
var options = new SentryOptions();
65+
var options = OptionsWithIgnoredCodes();
2966
var processor = new TraceIgnoreStatusCodeTransactionProcessor(options);
3067
var transaction = TransactionWithStatusCode(404);
3168

0 commit comments

Comments
 (0)