-
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathLoggerBatchProcessor.java
More file actions
148 lines (132 loc) · 4.57 KB
/
LoggerBatchProcessor.java
File metadata and controls
148 lines (132 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package io.sentry.logger;
import io.sentry.ISentryClient;
import io.sentry.ISentryExecutorService;
import io.sentry.ISentryLifecycleToken;
import io.sentry.SentryExecutorService;
import io.sentry.SentryLevel;
import io.sentry.SentryLogEvent;
import io.sentry.SentryLogEvents;
import io.sentry.SentryOptions;
import io.sentry.transport.ReusableCountLatch;
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 java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
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;
public static final int MAX_QUEUE_SIZE = 1000;
private final @NotNull SentryOptions options;
private final @NotNull ISentryClient client;
private final @NotNull Queue<SentryLogEvent> queue;
private final @NotNull ISentryExecutorService executorService;
private volatile @Nullable Future<?> scheduledFlush;
private static final @NotNull AutoClosableReentrantLock scheduleLock =
new AutoClosableReentrantLock();
private volatile boolean hasScheduled = false;
private final @NotNull ReusableCountLatch pendingCount = new ReusableCountLatch();
public LoggerBatchProcessor(
final @NotNull SentryOptions options, final @NotNull ISentryClient client) {
this.options = options;
this.client = client;
this.queue = new ConcurrentLinkedQueue<>();
this.executorService = new SentryExecutorService(options);
}
@Override
public void add(final @NotNull SentryLogEvent logEvent) {
if (pendingCount.getCount() >= MAX_QUEUE_SIZE) {
return;
}
pendingCount.increment();
queue.offer(logEvent);
maybeSchedule(false, false);
}
@SuppressWarnings("FutureReturnValueIgnored")
@Override
public void close(final boolean isRestarting) {
if (isRestarting) {
maybeSchedule(true, true);
executorService.submit(() -> executorService.close(options.getShutdownTimeoutMillis()));
} else {
executorService.close(options.getShutdownTimeoutMillis());
while (!queue.isEmpty()) {
flushBatch();
}
}
}
private void maybeSchedule(boolean forceSchedule, boolean immediately) {
if (hasScheduled && !forceSchedule) {
return;
}
try (final @NotNull ISentryLifecycleToken ignored = scheduleLock.acquire()) {
final @Nullable Future<?> latestScheduledFlush = scheduledFlush;
if (forceSchedule
|| latestScheduledFlush == null
|| latestScheduledFlush.isDone()
|| latestScheduledFlush.isCancelled()) {
hasScheduled = true;
final int flushAfterMs = immediately ? 0 : FLUSH_AFTER_MS;
try {
scheduledFlush = executorService.schedule(new BatchRunnable(), flushAfterMs);
} catch (RejectedExecutionException e) {
hasScheduled = false;
options
.getLogger()
.log(SentryLevel.WARNING, "Logs batch processor flush task rejected", e);
}
}
}
}
@Override
public void flush(long timeoutMillis) {
maybeSchedule(true, true);
try {
pendingCount.waitTillZero(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
options.getLogger().log(SentryLevel.ERROR, "Failed to flush log events", e);
Thread.currentThread().interrupt();
}
}
private void flush() {
flushInternal();
try (final @NotNull ISentryLifecycleToken ignored = scheduleLock.acquire()) {
if (!queue.isEmpty()) {
maybeSchedule(true, false);
} else {
hasScheduled = false;
}
}
}
private void flushInternal() {
do {
flushBatch();
} while (queue.size() >= MAX_BATCH_SIZE);
}
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);
if (!logEvents.isEmpty()) {
client.captureBatchedLogEvents(new SentryLogEvents(logEvents));
for (int i = 0; i < logEvents.size(); i++) {
pendingCount.decrement();
}
}
}
private class BatchRunnable implements Runnable {
@Override
public void run() {
flush();
}
}
}