Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutOfMemoryError> { interceptor.intercept(request, chain) }
}

@Test
fun `adds breadcrumb when http calls succeeds`() {
executeQuery(fixture.getSut())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutOfMemoryError> { interceptor.intercept(request, chain) }
}

@Test
fun `adds breadcrumb when http calls succeeds`() {
executeQuery(fixture.getSut())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,6 +86,9 @@ <K, V, U> 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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,6 +187,9 @@ static final class SentryProducerHandler<K, V> 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()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -347,6 +349,16 @@ void setTransportFactory(final @Nullable ITransportFactory transportFactory) {
this.transportFactory = transportFactory;
}

/**
* Sets the encoder that formats the message sent to Sentry.
*
* <p>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 <configuration debug="true">} or a
* status listener.
*
* @param encoder the encoder
*/
public void setEncoder(Encoder<ILoggingEvent> encoder) {
this.encoder = encoder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -329,6 +330,22 @@ class SentryAppenderTest {
)
}

class FatalEncoder : EncoderBase<ILoggingEvent>() {
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<OutOfMemoryError> { fixture.logger.info("testing a fatal encoder error") }
}

@Test
fun `event date is in UTC`() {
fixture = Fixture(minimumEventLevel = Level.DEBUG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,6 +117,9 @@ private List<String> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
/**
* Monitor slug. If not set, no check-in will be sent.
*
* <p>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")
Expand Down
Loading
Loading