@@ -20,6 +20,8 @@ import io.sentry.SentryLogEvent
2020import io.sentry.SentryLogEvents
2121import io.sentry.SentryLogLevel
2222import io.sentry.SentryLongDate
23+ import io.sentry.SentryMetricsEvent
24+ import io.sentry.SentryMetricsEvents
2325import io.sentry.SentryOptions
2426import io.sentry.SentryReplayEvent
2527import 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