|
1 | 1 | package io.sentry |
2 | 2 |
|
| 3 | +import com.google.common.truth.Truth.assertThat |
3 | 4 | import io.sentry.Scope.IWithPropagationContext |
4 | 5 | import io.sentry.SentryLevel.WARNING |
5 | 6 | import io.sentry.Session.State.Crashed |
@@ -474,6 +475,48 @@ class SentryClientTest { |
474 | 475 | ) |
475 | 476 | } |
476 | 477 |
|
| 478 | + @Test |
| 479 | + fun `throwing log processor drops log and stops callbacks`() { |
| 480 | + val scope = createScope() |
| 481 | + val logEvent = SentryLogEvent(SentryId(), SentryNanotimeDate(), "message", SentryLogLevel.WARN) |
| 482 | + val logEventNumberOfBytes = |
| 483 | + JsonSerializationUtils.byteSizeOf( |
| 484 | + fixture.sentryOptions.serializer, |
| 485 | + fixture.sentryOptions.logger, |
| 486 | + logEvent, |
| 487 | + ) |
| 488 | + val throwingProcessor = mock<EventProcessor>() |
| 489 | + val nextProcessor = mock<EventProcessor>() |
| 490 | + val beforeSend = mock<SentryOptions.Logs.BeforeSendLogCallback>() |
| 491 | + val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 492 | + whenever(throwingProcessor.process(any<SentryLogEvent>())) |
| 493 | + .thenThrow(IllegalStateException("test")) |
| 494 | + scope.addEventProcessor(throwingProcessor) |
| 495 | + scope.addEventProcessor(nextProcessor) |
| 496 | + fixture.sentryOptions.logs.beforeSend = beforeSend |
| 497 | + fixture.sentryOptions.onDiscard = onDiscard |
| 498 | + |
| 499 | + fixture.getSut().captureLog(logEvent, scope) |
| 500 | + |
| 501 | + verify(nextProcessor, never()).process(any<SentryLogEvent>()) |
| 502 | + verify(beforeSend, never()).execute(any()) |
| 503 | + verify(fixture.loggerBatchProcessor, never()).add(any()) |
| 504 | + assertClientReport( |
| 505 | + fixture.sentryOptions.clientReportRecorder, |
| 506 | + listOf( |
| 507 | + DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.LogItem.category, 1), |
| 508 | + DiscardedEvent( |
| 509 | + DiscardReason.CALLBACK_ERROR.reason, |
| 510 | + DataCategory.LogByte.category, |
| 511 | + logEventNumberOfBytes, |
| 512 | + ), |
| 513 | + ), |
| 514 | + ) |
| 515 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.LogItem, 1) |
| 516 | + verify(onDiscard) |
| 517 | + .execute(DiscardReason.CALLBACK_ERROR, DataCategory.LogByte, logEventNumberOfBytes) |
| 518 | + } |
| 519 | + |
477 | 520 | @Test |
478 | 521 | fun `when beforeSendLog is returns new instance, new instance is sent`() { |
479 | 522 | val scope = createScope() |
@@ -595,6 +638,56 @@ class SentryClientTest { |
595 | 638 | ) |
596 | 639 | } |
597 | 640 |
|
| 641 | + @Test |
| 642 | + fun `throwing metric processor drops metric and stops callbacks`() { |
| 643 | + val scope = createScope() |
| 644 | + val metricsEvent = SentryMetricsEvent(SentryId(), SentryNanotimeDate(), "name", "gauge", 123.0) |
| 645 | + val metricsEventNumberOfBytes = |
| 646 | + JsonSerializationUtils.byteSizeOf( |
| 647 | + fixture.sentryOptions.serializer, |
| 648 | + fixture.sentryOptions.logger, |
| 649 | + metricsEvent, |
| 650 | + ) |
| 651 | + val throwingProcessor = mock<EventProcessor>() |
| 652 | + val nextProcessor = mock<EventProcessor>() |
| 653 | + val beforeSend = mock<SentryOptions.Metrics.BeforeSendMetricCallback>() |
| 654 | + val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 655 | + whenever(throwingProcessor.process(any<SentryMetricsEvent>(), anyOrNull())) |
| 656 | + .thenThrow(IllegalStateException("test")) |
| 657 | + scope.addEventProcessor(throwingProcessor) |
| 658 | + scope.addEventProcessor(nextProcessor) |
| 659 | + fixture.sentryOptions.metrics.beforeSend = beforeSend |
| 660 | + fixture.sentryOptions.onDiscard = onDiscard |
| 661 | + |
| 662 | + fixture.getSut().captureMetric(metricsEvent, scope, null) |
| 663 | + |
| 664 | + verify(nextProcessor, never()).process(any<SentryMetricsEvent>(), anyOrNull()) |
| 665 | + verify(beforeSend, never()).execute(any(), anyOrNull()) |
| 666 | + verify(fixture.metricsBatchProcessor, never()).add(any()) |
| 667 | + assertClientReport( |
| 668 | + fixture.sentryOptions.clientReportRecorder, |
| 669 | + listOf( |
| 670 | + DiscardedEvent( |
| 671 | + DiscardReason.CALLBACK_ERROR.reason, |
| 672 | + DataCategory.TraceMetric.category, |
| 673 | + 1, |
| 674 | + ), |
| 675 | + DiscardedEvent( |
| 676 | + DiscardReason.CALLBACK_ERROR.reason, |
| 677 | + DataCategory.TraceMetricByte.category, |
| 678 | + metricsEventNumberOfBytes, |
| 679 | + ), |
| 680 | + ), |
| 681 | + ) |
| 682 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.TraceMetric, 1) |
| 683 | + verify(onDiscard) |
| 684 | + .execute( |
| 685 | + DiscardReason.CALLBACK_ERROR, |
| 686 | + DataCategory.TraceMetricByte, |
| 687 | + metricsEventNumberOfBytes, |
| 688 | + ) |
| 689 | + } |
| 690 | + |
598 | 691 | @Test |
599 | 692 | fun `when beforeSendMetric is returns new instance, new instance is sent`() { |
600 | 693 | val scope = createScope() |
@@ -1242,6 +1335,42 @@ class SentryClientTest { |
1242 | 1335 | ) |
1243 | 1336 | } |
1244 | 1337 |
|
| 1338 | + @Test |
| 1339 | + fun `throwing transaction processor drops transaction and stops callbacks`() { |
| 1340 | + val throwingProcessor = mock<EventProcessor>() |
| 1341 | + val nextProcessor = mock<EventProcessor>() |
| 1342 | + val beforeSend = mock<SentryOptions.BeforeSendTransactionCallback>() |
| 1343 | + val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 1344 | + whenever(throwingProcessor.process(any<SentryTransaction>(), anyOrNull())) |
| 1345 | + .thenThrow(IllegalStateException("test")) |
| 1346 | + fixture.sentryOptions.addEventProcessor(throwingProcessor) |
| 1347 | + fixture.sentryOptions.addEventProcessor(nextProcessor) |
| 1348 | + fixture.sentryOptions.beforeSendTransaction = beforeSend |
| 1349 | + fixture.sentryOptions.onDiscard = onDiscard |
| 1350 | + |
| 1351 | + val id = |
| 1352 | + fixture |
| 1353 | + .getSut() |
| 1354 | + .captureTransaction( |
| 1355 | + SentryTransaction(fixture.sentryTracer), |
| 1356 | + fixture.sentryTracer.traceContext(), |
| 1357 | + ) |
| 1358 | + |
| 1359 | + assertThat(id).isEqualTo(SentryId.EMPTY_ID) |
| 1360 | + verify(nextProcessor, never()).process(any<SentryTransaction>(), anyOrNull()) |
| 1361 | + verify(beforeSend, never()).execute(any(), anyOrNull()) |
| 1362 | + verify(fixture.transport, never()).send(any(), anyOrNull()) |
| 1363 | + assertClientReport( |
| 1364 | + fixture.sentryOptions.clientReportRecorder, |
| 1365 | + listOf( |
| 1366 | + DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Transaction.category, 1), |
| 1367 | + DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Span.category, 2), |
| 1368 | + ), |
| 1369 | + ) |
| 1370 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) |
| 1371 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 2) |
| 1372 | + } |
| 1373 | + |
1245 | 1374 | @Test |
1246 | 1375 | fun `transaction dropped by ignoredTransactions is recorded`() { |
1247 | 1376 | fixture.sentryOptions.setIgnoredTransactions(listOf("a-transaction")) |
@@ -1927,10 +2056,29 @@ class SentryClientTest { |
1927 | 2056 | } |
1928 | 2057 |
|
1929 | 2058 | @Test |
1930 | | - fun `exception thrown by an event processor is handled gracefully`() { |
1931 | | - fixture.sentryOptions.addEventProcessor(eventProcessorThrows()) |
1932 | | - val sut = fixture.getSut() |
1933 | | - sut.captureEvent(SentryEvent()) |
| 2059 | + fun `exception thrown by an event processor drops event and stops callbacks`() { |
| 2060 | + val throwingProcessor = mock<EventProcessor>() |
| 2061 | + val nextProcessor = mock<EventProcessor>() |
| 2062 | + val beforeSend = mock<SentryOptions.BeforeSendCallback>() |
| 2063 | + val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 2064 | + whenever(throwingProcessor.process(any<SentryEvent>(), anyOrNull())) |
| 2065 | + .thenThrow(IllegalStateException("test")) |
| 2066 | + fixture.sentryOptions.addEventProcessor(throwingProcessor) |
| 2067 | + fixture.sentryOptions.addEventProcessor(nextProcessor) |
| 2068 | + fixture.sentryOptions.beforeSend = beforeSend |
| 2069 | + fixture.sentryOptions.onDiscard = onDiscard |
| 2070 | + |
| 2071 | + val id = fixture.getSut().captureEvent(SentryEvent()) |
| 2072 | + |
| 2073 | + assertThat(id).isEqualTo(SentryId.EMPTY_ID) |
| 2074 | + verify(nextProcessor, never()).process(any<SentryEvent>(), anyOrNull()) |
| 2075 | + verify(beforeSend, never()).execute(any(), anyOrNull()) |
| 2076 | + verify(fixture.transport, never()).send(any(), anyOrNull()) |
| 2077 | + assertClientReport( |
| 2078 | + fixture.sentryOptions.clientReportRecorder, |
| 2079 | + listOf(DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Error.category, 1)), |
| 2080 | + ) |
| 2081 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Error, 1) |
1934 | 2082 | } |
1935 | 2083 |
|
1936 | 2084 | @Test |
@@ -3524,6 +3672,32 @@ class SentryClientTest { |
3524 | 3672 | verify(onDiscardMock, times(1)).execute(DiscardReason.EVENT_PROCESSOR, DataCategory.Replay, 1) |
3525 | 3673 | } |
3526 | 3674 |
|
| 3675 | + @Test |
| 3676 | + fun `throwing replay processor drops replay and stops callbacks`() { |
| 3677 | + val throwingProcessor = mock<EventProcessor>() |
| 3678 | + val nextProcessor = mock<EventProcessor>() |
| 3679 | + val beforeSend = mock<SentryOptions.BeforeSendReplayCallback>() |
| 3680 | + val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 3681 | + whenever(throwingProcessor.process(any<SentryReplayEvent>(), anyOrNull())) |
| 3682 | + .thenThrow(IllegalStateException("test")) |
| 3683 | + fixture.sentryOptions.addEventProcessor(throwingProcessor) |
| 3684 | + fixture.sentryOptions.addEventProcessor(nextProcessor) |
| 3685 | + fixture.sentryOptions.beforeSendReplay = beforeSend |
| 3686 | + fixture.sentryOptions.onDiscard = onDiscard |
| 3687 | + |
| 3688 | + val id = fixture.getSut().captureReplayEvent(createReplayEvent(), createScope(), null) |
| 3689 | + |
| 3690 | + assertThat(id).isEqualTo(SentryId.EMPTY_ID) |
| 3691 | + verify(nextProcessor, never()).process(any<SentryReplayEvent>(), anyOrNull()) |
| 3692 | + verify(beforeSend, never()).execute(any(), anyOrNull()) |
| 3693 | + verify(fixture.transport, never()).send(any(), anyOrNull()) |
| 3694 | + assertClientReport( |
| 3695 | + fixture.sentryOptions.clientReportRecorder, |
| 3696 | + listOf(DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Replay.category, 1)), |
| 3697 | + ) |
| 3698 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Replay, 1) |
| 3699 | + } |
| 3700 | + |
3527 | 3701 | @Test |
3528 | 3702 | fun `calls captureReplay on replay controller for error events`() { |
3529 | 3703 | var called = false |
@@ -4086,6 +4260,34 @@ class SentryClientTest { |
4086 | 4260 | verify(onDiscardMock, times(1)).execute(DiscardReason.EVENT_PROCESSOR, DataCategory.Feedback, 1) |
4087 | 4261 | } |
4088 | 4262 |
|
| 4263 | + @Test |
| 4264 | + fun `throwing feedback processor drops feedback and stops callbacks`() { |
| 4265 | + val throwingProcessor = mock<EventProcessor>() |
| 4266 | + val nextProcessor = mock<EventProcessor>() |
| 4267 | + val beforeSend = mock<SentryOptions.BeforeSendCallback>() |
| 4268 | + val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 4269 | + whenever(throwingProcessor.process(any<SentryEvent>(), anyOrNull())) |
| 4270 | + .thenThrow(IllegalStateException("test")) |
| 4271 | + fixture.sentryOptions.addEventProcessor(throwingProcessor) |
| 4272 | + fixture.sentryOptions.addEventProcessor(nextProcessor) |
| 4273 | + fixture.sentryOptions.beforeSendFeedback = beforeSend |
| 4274 | + fixture.sentryOptions.onDiscard = onDiscard |
| 4275 | + |
| 4276 | + val id = fixture.getSut().captureFeedback(Feedback("message"), null, createScope()) |
| 4277 | + |
| 4278 | + assertThat(id).isEqualTo(SentryId.EMPTY_ID) |
| 4279 | + verify(nextProcessor, never()).process(any<SentryEvent>(), anyOrNull()) |
| 4280 | + verify(beforeSend, never()).execute(any(), anyOrNull()) |
| 4281 | + verify(fixture.transport, never()).send(any(), anyOrNull()) |
| 4282 | + assertClientReport( |
| 4283 | + fixture.sentryOptions.clientReportRecorder, |
| 4284 | + listOf( |
| 4285 | + DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Feedback.category, 1) |
| 4286 | + ), |
| 4287 | + ) |
| 4288 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Feedback, 1) |
| 4289 | + } |
| 4290 | + |
4089 | 4291 | // endregion |
4090 | 4292 |
|
4091 | 4293 | private fun givenScopeWithStartedSession( |
@@ -4352,14 +4554,6 @@ class SentryClientTest { |
4352 | 4554 | override fun timestamp(): Long? = null |
4353 | 4555 | } |
4354 | 4556 |
|
4355 | | - private fun eventProcessorThrows(): EventProcessor { |
4356 | | - return object : EventProcessor { |
4357 | | - override fun process(event: SentryEvent, hint: Hint): SentryEvent? { |
4358 | | - throw Throwable() |
4359 | | - } |
4360 | | - } |
4361 | | - } |
4362 | | - |
4363 | 4557 | private class BackfillableHint : Backfillable { |
4364 | 4558 | override fun shouldEnrich(): Boolean = false |
4365 | 4559 | } |
|
0 commit comments