From f0a46f66f5bd56134d8b8fba08495461cba2999c Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Wed, 18 Nov 2026 11:56:30 +0100 Subject: [PATCH] fix: Rethrow fatal errors at the JVM integration boundaries The broad catches in the Apollo, Kafka, Logback, Quartz, Spotlight and Spring integrations swallowed every Throwable, including OutOfMemoryError and StackOverflowError. Each now calls ExceptionUtils.rethrowIfFatal first and says why it is broad and what happens on failure. None of them needs a LinkageError arm: the calls into the framework outside these catches need the same APIs, so a mismatched version fails before them. The user-facing callbacks and options these catches wrap say how a failure shows up: Apollo's BeforeSpanCallback, SentryAppender's encoder, and the slug of @SentryCheckIn. Refs JAVA-552 Co-Authored-By: Claude Opus 5.5 --- .../apollo3/SentryApollo3HttpInterceptor.kt | 14 +++++++++++++ .../apollo3/SentryApollo3InterceptorTest.kt | 21 +++++++++++++++++++ .../apollo4/SentryApollo4HttpInterceptor.kt | 14 +++++++++++++ .../SentryApollo4HttpInterceptorTest.kt | 21 +++++++++++++++++++ .../kafka/SentryKafkaConsumerTracing.java | 10 +++++++++ .../io/sentry/kafka/SentryKafkaProducer.java | 8 +++++++ .../io/sentry/logback/SentryAppender.java | 16 ++++++++++++-- .../io/sentry/logback/SentryAppenderTest.kt | 17 +++++++++++++++ .../io/sentry/quartz/SentryJobListener.java | 7 +++++++ .../spotlight/SpotlightIntegration.java | 4 ++++ .../sentry/spring7/SentryRequestResolver.java | 4 ++++ .../io/sentry/spring7/SentrySpringFilter.java | 4 ++++ .../sentry/spring7/checkin/SentryCheckIn.java | 5 +++++ .../spring7/checkin/SentryCheckInAdvice.java | 4 ++++ .../kafka/SentryKafkaRecordInterceptor.java | 4 ++++ .../spring/jakarta/SentryRequestResolver.java | 4 ++++ .../spring/jakarta/SentrySpringFilter.java | 4 ++++ .../spring/jakarta/checkin/SentryCheckIn.java | 5 +++++ .../jakarta/checkin/SentryCheckInAdvice.java | 4 ++++ .../kafka/SentryKafkaRecordInterceptor.java | 4 ++++ .../sentry/spring/SentryRequestResolver.java | 4 ++++ .../io/sentry/spring/SentrySpringFilter.java | 4 ++++ .../sentry/spring/checkin/SentryCheckIn.java | 5 +++++ .../spring/checkin/SentryCheckInAdvice.java | 4 ++++ .../kafka/SentryKafkaRecordInterceptor.java | 4 ++++ 25 files changed, 193 insertions(+), 2 deletions(-) diff --git a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt index 400643cef4a..3cbf323e712 100644 --- a/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt +++ b/sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt @@ -30,6 +30,7 @@ import io.sentry.protocol.Mechanism import io.sentry.protocol.Request import io.sentry.protocol.Response import io.sentry.util.CookieUtils +import io.sentry.util.ExceptionUtils import io.sentry.util.GraphqlUtils import io.sentry.util.HttpUtils import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion @@ -229,6 +230,9 @@ constructor( span.spanContext.sampled = false } } catch (e: Throwable) { + // beforeSpan is the user's callback, so we can't know what it throws. The span is + // dropped, as if the callback had returned null. + ExceptionUtils.rethrowIfFatal(e) span.spanContext.sampled = false if (wasSampled) { scopes.options.clientReportRecorder.recordLostEvent( @@ -434,6 +438,9 @@ constructor( it.writeTo(buffer) data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options) } catch (e: Throwable) { + // writeTo runs the app's own request body code, so we can't know what it throws. + // The event is sent without the request body. + ExceptionUtils.rethrowIfFatal(e) scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e) // continue because the response body alone can already give some insights } finally { @@ -466,6 +473,9 @@ constructor( scopes.captureEvent(event, hint) } catch (e: Throwable) { + // This runs inside the app's GraphQL request, which must not fail because reporting it + // failed. The failed-request event is not sent. + ExceptionUtils.rethrowIfFatal(e) scopes.options.logger.log(SentryLevel.ERROR, "Error capturing the GraphQL error.", e) } } @@ -475,6 +485,10 @@ constructor( /** * Mutates span before being added. * + * If this throws, the span is dropped, as if this had returned null. Fatal errors such as + * [OutOfMemoryError] are rethrown instead. The failure is only logged at ERROR level through + * [io.sentry.SentryOptions.getLogger], which by default prints only when debug is enabled. + * * @param span the span to mutate or drop * @param request the Apollo request object * @param response the Apollo response object diff --git a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt index 7b9454f394a..f18bc20a8ad 100644 --- a/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt +++ b/sentry-apollo-3/src/test/java/io/sentry/apollo3/SentryApollo3InterceptorTest.kt @@ -484,6 +484,27 @@ class SentryApollo3InterceptorTest { ) } + @Test + fun `when beforeSpan throws a fatal error, rethrows it`(): Unit = runBlocking { + val tx = + SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.span).thenReturn(tx) + val chain = + object : HttpInterceptorChain { + override suspend fun proceed(request: HttpRequest): HttpResponse = + HttpResponse.Builder(200).build() + } + val request = HttpRequest.Builder(HttpMethod.Post, "https://example.com/graphql").build() + val interceptor = + SentryApollo3HttpInterceptor( + fixture.scopes, + { _, _, _ -> throw OutOfMemoryError("callback") }, + captureFailedRequests = false, + ) + + assertFailsWith { interceptor.intercept(request, chain) } + } + @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery(fixture.getSut()) diff --git a/sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApollo4HttpInterceptor.kt b/sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApollo4HttpInterceptor.kt index 539bdb25b21..6610f331c46 100644 --- a/sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApollo4HttpInterceptor.kt +++ b/sentry-apollo-4/src/main/java/io/sentry/apollo4/SentryApollo4HttpInterceptor.kt @@ -28,6 +28,7 @@ import io.sentry.protocol.Mechanism import io.sentry.protocol.Request import io.sentry.protocol.Response import io.sentry.util.CookieUtils +import io.sentry.util.ExceptionUtils import io.sentry.util.GraphqlUtils import io.sentry.util.HttpUtils import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion @@ -228,6 +229,9 @@ constructor( span.spanContext.sampled = false } } catch (e: Throwable) { + // beforeSpan is the user's callback, so we can't know what it throws. The span is + // dropped, as if the callback had returned null. + ExceptionUtils.rethrowIfFatal(e) span.spanContext.sampled = false if (wasSampled) { scopes.options.clientReportRecorder.recordLostEvent( @@ -433,6 +437,9 @@ constructor( it.writeTo(buffer) data = GraphqlUtils.filterRequestBody(buffer.readUtf8(), scopes.options) } catch (e: Throwable) { + // writeTo runs the app's own request body code, so we can't know what it throws. + // The event is sent without the request body. + ExceptionUtils.rethrowIfFatal(e) scopes.options.logger.log(SentryLevel.ERROR, "Error reading the request body.", e) // continue because the response body alone can already give some insights } finally { @@ -465,6 +472,9 @@ constructor( scopes.captureEvent(event, hint) } catch (e: Throwable) { + // This runs inside the app's GraphQL request, which must not fail because reporting it + // failed. The failed-request event is not sent. + ExceptionUtils.rethrowIfFatal(e) scopes.options.logger.log(SentryLevel.ERROR, "Error capturing the GraphQL error.", e) } } @@ -474,6 +484,10 @@ constructor( /** * Mutates span before being added. * + * If this throws, the span is dropped, as if this had returned null. Fatal errors such as + * [OutOfMemoryError] are rethrown instead. The failure is only logged at ERROR level through + * [io.sentry.SentryOptions.getLogger], which by default prints only when debug is enabled. + * * @param span the span to mutate or drop * @param request the Apollo request object * @param response the Apollo response object diff --git a/sentry-apollo-4/src/test/java/io/sentry/apollo4/SentryApollo4HttpInterceptorTest.kt b/sentry-apollo-4/src/test/java/io/sentry/apollo4/SentryApollo4HttpInterceptorTest.kt index cb78ddcbd05..b32c822981a 100644 --- a/sentry-apollo-4/src/test/java/io/sentry/apollo4/SentryApollo4HttpInterceptorTest.kt +++ b/sentry-apollo-4/src/test/java/io/sentry/apollo4/SentryApollo4HttpInterceptorTest.kt @@ -498,6 +498,27 @@ abstract class SentryApollo4HttpInterceptorTest( ) } + @Test + fun `when beforeSpan throws a fatal error, rethrows it`(): Unit = runBlocking { + val tx = + SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.scopes) + whenever(fixture.scopes.span).thenReturn(tx) + val chain = + object : HttpInterceptorChain { + override suspend fun proceed(request: HttpRequest): HttpResponse = + HttpResponse.Builder(200).build() + } + val request = HttpRequest.Builder(HttpMethod.Post, "https://example.com/graphql").build() + val interceptor = + SentryApollo4HttpInterceptor( + fixture.scopes, + { _, _, _ -> throw OutOfMemoryError("callback") }, + captureFailedRequests = false, + ) + + assertFailsWith { interceptor.intercept(request, chain) } + } + @Test fun `adds breadcrumb when http calls succeeds`() { executeQuery(fixture.getSut()) diff --git a/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaConsumerTracing.java b/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaConsumerTracing.java index 15e97b3663d..0c69104284d 100644 --- a/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaConsumerTracing.java +++ b/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaConsumerTracing.java @@ -12,6 +12,7 @@ import io.sentry.SpanStatus; import io.sentry.TransactionContext; import io.sentry.TransactionOptions; +import io.sentry.util.ExceptionUtils; import io.sentry.util.SpanUtils; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -85,6 +86,9 @@ U withTracingImpl( forkedScopes = scopes.forkedRootScopes(CREATOR); lifecycleToken = forkedScopes.makeCurrent(); } catch (Throwable t) { + // Tracing must never stop the app from processing the record. The record is processed + // without a transaction. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() @@ -181,6 +185,9 @@ private boolean isIgnored() { return transaction; } catch (Throwable t) { + // This parses headers written by whichever producer sent the record, and tracing must never + // stop the app from processing it. The record is processed without a transaction. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() @@ -204,6 +211,9 @@ private void finishTransaction( } transaction.finish(); } catch (Throwable t) { + // finish() runs the event processors and beforeSendTransaction, which can be the app's own + // code, and the record's processing result must stand. The transaction is not sent. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() diff --git a/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaProducer.java b/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaProducer.java index 4ea3853977e..3e3050c9d57 100644 --- a/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaProducer.java +++ b/sentry-kafka/src/main/java/io/sentry/kafka/SentryKafkaProducer.java @@ -10,6 +10,7 @@ import io.sentry.SpanDataConvention; import io.sentry.SpanOptions; import io.sentry.SpanStatus; +import io.sentry.util.ExceptionUtils; import io.sentry.util.SpanUtils; import io.sentry.util.TracingUtils; import java.lang.reflect.InvocationHandler; @@ -186,6 +187,9 @@ static final class SentryProducerHandler implements InvocationHandler { span.setStatus(SpanStatus.OK); } } catch (Throwable t) { + // This runs on Kafka's I/O thread for every send, and a tracing failure must not reach + // the producer. The span finishes without a status, and the app's callback still runs. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() @@ -242,6 +246,10 @@ private void maybeInjectHeaders(final @NotNull Headers headers, final @Nullable .toString() .getBytes(StandardCharsets.UTF_8)); } catch (Throwable t) { + // The record and its headers come from the app, e.g. read-only headers of a record that + // was already sent, so we can't know what they throw. The record is sent without Sentry + // headers. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() diff --git a/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java b/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java index 127ca7aa707..f52110ffdc5 100644 --- a/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java +++ b/sentry-logback/src/main/java/io/sentry/logback/SentryAppender.java @@ -29,6 +29,7 @@ import io.sentry.protocol.Message; import io.sentry.protocol.SdkVersion; import io.sentry.util.CollectionUtils; +import io.sentry.util.ExceptionUtils; import io.sentry.util.LoggerPropertiesUtil; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -213,8 +214,9 @@ private String formatted(@NotNull ILoggingEvent loggingEvent) { try { return new String(encoder.encode(loggingEvent), StandardCharsets.UTF_8); } catch (final Throwable t) { - // catch exceptions from possibly incorrectly configured encoder - // and fallback to default formatted message + // The encoder is the app's own, possibly misconfigured, so we can't know what it throws. + // The event falls back to the default formatted message. + ExceptionUtils.rethrowIfFatal(t); addWarn("Failed to encode logging event", t); } } @@ -347,6 +349,16 @@ void setTransportFactory(final @Nullable ITransportFactory transportFactory) { this.transportFactory = transportFactory; } + /** + * Sets the encoder that formats the message sent to Sentry. + * + *

If the encoder throws, the logging event's formatted message is sent instead. Fatal errors + * such as {@link OutOfMemoryError} are rethrown. The failure is reported as a warning to + * logback's status manager, which you can see with {@code } or a + * status listener. + * + * @param encoder the encoder + */ public void setEncoder(Encoder encoder) { this.encoder = encoder; } diff --git a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt index a5ecb2b30cd..04b3a97da14 100644 --- a/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt +++ b/sentry-logback/src/test/kotlin/io/sentry/logback/SentryAppenderTest.kt @@ -32,6 +32,7 @@ import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertFalse import kotlin.test.assertNotNull import kotlin.test.assertNull @@ -329,6 +330,22 @@ class SentryAppenderTest { ) } + class FatalEncoder : EncoderBase() { + override fun headerBytes(): ByteArray = ByteArray(0) + + override fun footerBytes(): ByteArray = ByteArray(0) + + override fun encode(event: ILoggingEvent?): ByteArray = throw OutOfMemoryError("encoder") + } + + @Test + fun `rethrows a fatal error from the encoder`() { + fixture = + Fixture(minimumEventLevel = Level.DEBUG, encoder = FatalEncoder(), sendDefaultPii = true) + + assertFailsWith { fixture.logger.info("testing a fatal encoder error") } + } + @Test fun `event date is in UTC`() { fixture = Fixture(minimumEventLevel = Level.DEBUG) diff --git a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java index 5d48cbbaf87..f28b6c735f8 100644 --- a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java +++ b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java @@ -10,6 +10,7 @@ import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; +import io.sentry.util.ExceptionUtils; import io.sentry.util.LifecycleHelper; import io.sentry.util.MonitorConfigUtils; import io.sentry.util.Objects; @@ -92,6 +93,9 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) { context.put(SENTRY_CHECK_IN_ID_KEY, checkInId); context.put(SENTRY_SLUG_KEY, slug); } catch (Throwable t) { + // This reads the app's job data and trigger, and Quartz doesn't run the job when a listener + // throws, so we can't let anything escape. The job runs without a check-in. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() @@ -281,6 +285,9 @@ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jo scopes.captureCheckIn(new CheckIn(checkInId, slug, status)); } } catch (Throwable t) { + // This runs on Quartz's job thread after the app's job, which a reporting failure must not + // disturb. The check-in for this run is not sent. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() diff --git a/sentry-spotlight/src/main/java/io/sentry/spotlight/SpotlightIntegration.java b/sentry-spotlight/src/main/java/io/sentry/spotlight/SpotlightIntegration.java index ac8916cd08c..c1a60848aa7 100644 --- a/sentry-spotlight/src/main/java/io/sentry/spotlight/SpotlightIntegration.java +++ b/sentry-spotlight/src/main/java/io/sentry/spotlight/SpotlightIntegration.java @@ -16,6 +16,7 @@ import io.sentry.SentryExecutorService; import io.sentry.SentryIntegrationPackageStorage; import io.sentry.SentryOptions; +import io.sentry.util.ExceptionUtils; import io.sentry.util.Platform; import java.io.Closeable; import java.io.IOException; @@ -81,6 +82,9 @@ private void sendEnvelope(final @NotNull SentryEnvelope envelope) { final GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) { options.getSerializer().serialize(envelope, gzip); } catch (Throwable e) { + // Serializing reads the app's own data, such as attachments and serializable extras, so + // we can't know what it throws. The envelope is not sent to Spotlight. + ExceptionUtils.rethrowIfFatal(e); logger.log( ERROR, "An exception occurred while submitting the envelope to the Sentry server.", e); } finally { diff --git a/sentry-spring-7/src/main/java/io/sentry/spring7/SentryRequestResolver.java b/sentry-spring-7/src/main/java/io/sentry/spring7/SentryRequestResolver.java index 4995d4bfe09..b5e6785c92e 100644 --- a/sentry-spring-7/src/main/java/io/sentry/spring7/SentryRequestResolver.java +++ b/sentry-spring-7/src/main/java/io/sentry/spring7/SentryRequestResolver.java @@ -7,6 +7,7 @@ import io.sentry.protocol.Request; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.CookieUtils; +import io.sentry.util.ExceptionUtils; import io.sentry.util.HttpUtils; import io.sentry.util.Objects; import io.sentry.util.UrlUtils; @@ -116,6 +117,9 @@ private List extractSecurityCookieNames(final @NotNull HttpServletReques } } } catch (Throwable t) { + // The request and its servlet context are the container's, whose implementations differ, so + // we can't know what they throw. Only the default session cookie names are filtered then. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() diff --git a/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java b/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java index 6abb989637e..3a452530a7d 100644 --- a/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java +++ b/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java @@ -17,6 +17,7 @@ import io.sentry.internal.eventprocessor.SentryEventProcessor; import io.sentry.spring7.tracing.SpringMvcTransactionNameProvider; import io.sentry.spring7.tracing.TransactionNameProvider; +import io.sentry.util.ExceptionUtils; import io.sentry.util.Objects; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; @@ -101,6 +102,9 @@ private void configureScope( } }); } catch (Throwable e) { + // This runs inside the app's HTTP request and reads the container's request object, so we + // can't know what it throws. The request continues without Sentry request data on the scope. + ExceptionUtils.rethrowIfFatal(e); scopes .getOptions() .getLogger() diff --git a/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckIn.java b/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckIn.java index ac17f85a985..663c3316650 100644 --- a/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckIn.java +++ b/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckIn.java @@ -42,6 +42,11 @@ /** * Monitor slug. If not set, no check-in will be sent. * + *

Placeholders and SpEL expressions in the slug are resolved. If resolving fails, the + * unresolved slug is used, so check-ins go to a monitor that likely does not exist. The failure + * is only logged as a warning through {@link io.sentry.SentryOptions#getLogger()}, which by + * default prints only when debug is enabled. + * * @return monitor slug */ @AliasFor("monitorSlug") diff --git a/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckInAdvice.java b/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckInAdvice.java index 2f83a079d0d..0f2e5230138 100644 --- a/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckInAdvice.java +++ b/sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckInAdvice.java @@ -11,6 +11,7 @@ import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.time.Stopwatch; +import io.sentry.util.ExceptionUtils; import io.sentry.util.MonitorConfigUtils; import io.sentry.util.Objects; import io.sentry.util.TracingUtils; @@ -128,6 +129,9 @@ public SentryCheckInAdvice(final @NotNull IScopes scopes, final boolean monitorS try { monitorSlug = resolver.resolveStringValue(checkInAnnotation.value()); } catch (Throwable e) { + // The resolver evaluates the app's placeholders and SpEL expressions, so we can't know what + // it throws. + ExceptionUtils.rethrowIfFatal(e); // When resolving fails, we fall back to the original string which may contain unresolved // expressions. Testing shows this can also happen if properties cannot be resolved (without // an exception being thrown). Sentry should alert the user about missed checkins in this diff --git a/sentry-spring-7/src/main/java/io/sentry/spring7/kafka/SentryKafkaRecordInterceptor.java b/sentry-spring-7/src/main/java/io/sentry/spring7/kafka/SentryKafkaRecordInterceptor.java index abae5b44cb6..d5d511d6cf8 100644 --- a/sentry-spring-7/src/main/java/io/sentry/spring7/kafka/SentryKafkaRecordInterceptor.java +++ b/sentry-spring-7/src/main/java/io/sentry/spring7/kafka/SentryKafkaRecordInterceptor.java @@ -12,6 +12,7 @@ import io.sentry.TransactionContext; import io.sentry.TransactionOptions; import io.sentry.kafka.SentryKafkaProducer; +import io.sentry.util.ExceptionUtils; import io.sentry.util.SpanUtils; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -71,6 +72,9 @@ public SentryKafkaRecordInterceptor( startTransaction(forkedScopes, record, transactionContext); currentContext.set(new SentryRecordContext(lifecycleToken, transaction)); } catch (Throwable t) { + // This parses headers written by whichever producer sent the record, and tracing must never + // stop the record from reaching the listener. The record is processed without a transaction. + ExceptionUtils.rethrowIfFatal(t); scopes.getOptions().getLogger().log(SentryLevel.ERROR, "Unable to wrap Kafka consumer.", t); } return delegateIntercept(record, consumer); diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java index 9fcb30665cb..d97946ce7a5 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryRequestResolver.java @@ -7,6 +7,7 @@ import io.sentry.protocol.Request; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.CookieUtils; +import io.sentry.util.ExceptionUtils; import io.sentry.util.HttpUtils; import io.sentry.util.Objects; import io.sentry.util.UrlUtils; @@ -116,6 +117,9 @@ private List extractSecurityCookieNames(final @NotNull HttpServletReques } } } catch (Throwable t) { + // The request and its servlet context are the container's, whose implementations differ, so + // we can't know what they throw. Only the default session cookie names are filtered then. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java index a710116896c..02a98881718 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java @@ -17,6 +17,7 @@ import io.sentry.internal.eventprocessor.SentryEventProcessor; import io.sentry.spring.jakarta.tracing.SpringMvcTransactionNameProvider; import io.sentry.spring.jakarta.tracing.TransactionNameProvider; +import io.sentry.util.ExceptionUtils; import io.sentry.util.Objects; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; @@ -101,6 +102,9 @@ private void configureScope( } }); } catch (Throwable e) { + // This runs inside the app's HTTP request and reads the container's request object, so we + // can't know what it throws. The request continues without Sentry request data on the scope. + ExceptionUtils.rethrowIfFatal(e); scopes .getOptions() .getLogger() diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckIn.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckIn.java index 89b04aa561a..82530a993a9 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckIn.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckIn.java @@ -42,6 +42,11 @@ /** * Monitor slug. If not set, no check-in will be sent. * + *

Placeholders and SpEL expressions in the slug are resolved. If resolving fails, the + * unresolved slug is used, so check-ins go to a monitor that likely does not exist. The failure + * is only logged as a warning through {@link io.sentry.SentryOptions#getLogger()}, which by + * default prints only when debug is enabled. + * * @return monitor slug */ @AliasFor("monitorSlug") diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java index a544c401966..754eea6bdde 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java @@ -11,6 +11,7 @@ import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.time.Stopwatch; +import io.sentry.util.ExceptionUtils; import io.sentry.util.MonitorConfigUtils; import io.sentry.util.Objects; import io.sentry.util.TracingUtils; @@ -125,6 +126,9 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl try { monitorSlug = resolver.resolveStringValue(checkInAnnotation.value()); } catch (Throwable e) { + // The resolver evaluates the app's placeholders and SpEL expressions, so we can't know what + // it throws. + ExceptionUtils.rethrowIfFatal(e); // When resolving fails, we fall back to the original string which may contain unresolved // expressions. Testing shows this can also happen if properties cannot be resolved (without // an exception being thrown). Sentry should alert the user about missed checkins in this diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/kafka/SentryKafkaRecordInterceptor.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/kafka/SentryKafkaRecordInterceptor.java index 32db2eea846..511b719b616 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/kafka/SentryKafkaRecordInterceptor.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/kafka/SentryKafkaRecordInterceptor.java @@ -12,6 +12,7 @@ import io.sentry.TransactionContext; import io.sentry.TransactionOptions; import io.sentry.kafka.SentryKafkaProducer; +import io.sentry.util.ExceptionUtils; import io.sentry.util.SpanUtils; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -71,6 +72,9 @@ public SentryKafkaRecordInterceptor( startTransaction(forkedScopes, record, transactionContext); currentContext.set(new SentryRecordContext(lifecycleToken, transaction)); } catch (Throwable t) { + // This parses headers written by whichever producer sent the record, and tracing must never + // stop the record from reaching the listener. The record is processed without a transaction. + ExceptionUtils.rethrowIfFatal(t); scopes.getOptions().getLogger().log(SentryLevel.ERROR, "Unable to wrap Kafka consumer.", t); } return delegateIntercept(record, consumer); diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java index 1c8ec52ac88..f598a58d7bb 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryRequestResolver.java @@ -7,6 +7,7 @@ import io.sentry.protocol.Request; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.CookieUtils; +import io.sentry.util.ExceptionUtils; import io.sentry.util.HttpUtils; import io.sentry.util.Objects; import io.sentry.util.UrlUtils; @@ -116,6 +117,9 @@ private List extractSecurityCookieNames(final @NotNull HttpServletReques } } } catch (Throwable t) { + // The request and its servlet context are the container's, whose implementations differ, so + // we can't know what they throw. Only the default session cookie names are filtered then. + ExceptionUtils.rethrowIfFatal(t); scopes .getOptions() .getLogger() diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java index 4718ee982b1..353aecd8b4c 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java @@ -17,6 +17,7 @@ import io.sentry.internal.eventprocessor.SentryEventProcessor; import io.sentry.spring.tracing.SpringMvcTransactionNameProvider; import io.sentry.spring.tracing.TransactionNameProvider; +import io.sentry.util.ExceptionUtils; import io.sentry.util.Objects; import java.io.IOException; import javax.servlet.FilterChain; @@ -101,6 +102,9 @@ private void configureScope( } }); } catch (Throwable e) { + // This runs inside the app's HTTP request and reads the container's request object, so we + // can't know what it throws. The request continues without Sentry request data on the scope. + ExceptionUtils.rethrowIfFatal(e); scopes .getOptions() .getLogger() diff --git a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckIn.java b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckIn.java index 55fedd93c2e..2c10ae38a76 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckIn.java +++ b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckIn.java @@ -42,6 +42,11 @@ /** * Monitor slug. If not set, no check-in will be sent. * + *

Placeholders and SpEL expressions in the slug are resolved. If resolving fails, the + * unresolved slug is used, so check-ins go to a monitor that likely does not exist. The failure + * is only logged as a warning through {@link io.sentry.SentryOptions#getLogger()}, which by + * default prints only when debug is enabled. + * * @return monitor slug */ @AliasFor("monitorSlug") diff --git a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java index f6923c77e6e..1c44d0bdef8 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java @@ -11,6 +11,7 @@ import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.time.Stopwatch; +import io.sentry.util.ExceptionUtils; import io.sentry.util.MonitorConfigUtils; import io.sentry.util.Objects; import io.sentry.util.TracingUtils; @@ -125,6 +126,9 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl try { monitorSlug = resolver.resolveStringValue(checkInAnnotation.value()); } catch (Throwable e) { + // The resolver evaluates the app's placeholders and SpEL expressions, so we can't know what + // it throws. + ExceptionUtils.rethrowIfFatal(e); // When resolving fails, we fall back to the original string which may contain unresolved // expressions. // Testing shows this can also happen if properties cannot be resolved (without an exception diff --git a/sentry-spring/src/main/java/io/sentry/spring/kafka/SentryKafkaRecordInterceptor.java b/sentry-spring/src/main/java/io/sentry/spring/kafka/SentryKafkaRecordInterceptor.java index bf65b64f950..fadc3b3e1c0 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/kafka/SentryKafkaRecordInterceptor.java +++ b/sentry-spring/src/main/java/io/sentry/spring/kafka/SentryKafkaRecordInterceptor.java @@ -12,6 +12,7 @@ import io.sentry.TransactionContext; import io.sentry.TransactionOptions; import io.sentry.kafka.SentryKafkaProducer; +import io.sentry.util.ExceptionUtils; import io.sentry.util.SpanUtils; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -77,6 +78,9 @@ public SentryKafkaRecordInterceptor( startTransaction(forkedScopes, record, transactionContext); currentContext.set(new SentryRecordContext(lifecycleToken, transaction)); } catch (Throwable t) { + // This parses headers written by whichever producer sent the record, and tracing must never + // stop the record from reaching the listener. The record is processed without a transaction. + ExceptionUtils.rethrowIfFatal(t); scopes.getOptions().getLogger().log(SentryLevel.ERROR, "Unable to wrap Kafka consumer.", t); } return delegateIntercept(record, consumer);