Skip to content

Commit 0a7d341

Browse files
committed
Merge branch 'feat/ignore404-filter' into feat/ignore404-default
# Conflicts: # sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryAutoConfigurationTest.kt # sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt # sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt
2 parents becc6d0 + 8eb7843 commit 0a7d341

15 files changed

Lines changed: 144 additions & 13 deletions

File tree

‎sentry-spring-7/src/main/java/io/sentry/spring7/tracing/SentryTracingFilter.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ private void doFilterWithTransaction(
140140
final @Nullable ITransaction transaction =
141141
getOrStartTransaction(httpRequest, transactionContext);
142142

143+
boolean filterChainCompleted = false;
143144
try {
144145
filterChain.doFilter(httpRequest, httpResponse);
146+
filterChainCompleted = true;
145147
} catch (Throwable e) {
146148
if (transaction != null) {
147149
// exceptions that are not handled by Spring
@@ -165,7 +167,10 @@ private void doFilterWithTransaction(
165167
if (transactionName != null) {
166168
transaction.setName(transactionName, transactionNameSource);
167169
transaction.setOperation(TRANSACTION_OP);
168-
transaction.setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, httpResponse.getStatus());
170+
// An escaping exception may be mapped to an HTTP status later by the container.
171+
if (filterChainCompleted) {
172+
transaction.setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, httpResponse.getStatus());
173+
}
169174
// if exception has been thrown, transaction status is already set to INTERNAL_ERROR, and
170175
// httpResponse.getStatus() returns 200.
171176
if (transaction.getStatus() == null) {

‎sentry-spring-7/src/test/kotlin/io/sentry/spring7/tracing/SentryTracingFilterTest.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ class SentryTracingFilterTest {
276276
} catch (_: Exception) {}
277277
verify(fixture.scopes)
278278
.captureTransaction(
279-
check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) },
279+
check {
280+
assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR)
281+
assertThat(it.contexts.trace!!.data)
282+
.doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY)
283+
},
280284
anyOrNull<TraceContext>(),
281285
anyOrNull(),
282286
anyOrNull(),

‎sentry-spring-boot-4/src/integrationTest/kotlin/io/sentry/spring/boot4/integrationtest/PersonIntegrationTest.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class PersonIntegrationTest : IntegrationTest() {
4848
.transaction()!!
4949
assertThat(transaction.contexts.trace!!.data[SpanDataConvention.SENTRY_KIND_KEY])
5050
.isEqualTo("server")
51-
assertThat(transaction.contexts.trace!!.data[SpanDataConvention.HTTP_STATUS_CODE_KEY])
52-
.isEqualTo(200)
51+
assertThat(transaction.contexts.trace!!.data)
52+
.doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY)
5353
assertThat(
5454
transaction.hasOp(
5555
"http.server",

‎sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryAutoConfigurationTest.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import org.springframework.context.annotation.Bean
8585
import org.springframework.context.annotation.Configuration
8686
import org.springframework.core.Ordered
8787
import org.springframework.core.annotation.Order
88+
import org.springframework.core.env.MapPropertySource
8889
import org.springframework.scheduling.annotation.EnableScheduling
8990
import org.springframework.scheduling.annotation.Scheduled
9091
import org.springframework.scheduling.quartz.SchedulerFactoryBean
@@ -350,6 +351,30 @@ class SentryAutoConfigurationTest {
350351
}
351352
}
352353

354+
@Test
355+
fun `YAML list binds integer ignored server HTTP status codes`() {
356+
dsnEnabledRunner
357+
.withInitializer { context ->
358+
// YAML exposes unquoted numbers as Integer values in indexed property sources.
359+
context.environment.propertySources.addFirst(
360+
MapPropertySource(
361+
"yaml",
362+
mapOf<String, Any>(
363+
"sentry.trace-ignore-status-codes[0]" to 404,
364+
"sentry.trace-ignore-status-codes[1]" to "500-599",
365+
),
366+
)
367+
)
368+
}
369+
.run {
370+
val ranges = it.getBean(SentryProperties::class.java).traceIgnoreStatusCodes
371+
assertThat(ranges).hasSize(2)
372+
assertThat(ranges!![0].isInRange(404)).isTrue()
373+
assertThat(ranges[1].isInRange(500)).isTrue()
374+
assertThat(ranges[1].isInRange(599)).isTrue()
375+
}
376+
}
377+
353378
@Test
354379
fun `empty ignored server HTTP status codes disable the configuration`() {
355380
contextRunner

‎sentry-spring-boot-jakarta/src/integrationTest/kotlin/io/sentry/spring/boot/jakarta/integrationtest/PersonIntegrationTest.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class PersonIntegrationTest : IntegrationTest() {
4848
.transaction()!!
4949
assertThat(transaction.contexts.trace!!.data[SpanDataConvention.SENTRY_KIND_KEY])
5050
.isEqualTo("server")
51-
assertThat(transaction.contexts.trace!!.data[SpanDataConvention.HTTP_STATUS_CODE_KEY])
52-
.isEqualTo(200)
51+
assertThat(transaction.contexts.trace!!.data)
52+
.doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY)
5353
assertThat(
5454
transaction.hasOp(
5555
"http.server",

‎sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import org.springframework.context.annotation.Bean
8888
import org.springframework.context.annotation.Configuration
8989
import org.springframework.core.Ordered
9090
import org.springframework.core.annotation.Order
91+
import org.springframework.core.env.MapPropertySource
9192
import org.springframework.scheduling.annotation.EnableScheduling
9293
import org.springframework.scheduling.annotation.Scheduled
9394
import org.springframework.scheduling.quartz.SchedulerFactoryBean
@@ -358,6 +359,30 @@ class SentryAutoConfigurationTest {
358359
}
359360
}
360361

362+
@Test
363+
fun `YAML list binds integer ignored server HTTP status codes`() {
364+
dsnEnabledRunner
365+
.withInitializer { context ->
366+
// YAML exposes unquoted numbers as Integer values in indexed property sources.
367+
context.environment.propertySources.addFirst(
368+
MapPropertySource(
369+
"yaml",
370+
mapOf<String, Any>(
371+
"sentry.trace-ignore-status-codes[0]" to 404,
372+
"sentry.trace-ignore-status-codes[1]" to "500-599",
373+
),
374+
)
375+
)
376+
}
377+
.run {
378+
val ranges = it.getBean(SentryProperties::class.java).traceIgnoreStatusCodes
379+
assertThat(ranges).hasSize(2)
380+
assertThat(ranges!![0].isInRange(404)).isTrue()
381+
assertThat(ranges[1].isInRange(500)).isTrue()
382+
assertThat(ranges[1].isInRange(599)).isTrue()
383+
}
384+
}
385+
361386
@Test
362387
fun `empty ignored server HTTP status codes disable the configuration`() {
363388
contextRunner

‎sentry-spring-boot/src/integrationTest/kotlin/io/sentry/spring/boot/integrationtest/PersonIntegrationTest.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class PersonIntegrationTest : IntegrationTest() {
4848
.transaction()!!
4949
assertThat(transaction.contexts.trace!!.data[SpanDataConvention.SENTRY_KIND_KEY])
5050
.isEqualTo("server")
51-
assertThat(transaction.contexts.trace!!.data[SpanDataConvention.HTTP_STATUS_CODE_KEY])
52-
.isEqualTo(200)
51+
assertThat(transaction.contexts.trace!!.data)
52+
.doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY)
5353
assertThat(
5454
transaction.hasOp(
5555
"http.server",

‎sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import org.springframework.context.annotation.Bean
8888
import org.springframework.context.annotation.Configuration
8989
import org.springframework.core.Ordered
9090
import org.springframework.core.annotation.Order
91+
import org.springframework.core.env.MapPropertySource
9192
import org.springframework.scheduling.annotation.EnableScheduling
9293
import org.springframework.scheduling.annotation.Scheduled
9394
import org.springframework.scheduling.quartz.SchedulerFactoryBean
@@ -356,6 +357,30 @@ class SentryAutoConfigurationTest {
356357
}
357358
}
358359

360+
@Test
361+
fun `YAML list binds integer ignored server HTTP status codes`() {
362+
dsnEnabledRunner
363+
.withInitializer { context ->
364+
// YAML exposes unquoted numbers as Integer values in indexed property sources.
365+
context.environment.propertySources.addFirst(
366+
MapPropertySource(
367+
"yaml",
368+
mapOf<String, Any>(
369+
"sentry.trace-ignore-status-codes[0]" to 404,
370+
"sentry.trace-ignore-status-codes[1]" to "500-599",
371+
),
372+
)
373+
)
374+
}
375+
.run {
376+
val ranges = it.getBean(SentryProperties::class.java).traceIgnoreStatusCodes
377+
assertThat(ranges).hasSize(2)
378+
assertThat(ranges!![0].isInRange(404)).isTrue()
379+
assertThat(ranges[1].isInRange(500)).isTrue()
380+
assertThat(ranges[1].isInRange(599)).isTrue()
381+
}
382+
}
383+
359384
@Test
360385
fun `empty ignored server HTTP status codes disable the configuration`() {
361386
contextRunner

‎sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/tracing/SentryTracingFilter.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ private void doFilterWithTransaction(
140140
final @Nullable ITransaction transaction =
141141
getOrStartTransaction(httpRequest, transactionContext);
142142

143+
boolean filterChainCompleted = false;
143144
try {
144145
filterChain.doFilter(httpRequest, httpResponse);
146+
filterChainCompleted = true;
145147
} catch (Throwable e) {
146148
if (transaction != null) {
147149
// exceptions that are not handled by Spring
@@ -165,7 +167,10 @@ private void doFilterWithTransaction(
165167
if (transactionName != null) {
166168
transaction.setName(transactionName, transactionNameSource);
167169
transaction.setOperation(TRANSACTION_OP);
168-
transaction.setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, httpResponse.getStatus());
170+
// An escaping exception may be mapped to an HTTP status later by the container.
171+
if (filterChainCompleted) {
172+
transaction.setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, httpResponse.getStatus());
173+
}
169174
// if exception has been thrown, transaction status is already set to INTERNAL_ERROR, and
170175
// httpResponse.getStatus() returns 200.
171176
if (transaction.getStatus() == null) {

‎sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/tracing/SentryTracingFilterTest.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ class SentryTracingFilterTest {
276276
} catch (_: Exception) {}
277277
verify(fixture.scopes)
278278
.captureTransaction(
279-
check { assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR) },
279+
check {
280+
assertThat(it.status).isEqualTo(SpanStatus.INTERNAL_ERROR)
281+
assertThat(it.contexts.trace!!.data)
282+
.doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY)
283+
},
280284
anyOrNull<TraceContext>(),
281285
anyOrNull(),
282286
anyOrNull(),

0 commit comments

Comments
 (0)