Skip to content

Commit 640e2d4

Browse files
committed
Record client report for discarded metrics envelope item
1 parent cc1c361 commit 640e2d4

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

sentry/src/main/java/io/sentry/clientreport/ClientReportRecorder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import io.sentry.SentryLevel;
99
import io.sentry.SentryLogEvent;
1010
import io.sentry.SentryLogEvents;
11+
import io.sentry.SentryMetricsEvent;
12+
import io.sentry.SentryMetricsEvents;
1113
import io.sentry.SentryOptions;
1214
import io.sentry.protocol.SentrySpan;
1315
import io.sentry.protocol.SentryTransaction;
@@ -115,6 +117,19 @@ public void recordLostEnvelopeItem(
115117
} else {
116118
options.getLogger().log(SentryLevel.ERROR, "Unable to parse lost logs envelope item.");
117119
}
120+
} else if (itemCategory.equals(DataCategory.TraceMetric)) {
121+
final @Nullable SentryMetricsEvents metrics =
122+
envelopeItem.getMetrics(options.getSerializer());
123+
if (metrics != null) {
124+
final @NotNull List<SentryMetricsEvent> items = metrics.getItems();
125+
final long count = items.size();
126+
recordLostEventInternal(reason.getReason(), itemCategory.getCategory(), count);
127+
executeOnDiscard(reason, itemCategory, count);
128+
} else {
129+
options
130+
.getLogger()
131+
.log(SentryLevel.ERROR, "Unable to parse lost metrics envelope item.");
132+
}
118133
} else {
119134
recordLostEventInternal(reason.getReason(), itemCategory.getCategory(), 1L);
120135
executeOnDiscard(reason, itemCategory, 1L);

sentry/src/test/java/io/sentry/clientreport/ClientReportTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import io.sentry.SentryLogEvent
2020
import io.sentry.SentryLogEvents
2121
import io.sentry.SentryLogLevel
2222
import io.sentry.SentryLongDate
23+
import io.sentry.SentryMetricsEvent
24+
import io.sentry.SentryMetricsEvents
2325
import io.sentry.SentryOptions
2426
import io.sentry.SentryReplayEvent
2527
import io.sentry.SentryTracer
@@ -382,6 +384,36 @@ class ClientReportTest {
382384
assertEquals(226, logByte.quantity)
383385
}
384386

387+
@Test
388+
fun `recording lost client report counts metric entries`() {
389+
val onDiscardMock = mock<SentryOptions.OnDiscardCallback>()
390+
givenClientReportRecorder { options -> options.onDiscard = onDiscardMock }
391+
392+
val envelope =
393+
testHelper.newEnvelope(
394+
SentryEnvelopeItem.fromMetrics(
395+
opts.serializer,
396+
SentryMetricsEvents(
397+
listOf(
398+
SentryMetricsEvent(SentryId(), SentryLongDate(1), "metric1", "counter", 1.0),
399+
SentryMetricsEvent(SentryId(), SentryLongDate(2), "metric2", "gauge", 2.0),
400+
SentryMetricsEvent(SentryId(), SentryLongDate(3), "metric3", "distribution", 3.0),
401+
)
402+
),
403+
)
404+
)
405+
406+
clientReportRecorder.recordLostEnvelopeItem(DiscardReason.NETWORK_ERROR, envelope.items.first())
407+
408+
verify(onDiscardMock, times(1))
409+
.execute(DiscardReason.NETWORK_ERROR, DataCategory.TraceMetric, 3)
410+
411+
val clientReport = clientReportRecorder.resetCountsAndGenerateClientReport()
412+
val metricItem =
413+
clientReport!!.discardedEvents!!.first { it.category == DataCategory.TraceMetric.category }
414+
assertEquals(3, metricItem.quantity)
415+
}
416+
385417
private fun givenClientReportRecorder(
386418
callback: Sentry.OptionsConfiguration<SentryOptions>? = null
387419
) {

0 commit comments

Comments
 (0)