Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7bbbe08
ref: fix test linting (#491)
marandaneto Jul 23, 2020
95cc5f4
fix: event.level fatal is not a crashed session (#492)
marandaneto Jul 23, 2020
50a2165
feat: drop store endpoint in favor of envelope (#490)
marandaneto Jul 24, 2020
2f3b563
fix: ktlint for kts files (#493)
marandaneto Jul 24, 2020
29fbcc7
sentry-java readme
bruno-garcia Jul 27, 2020
14a5e29
feat: rotate cache folder (#494)
marandaneto Jul 28, 2020
db5345b
Merge remote-tracking branch 'origin' into feat/sentry-java
maciejwalkowiak Jul 29, 2020
ad2e0d8
make EventProcessor.process Nullable (#507)
davemt Aug 5, 2020
707015f
Merge remote-tracking branch 'origin/master' into feat/sentry-java
maciejwalkowiak Aug 11, 2020
acfb973
Merge remote-tracking branch 'origin/master' into feat/sentry-java
maciejwalkowiak Aug 12, 2020
9607fea
fix merge conflict
marandaneto Aug 13, 2020
d18da36
Add Logback integration. (#511)
maciejwalkowiak Aug 20, 2020
d14445a
Merge branch 'master' into 3.0.0
marandaneto Aug 26, 2020
68c7ceb
Sentry Spring Boot Starter. (#517)
maciejwalkowiak Aug 27, 2020
2d6e215
Refactor binding options (#530)
maciejwalkowiak Aug 29, 2020
0a8bde6
Add `sendDefaultPii` flag to `SentryOptions`. (#531)
maciejwalkowiak Aug 30, 2020
82e4efe
Attach breadcrumbs to events triggered in Logback integration. (#532)
maciejwalkowiak Aug 31, 2020
944f478
Bump to 3.0.0-alpha.1 (#535)
marandaneto Sep 1, 2020
eefdd31
feat: enable release health by default (#534)
marandaneto Sep 1, 2020
df08324
Merge branch 'master' into 3.0.0
marandaneto Sep 2, 2020
35750dd
Add Log4j2 integration. (#537)
maciejwalkowiak Sep 2, 2020
3737aab
feat: session updates along with the event (#514)
marandaneto Sep 3, 2020
cd6f526
Merge remote-tracking branch 'origin/master' into feat/sentry-java
maciejwalkowiak Sep 3, 2020
3915ca4
fix: enable session tracking by default on sample (#538)
marandaneto Sep 3, 2020
9aad2cb
chore 3.0.0-alpha.1
marandaneto Sep 3, 2020
e767af6
prepare: 3.0.0-alpha.2
marandaneto Sep 3, 2020
b754303
Bump Gradle to 6.6.1
marandaneto Sep 3, 2020
60af521
Extract minimum required coverage by Jacoco plugin. (#541)
maciejwalkowiak Sep 4, 2020
799e581
Merge branch 'master' into 3.0.0
marandaneto Sep 4, 2020
923f9fc
Add Spring Integration. (#539)
maciejwalkowiak Sep 8, 2020
7845eb8
Merge branch '3.0.0' into feat/sentry-java
bruno-garcia Sep 8, 2020
89b7606
fix conflicts
marandaneto Sep 9, 2020
ed1b133
Merge branch 'master' into 3.0.0
marandaneto Sep 9, 2020
33766f4
ref: move init flag (#542)
marandaneto Sep 9, 2020
c891d6a
fix: Asserting events on Envelope sent to Transport (#548)
maciejwalkowiak Sep 9, 2020
ae0219d
Add option to set user information via `SentryUserProvider` (#549)
maciejwalkowiak Sep 9, 2020
cdb4530
Merge branch '3.0.0' into feat/sentry-java
bruno-garcia Sep 9, 2020
6d02f98
upgrade cache id
marandaneto Sep 10, 2020
dd0e7d8
Feat/sentry java fix kotlin (#552)
maciejwalkowiak Sep 10, 2020
188ef14
ref: sentry-core to sentry (#553)
bruno-garcia Sep 10, 2020
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
Prev Previous commit
Next Next commit
ref: move init flag (#542)
  • Loading branch information
marandaneto authored Sep 9, 2020
commit 33766f404dec923c36a1b6ad2680f694df62cdc1
7 changes: 7 additions & 0 deletions sentry-core/src/main/java/io/sentry/core/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -142,6 +143,12 @@ public Session(
return init;
}

/** Used for migrating the init flag when an session is gonna be deleted. */
@ApiStatus.Internal
public void setInitAsTrue() {
this.init = true;
}

public int errorCount() {
return errorCount.get();
}
Expand Down
214 changes: 213 additions & 1 deletion sentry-core/src/main/java/io/sentry/core/cache/CacheStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@
import static io.sentry.core.SentryLevel.ERROR;

import io.sentry.core.ISerializer;
import io.sentry.core.SentryEnvelope;
import io.sentry.core.SentryEnvelopeItem;
import io.sentry.core.SentryItemType;
import io.sentry.core.SentryLevel;
import io.sentry.core.SentryOptions;
import io.sentry.core.Session;
import io.sentry.core.util.Objects;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

abstract class CacheStrategy {

Expand Down Expand Up @@ -79,10 +101,15 @@ protected void rotateCacheIfNeeded(final @NotNull File[] files) {

sortFilesOldestToNewest(files);

final File[] notDeletedFiles = Arrays.copyOfRange(files, totalToBeDeleted, length);

// delete files from the top of the Array as its sorted by the oldest to the newest
for (int i = 0; i < totalToBeDeleted; i++) {
final File file = files[i];
// sanity check if the file actually exists.

// move init flag if necessary
moveInitFlagIfNecessary(file, notDeletedFiles);

if (!file.delete()) {
options
.getLogger()
Expand All @@ -91,4 +118,189 @@ protected void rotateCacheIfNeeded(final @NotNull File[] files) {
}
}
}

private void moveInitFlagIfNecessary(
final @NotNull File currentFile, final @NotNull File[] notDeletedFiles) {
final SentryEnvelope currentEnvelope = readEnvelope(currentFile);

if (!isValidEnvelope(currentEnvelope)) {
return;
}

final Session currentSession = getFirstSession(currentEnvelope);

if (!isValidSession(currentSession)) {
return;
}

// nothing to do if its not true
final Boolean currentSessionInit = currentSession.getInit();
if (currentSessionInit == null || !currentSessionInit) {
return;
}

// we need to move the init flag
for (final File notDeletedFile : notDeletedFiles) {
final SentryEnvelope envelope = readEnvelope(notDeletedFile);

if (!isValidEnvelope(envelope)) {
continue;
}

SentryEnvelopeItem newSessionItem = null;
final Iterator<SentryEnvelopeItem> itemsIterator = envelope.getItems().iterator();

while (itemsIterator.hasNext()) {
final SentryEnvelopeItem envelopeItem = itemsIterator.next();

if (!isSessionType(envelopeItem)) {
continue;
}

final Session session = readSession(envelopeItem);

if (!isValidSession(session)) {
continue;
}

final Boolean init = session.getInit();
if (init != null && init) {
options
.getLogger()
.log(ERROR, "Session %s has 2 times the init flag.", currentSession.getSessionId());
return;
}

if (currentSession.getSessionId().equals(session.getSessionId())) {
session.setInitAsTrue();
try {
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();
} catch (IOException e) {
options
.getLogger()
.log(
ERROR,
e,
"Failed to create new envelope item for the session %s",
currentSession.getSessionId());
}

break;
}
}

if (newSessionItem != null) {
final SentryEnvelope newEnvelope = buildNewEnvelope(envelope, newSessionItem);

long notDeletedFileTimestamp = notDeletedFile.lastModified();
if (!notDeletedFile.delete()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"File can't be deleted: %s",
notDeletedFile.getAbsolutePath());
}

saveNewEnvelope(newEnvelope, notDeletedFile, notDeletedFileTimestamp);
break;
}
}
}

private @Nullable SentryEnvelope readEnvelope(final @NotNull File file) {
try (final InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
return serializer.deserializeEnvelope(inputStream);
} catch (IOException e) {
options.getLogger().log(ERROR, "Failed to deserialize the envelope.", e);
}

return null;
}

private @Nullable Session getFirstSession(final @NotNull SentryEnvelope envelope) {
for (final SentryEnvelopeItem item : envelope.getItems()) {
if (!isSessionType(item)) {
continue;
}

// we are assuming that there's only 1 session per envelope for now
return readSession(item);
}
return null;
}

private boolean isValidSession(final @Nullable Session session) {
if (session == null) {
return false;
}

if (!session.getStatus().equals(Session.State.Ok)) {
return false;
}

final UUID sessionId = session.getSessionId();

if (sessionId == null) {
return false;
}
return true;
}

private boolean isSessionType(final @Nullable SentryEnvelopeItem item) {
if (item == null) {
return false;
}

return item.getHeader().getType().equals(SentryItemType.Session);
}

private @Nullable Session readSession(final @NotNull SentryEnvelopeItem item) {
try (final Reader reader =
new BufferedReader(
new InputStreamReader(new ByteArrayInputStream(item.getData()), UTF_8))) {
return serializer.deserializeSession(reader);
} catch (Exception e) {
options.getLogger().log(ERROR, "Failed to deserialize the session.", e);
}
return null;
}

private void saveNewEnvelope(
final @NotNull SentryEnvelope envelope, final @NotNull File file, final long timestamp) {
try (final OutputStream outputStream = new FileOutputStream(file);
final Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8))) {
serializer.serialize(envelope, writer);
// we need to set the same timestamp so the sorting from oldest to newest wont break.
file.setLastModified(timestamp);
} catch (Exception e) {
options.getLogger().log(ERROR, "Failed to serialize the new envelope to the disk.", e);
}
}

private @NotNull SentryEnvelope buildNewEnvelope(
final @NotNull SentryEnvelope envelope, final @NotNull SentryEnvelopeItem sessionItem) {
final List<SentryEnvelopeItem> newEnvelopeItems = new ArrayList<>();

for (final SentryEnvelopeItem newEnvelopeItem : envelope.getItems()) {
newEnvelopeItems.add(newEnvelopeItem);
}
newEnvelopeItems.add(sessionItem);

return new SentryEnvelope(envelope.getHeader(), newEnvelopeItems);
}

private boolean isValidEnvelope(final @Nullable SentryEnvelope envelope) {
if (envelope == null) {
return false;
}

if (!envelope.getItems().iterator().hasNext()) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ public void store(final @NotNull SentryEnvelope envelope, final @Nullable Object
}
updateCurrentSession(currentSessionFile, envelope);
}
// TODO: problem we need to update the current session file

// TODO: probably we need to update the current session file for session updates to because of
// hardcrash events

final File envelopeFile = getEnvelopeFile(envelope);
if (envelopeFile.exists()) {
options
Expand Down
Loading