-
-
Notifications
You must be signed in to change notification settings - Fork 452
[Logs 4] Send logs in batches #4378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
sentry/src/main/java/io/sentry/logger/ILoggerBatchProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.sentry.logger; | ||
|
||
import io.sentry.SentryLogEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface ILoggerBatchProcessor { | ||
void add(@NotNull SentryLogEvent event); | ||
|
||
void close(boolean isRestarting); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package io.sentry.logger; | ||
|
||
import io.sentry.ISentryClient; | ||
import io.sentry.ISentryLifecycleToken; | ||
import io.sentry.SentryLogEvent; | ||
import io.sentry.SentryLogEvents; | ||
import io.sentry.SentryOptions; | ||
import io.sentry.util.AutoClosableReentrantLock; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.Future; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class LoggerBatchProcessor implements ILoggerBatchProcessor { | ||
|
||
public static final int FLUSH_AFTER_MS = 5000; | ||
public static final int MAX_BATCH_SIZE = 100; | ||
|
||
private final @NotNull SentryOptions options; | ||
private final @NotNull ISentryClient client; | ||
private final @NotNull Queue<SentryLogEvent> queue; | ||
private volatile @Nullable Future<?> scheduledFlush; | ||
private static final @NotNull AutoClosableReentrantLock scheduleLock = | ||
new AutoClosableReentrantLock(); | ||
|
||
public LoggerBatchProcessor( | ||
final @NotNull SentryOptions options, final @NotNull ISentryClient client) { | ||
this.options = options; | ||
this.client = client; | ||
this.queue = new ConcurrentLinkedQueue<>(); | ||
} | ||
|
||
@Override | ||
public void add(final @NotNull SentryLogEvent logEvent) { | ||
queue.offer(logEvent); | ||
maybeSchedule(false, false); | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void close(final boolean isRestarting) { | ||
if (isRestarting) { | ||
maybeSchedule(true, true); | ||
} else { | ||
while (!queue.isEmpty()) { | ||
flushBatch(); | ||
} | ||
} | ||
} | ||
|
||
private void maybeSchedule(boolean forceSchedule, boolean immediately) { | ||
try (final @NotNull ISentryLifecycleToken ignored = scheduleLock.acquire()) { | ||
final @Nullable Future<?> latestScheduledFlush = scheduledFlush; | ||
if (forceSchedule | ||
|| latestScheduledFlush == null | ||
|| latestScheduledFlush.isDone() | ||
|| latestScheduledFlush.isCancelled()) { | ||
final int flushAfterMs = immediately ? 0 : FLUSH_AFTER_MS; | ||
scheduledFlush = options.getExecutorService().schedule(new BatchRunnable(), flushAfterMs); | ||
} | ||
} | ||
} | ||
|
||
private void flush() { | ||
flushInternal(); | ||
try (final @NotNull ISentryLifecycleToken ignored = scheduleLock.acquire()) { | ||
if (!queue.isEmpty()) { | ||
maybeSchedule(true, false); | ||
} | ||
} | ||
} | ||
|
||
private void flushInternal() { | ||
flushBatch(); | ||
if (queue.size() >= MAX_BATCH_SIZE) { | ||
flushInternal(); | ||
} | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private void flushBatch() { | ||
final @NotNull List<SentryLogEvent> logEvents = new ArrayList<>(MAX_BATCH_SIZE); | ||
do { | ||
final @Nullable SentryLogEvent logEvent = queue.poll(); | ||
if (logEvent != null) { | ||
logEvents.add(logEvent); | ||
} | ||
} while (!queue.isEmpty() && logEvents.size() < MAX_BATCH_SIZE); | ||
|
||
client.captureBatchedLogEvents(new SentryLogEvents(logEvents)); | ||
} | ||
|
||
private class BatchRunnable implements Runnable { | ||
|
||
@Override | ||
public void run() { | ||
flush(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
sentry/src/main/java/io/sentry/logger/NoOpLoggerBatchProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.sentry.logger; | ||
|
||
import io.sentry.SentryLogEvent; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@ApiStatus.Experimental | ||
public final class NoOpLoggerBatchProcessor implements ILoggerBatchProcessor { | ||
|
||
private static final NoOpLoggerBatchProcessor instance = new NoOpLoggerBatchProcessor(); | ||
|
||
private NoOpLoggerBatchProcessor() {} | ||
|
||
public static NoOpLoggerBatchProcessor getInstance() { | ||
return instance; | ||
} | ||
|
||
@Override | ||
public void add(@NotNull SentryLogEvent event) { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public void close(final boolean isRestarting) { | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// do nothing | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.