Skip to content

Commit 54a5d67

Browse files
committed
record client report
1 parent 004bf19 commit 54a5d67

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

sentry/src/main/java/io/sentry/logger/LoggerBatchProcessor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.logger;
22

3+
import io.sentry.DataCategory;
34
import io.sentry.ISentryClient;
45
import io.sentry.ISentryExecutorService;
56
import io.sentry.ISentryLifecycleToken;
@@ -8,8 +9,10 @@
89
import io.sentry.SentryLogEvent;
910
import io.sentry.SentryLogEvents;
1011
import io.sentry.SentryOptions;
12+
import io.sentry.clientreport.DiscardReason;
1113
import io.sentry.transport.ReusableCountLatch;
1214
import io.sentry.util.AutoClosableReentrantLock;
15+
import io.sentry.util.JsonSerializationUtils;
1316
import java.util.ArrayList;
1417
import java.util.List;
1518
import java.util.Queue;
@@ -48,6 +51,15 @@ public LoggerBatchProcessor(
4851
@Override
4952
public void add(final @NotNull SentryLogEvent logEvent) {
5053
if (pendingCount.getCount() >= MAX_QUEUE_SIZE) {
54+
options
55+
.getClientReportRecorder()
56+
.recordLostEvent(DiscardReason.QUEUE_OVERFLOW, DataCategory.LogItem);
57+
final long lostBytes =
58+
JsonSerializationUtils.byteSizeOf(
59+
options.getSerializer(), options.getLogger(), logEvent);
60+
options
61+
.getClientReportRecorder()
62+
.recordLostEvent(DiscardReason.QUEUE_OVERFLOW, DataCategory.Attachment, lostBytes);
5163
return;
5264
}
5365
pendingCount.increment();

sentry/src/test/java/io/sentry/logger/LoggerBatchProcessorTest.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package io.sentry.logger
22

3+
import io.sentry.DataCategory
34
import io.sentry.ISentryClient
45
import io.sentry.SentryLogEvent
56
import io.sentry.SentryLogEvents
67
import io.sentry.SentryLogLevel
78
import io.sentry.SentryNanotimeDate
89
import io.sentry.SentryOptions
10+
import io.sentry.clientreport.ClientReportTestHelper
11+
import io.sentry.clientreport.DiscardReason
12+
import io.sentry.clientreport.DiscardedEvent
913
import io.sentry.protocol.SentryId
1014
import io.sentry.test.DeferredExecutorService
1115
import io.sentry.test.injectForField
16+
import io.sentry.util.JsonSerializationUtils
1217
import kotlin.test.Test
1318
import kotlin.test.assertEquals
1419
import kotlin.test.assertFalse
@@ -54,4 +59,43 @@ class LoggerBatchProcessorTest {
5459
assertTrue(log1000Found, "Log 1000 should have been sent")
5560
assertFalse(log1001Found, "Log 1001 should not have been sent")
5661
}
62+
63+
@Test
64+
fun `records client report when log event is dropped due to queue overflow`() {
65+
// given
66+
val mockClient = mock<ISentryClient>()
67+
val mockExecutor = DeferredExecutorService()
68+
val options = SentryOptions()
69+
val processor = LoggerBatchProcessor(options, mockClient)
70+
processor.injectForField("executorService", mockExecutor)
71+
72+
// fill the queue to MAX_QUEUE_SIZE
73+
for (i in 1..1000) {
74+
val logEvent =
75+
SentryLogEvent(SentryId(), SentryNanotimeDate(), "log message $i", SentryLogLevel.INFO)
76+
processor.add(logEvent)
77+
}
78+
79+
// add one more log event that should be dropped
80+
val droppedLogEvent =
81+
SentryLogEvent(SentryId(), SentryNanotimeDate(), "dropped log", SentryLogLevel.INFO)
82+
processor.add(droppedLogEvent)
83+
84+
// calculate expected bytes for the dropped log event
85+
val expectedBytes =
86+
JsonSerializationUtils.byteSizeOf(options.serializer, options.logger, droppedLogEvent)
87+
88+
// verify that a client report was recorded for the dropped log item and bytes
89+
val expectedEvents =
90+
mutableListOf(
91+
DiscardedEvent(DiscardReason.QUEUE_OVERFLOW.reason, DataCategory.LogItem.category, 1),
92+
DiscardedEvent(
93+
DiscardReason.QUEUE_OVERFLOW.reason,
94+
DataCategory.Attachment.category,
95+
expectedBytes,
96+
),
97+
)
98+
99+
ClientReportTestHelper.assertClientReport(options.clientReportRecorder, expectedEvents)
100+
}
57101
}

0 commit comments

Comments
 (0)