Skip to content

Commit

Permalink
Bring back an old API for SentryEnvelopeItem.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejwalkowiak committed Nov 20, 2020
1 parent b9d65b3 commit a568d39
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
5 changes: 3 additions & 2 deletions sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ public SentryClient(final @NotNull SentryOptions options, @Nullable Connection c
final List<SentryEnvelopeItem> envelopeItems = new ArrayList<>();

if (event != null) {
final SentryEnvelopeItem eventItem = SentryEnvelopeItem.from(options.getSerializer(), event);
final SentryEnvelopeItem eventItem =
SentryEnvelopeItem.fromEvent(options.getSerializer(), event);
envelopeItems.add(eventItem);
sentryId = event.getEventId();
}

if (session != null) {
final SentryEnvelopeItem sessionItem =
SentryEnvelopeItem.from(options.getSerializer(), session);
SentryEnvelopeItem.fromSession(options.getSerializer(), session);
envelopeItems.add(sessionItem);
}

Expand Down
9 changes: 5 additions & 4 deletions sentry/src/main/java/io/sentry/SentryEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public SentryEnvelope(

public static @NotNull SentryEnvelope from(
final @NotNull ISerializer serializer,
final @NotNull Object item,
final @NotNull Session session,
final @Nullable SdkVersion sdkVersion)
throws IOException {
Objects.requireNonNull(serializer, "Serializer is required.");
Objects.requireNonNull(item, "item is required.");
Objects.requireNonNull(session, "session is required.");

return new SentryEnvelope(null, sdkVersion, SentryEnvelopeItem.from(serializer, item));
return new SentryEnvelope(
null, sdkVersion, SentryEnvelopeItem.fromSession(serializer, session));
}

public static @NotNull SentryEnvelope from(
Expand All @@ -73,6 +74,6 @@ public SentryEnvelope(
Objects.requireNonNull(event, "item is required.");

return new SentryEnvelope(
event.getEventId(), sdkVersion, SentryEnvelopeItem.from(serializer, event));
event.getEventId(), sdkVersion, SentryEnvelopeItem.fromEvent(serializer, event));
}
}
56 changes: 40 additions & 16 deletions sentry/src/main/java/io/sentry/SentryEnvelopeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ public final class SentryEnvelopeItem {
return header;
}

public static @NotNull SentryEnvelopeItem fromSession(
final @NotNull ISerializer serializer, final @NotNull Session session) throws IOException {
Objects.requireNonNull(serializer, "ISerializer is required.");
Objects.requireNonNull(session, "Session is required.");

final CachedItem cachedItem =
new CachedItem(
() -> {
try (final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final Writer writer = new BufferedWriter(new OutputStreamWriter(stream, UTF_8))) {
serializer.serialize(session, writer);
return stream.toByteArray();
}
});

SentryEnvelopeItemHeader itemHeader =
new SentryEnvelopeItemHeader(
SentryItemType.Session, () -> cachedItem.getBytes().length, "application/json", null);

return new SentryEnvelopeItem(itemHeader, () -> cachedItem.getBytes());
}

public @Nullable SentryEvent getEvent(final @NotNull ISerializer serializer) throws Exception {
if (header == null || header.getType() != SentryItemType.Event) {
return null;
Expand All @@ -64,41 +86,43 @@ public final class SentryEnvelopeItem {
}
}

public @Nullable SentryTransaction getTransaction(final @NotNull ISerializer serializer)
throws Exception {
if (header == null || header.getType() != SentryItemType.Transaction) {
return null;
}
try (final Reader eventReader =
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(getData()), UTF_8))) {
return serializer.deserializeTransaction(eventReader);
}
}

public static @NotNull SentryEnvelopeItem from(
final @NotNull ISerializer serializer, final @NotNull Object item) throws IOException {
Objects.requireNonNull(item, "item is required.");
public static @NotNull SentryEnvelopeItem fromEvent(
final @NotNull ISerializer serializer, final @NotNull SentryBaseEvent event)
throws IOException {
Objects.requireNonNull(serializer, "ISerializer is required.");
Objects.requireNonNull(event, "SentryEvent is required.");

final CachedItem cachedItem =
new CachedItem(
() -> {
try (final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final Writer writer = new BufferedWriter(new OutputStreamWriter(stream, UTF_8))) {
serializer.serialize(item, writer);
serializer.serialize(event, writer);
return stream.toByteArray();
}
});

SentryEnvelopeItemHeader itemHeader =
new SentryEnvelopeItemHeader(
SentryItemType.resolve(item),
SentryItemType.resolve(event),
() -> cachedItem.getBytes().length,
"application/json",
null);

return new SentryEnvelopeItem(itemHeader, () -> cachedItem.getBytes());
}

public @Nullable SentryTransaction getTransaction(final @NotNull ISerializer serializer)
throws Exception {
if (header == null || header.getType() != SentryItemType.Transaction) {
return null;
}
try (final Reader eventReader =
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(getData()), UTF_8))) {
return serializer.deserializeTransaction(eventReader);
}
}

public static SentryEnvelopeItem fromUserFeedback(
final @NotNull ISerializer serializer, final @NotNull UserFeedback userFeedback) {
Objects.requireNonNull(serializer, "ISerializer is required.");
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/cache/CacheStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void moveInitFlagIfNecessary(
if (currentSession.getSessionId().equals(session.getSessionId())) {
session.setInitAsTrue();
try {
newSessionItem = SentryEnvelopeItem.from(serializer, session);
newSessionItem = SentryEnvelopeItem.fromSession(serializer, session);
// remove item from envelope items so we can replace with the new one that has the
// init flag true
itemsIterator.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ class AsyncConnectionTest {

@Test
fun `When envelopes have retry after items, ignore them and send others`() {
val sessionItem = SentryEnvelopeItem.from(fixture.sentryOptions.serializer, createSession())
val eventItem = SentryEnvelopeItem.from(fixture.sentryOptions.serializer, SentryEvent())
val sessionItem = SentryEnvelopeItem.fromSession(fixture.sentryOptions.serializer, createSession())
val eventItem = SentryEnvelopeItem.fromEvent(fixture.sentryOptions.serializer, SentryEvent())
val envelope = SentryEnvelope(SentryEnvelopeHeader(), arrayListOf(sessionItem, eventItem))

whenever(fixture.transport.isRetryAfter(eq("event"))).thenReturn(false)
Expand Down

0 comments on commit a568d39

Please sign in to comment.