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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Fixes

- Session Replay: Mask Compose images loaded with Coil 3.1+ `AsyncImage` when `maskAllImages` is enabled ([#6759](https://github.com/getsentry/sentry-java/pull/6759))
- Attachments: A file attachment added by path is now streamed into the envelope when it's sent or cached, instead of being read into memory and held until the envelope is gone. If the file is missing when the envelope is sent, only that attachment is dropped. If it changes while it's being sent, the whole envelope is dropped instead of sending a corrupt one. Both cases are recorded in client reports with the reason `invalid`. `SentryEnvelopeItem.getData()` still returns its bytes, but reads the file on each call. Spotlight now serializes the envelope on the capturing thread before the SDK sends it ([#6777](https://github.com/getsentry/sentry-java/pull/6777))
- Profiling: No longer crash on API 35+ devices whose framework can't load `ProfilingManager`. Perfetto profiling is turned off on those devices instead ([#6742](https://github.com/getsentry/sentry-java/pull/6742))
- Android: Out-of-process crash reporting now supports Android 5.0 (API level 21) and later, previously required Android 11 (API level 30) ([#6614](https://github.com/getsentry/sentry-java/pull/6614))
- Android profiling: Uploading a Perfetto profile chunk now keeps the trace in memory once instead of up to three times, lowering peak memory use for traces up to 50 MB ([#6743](https://github.com/getsentry/sentry-java/pull/6743))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.SentryOptions;
import io.sentry.util.Platform;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -59,27 +60,48 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) {
}
}

/**
* Serializes the envelope on the calling thread, so it is done before the transport serializes
* the same envelope, and posts it to Spotlight on a background thread.
*/
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void execute(final @NotNull SentryEnvelope envelope, final @Nullable Hint hint) {
if (options == null) {
return;
}
final @NotNull byte[] body;
try {
body = serializeEnvelope(options, envelope);
} catch (Exception e) {
// ISerializer.serialize declares Exception. On failure the envelope isn't sent to Spotlight.
logger.log(ERROR, "Failed to serialize the envelope for Spotlight.", e);
return;
}
try {
executorService.submit(() -> sendEnvelope(envelope));
executorService.submit(() -> sendEnvelope(body));
} catch (RejectedExecutionException e) {
logger.log(WARNING, "Spotlight envelope submission rejected.", e);
}
}

private void sendEnvelope(final @NotNull SentryEnvelope envelope) {
private static @NotNull byte[] serializeEnvelope(
final @NotNull SentryOptions options, final @NotNull SentryEnvelope envelope)
throws Exception {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (final GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
options.getSerializer().serialize(envelope, gzip);
}
return outputStream.toByteArray();
}

private void sendEnvelope(final @NotNull byte[] body) {
try {
if (options == null) {
throw new IllegalArgumentException("SentryOptions are required to send envelopes.");
}
final String spotlightConnectionUrl = getSpotlightConnectionUrl();

final HttpURLConnection connection = createConnection(spotlightConnectionUrl);
try (final OutputStream outputStream = connection.getOutputStream();
final GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
options.getSerializer().serialize(envelope, gzip);
try (final OutputStream outputStream = connection.getOutputStream()) {
outputStream.write(body);
} catch (Throwable e) {
logger.log(
ERROR, "An exception occurred while submitting the envelope to the Sentry server.", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package io.sentry.spotlight

import io.sentry.IScopes
import io.sentry.ISerializer
import io.sentry.SentryEnvelope
import io.sentry.SentryOptions
import io.sentry.SentryOptions.BeforeEnvelopeCallback
import io.sentry.protocol.SentryId
import io.sentry.util.SpotlightPlatformTestManipulator
import java.io.OutputStream
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.same
import org.mockito.kotlin.verify

class SpotlightIntegrationTest {
@Test
Expand Down Expand Up @@ -71,4 +78,24 @@ class SpotlightIntegrationTest {

assertEquals("http://example.com:1234/stream", spotlight.spotlightConnectionUrl)
}

@Test
fun `serializes the envelope before execute returns`() {
val serializer = mock<ISerializer>()
val options =
SentryOptions().apply {
isEnableSpotlight = true
// Nothing listens there, so the background post fails fast.
spotlightConnectionUrl = "http://localhost:1/stream"
setSerializer(serializer)
}
val envelope = SentryEnvelope(SentryId(), null, emptyList())
val spotlight = SpotlightIntegration()
spotlight.register(mock<IScopes>(), options)

spotlight.execute(envelope, null)

verify(serializer).serialize(same(envelope), any<OutputStream>())
spotlight.close()
}
}
7 changes: 7 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -5183,6 +5183,7 @@ public final class io/sentry/clientreport/DiscardReason : java/lang/Enum {
public static final field CACHE_OVERFLOW Lio/sentry/clientreport/DiscardReason;
public static final field CALLBACK_ERROR Lio/sentry/clientreport/DiscardReason;
public static final field EVENT_PROCESSOR Lio/sentry/clientreport/DiscardReason;
public static final field INVALID Lio/sentry/clientreport/DiscardReason;
public static final field NETWORK_ERROR Lio/sentry/clientreport/DiscardReason;
public static final field QUEUE_OVERFLOW Lio/sentry/clientreport/DiscardReason;
public static final field RATELIMIT_BACKOFF Lio/sentry/clientreport/DiscardReason;
Expand Down Expand Up @@ -5270,6 +5271,11 @@ public final class io/sentry/exception/InvalidSentryTraceHeaderException : java/
public fun getSentryTraceHeader ()Ljava/lang/String;
}

public final class io/sentry/exception/SentryEnvelopeAbortedException : java/io/IOException {
public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
}

public final class io/sentry/exception/SentryEnvelopeException : java/lang/Exception {
public fun <init> (Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
Expand Down Expand Up @@ -7984,6 +7990,7 @@ public final class io/sentry/util/FileUtils {
public fun <init> ()V
public static fun createDirectory (Ljava/io/File;)Z
public static fun deleteRecursively (Ljava/io/File;)Z
public static fun ensureReadableFile (Ljava/io/File;J)V
public static fun inputStreamToByteArray (Ljava/io/InputStream;J)[B
public static fun readBytesFromFile (Ljava/lang/String;J)[B
public static fun readText (Ljava/io/File;)Ljava/lang/String;
Expand Down
31 changes: 22 additions & 9 deletions sentry/src/main/java/io/sentry/JsonSerializer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry;

import io.sentry.clientreport.ClientReport;
import io.sentry.clientreport.DiscardReason;
import io.sentry.profilemeasurements.ProfileMeasurement;
import io.sentry.profilemeasurements.ProfileMeasurementValue;
import io.sentry.protocol.App;
Expand Down Expand Up @@ -245,22 +246,34 @@ public void serialize(@NotNull SentryEnvelope envelope, @NotNull OutputStream ou
writer.write("\n");

for (final SentryEnvelopeItem item : envelope.getItems()) {
final @NotNull SentryEnvelopeItem.Payload payload;
try {
payload = item.openPayload();
} catch (Exception exception) {
// Nothing of the item is written yet, so the rest of the envelope stays valid.
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
if (item.markDropRecorded()) {
options.getClientReportRecorder().recordLostEnvelopeItem(DiscardReason.INVALID, item);
}
continue;
}
// Once the item header is written, a failure leaves the envelope invalid, so it propagates.
try {
// When this throws we don't write anything and continue with the next item.
final byte[] data = item.getData();

item.getHeader()
.serialize(new JsonObjectWriter(writer, options.getMaxDepth()), options.getLogger());
.serialize(
new JsonObjectWriter(writer, options.getMaxDepth()),
options.getLogger(),
payload.getLength());
writer.write("\n");
writer.flush();

outputStream.write(data);
payload.writeTo(outputStream);

writer.write("\n");
} catch (Exception exception) {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
} finally {
payload.close();
}
}
} finally {
Expand Down
Loading
Loading