Skip to content

Commit 8330a3e

Browse files
committed
Close and flush MetricsBatchProcessor from SentryClient
1 parent 53ea75b commit 8330a3e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ public void close(final boolean isRestarting) {
16571657
try {
16581658
flush(isRestarting ? 0 : options.getShutdownTimeoutMillis());
16591659
loggerBatchProcessor.close(isRestarting);
1660+
metricsBatchProcessor.close(isRestarting);
16601661
transport.close(isRestarting);
16611662
} catch (IOException e) {
16621663
options
@@ -1684,6 +1685,7 @@ public void close(final boolean isRestarting) {
16841685
@Override
16851686
public void flush(final long timeoutMillis) {
16861687
loggerBatchProcessor.flush(timeoutMillis);
1688+
metricsBatchProcessor.flush(timeoutMillis);
16871689
transport.flush(timeoutMillis);
16881690
}
16891691

sentry/src/test/java/io/sentry/SentryClientTest.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import io.sentry.hints.Cached
1414
import io.sentry.hints.DiskFlushNotification
1515
import io.sentry.hints.TransactionEnd
1616
import io.sentry.logger.ILoggerBatchProcessor
17+
import io.sentry.logger.ILoggerBatchProcessorFactory
1718
import io.sentry.metrics.IMetricsBatchProcessor
19+
import io.sentry.metrics.IMetricsBatchProcessorFactory
1820
import io.sentry.protocol.Contexts
1921
import io.sentry.protocol.Feedback
2022
import io.sentry.protocol.Mechanism
@@ -78,6 +80,10 @@ class SentryClientTest {
7880
class Fixture {
7981
var transport = mock<ITransport>()
8082
var factory = mock<ITransportFactory>()
83+
var loggerBatchProcessor = mock<ILoggerBatchProcessor>()
84+
var loggerBatchProcessorFactory = mock<ILoggerBatchProcessorFactory>()
85+
var metricsBatchProcessor = mock<IMetricsBatchProcessor>()
86+
var metricsBatchProcessorFactory = mock<IMetricsBatchProcessorFactory>()
8187
val maxAttachmentSize: Long = (5 * 1024 * 1024).toLong()
8288
val scopes = mock<IScopes>()
8389
val sentryTracer: SentryTracer
@@ -94,12 +100,16 @@ class SentryClientTest {
94100
setLogger(mock())
95101
maxAttachmentSize = this@Fixture.maxAttachmentSize
96102
setTransportFactory(factory)
103+
logs.setLoggerBatchProcessorFactory(loggerBatchProcessorFactory)
104+
metrics.setMetricsBatchProcessorFactory(metricsBatchProcessorFactory)
97105
release = "0.0.1"
98106
isTraceSampling = true
99107
}
100108

101109
init {
102110
whenever(factory.create(any(), any())).thenReturn(transport)
111+
whenever(loggerBatchProcessorFactory.create(any(), any())).thenReturn(loggerBatchProcessor)
112+
whenever(metricsBatchProcessorFactory.create(any(), any())).thenReturn(metricsBatchProcessor)
103113
whenever(scopes.options).thenReturn(sentryOptions)
104114
sentryTracer =
105115
SentryTracer(
@@ -168,21 +178,29 @@ class SentryClientTest {
168178

169179
@Test
170180
fun `when client is closed with isRestarting false, transport waits`() {
171-
val sut = fixture.getSut()
181+
val sut = fixture.getSut { options -> options.logs.isEnabled = true }
172182
assertTrue(sut.isEnabled)
173183
sut.close(false)
174184
assertNotEquals(0, fixture.sentryOptions.shutdownTimeoutMillis)
175185
verify(fixture.transport).flush(eq(fixture.sentryOptions.shutdownTimeoutMillis))
186+
verify(fixture.loggerBatchProcessor).flush(eq(fixture.sentryOptions.shutdownTimeoutMillis))
187+
verify(fixture.metricsBatchProcessor).flush(eq(fixture.sentryOptions.shutdownTimeoutMillis))
176188
verify(fixture.transport).close(eq(false))
189+
verify(fixture.loggerBatchProcessor).close(eq(false))
190+
verify(fixture.metricsBatchProcessor).close(eq(false))
177191
}
178192

179193
@Test
180194
fun `when client is closed with isRestarting true, transport does not wait`() {
181-
val sut = fixture.getSut()
195+
val sut = fixture.getSut { options -> options.logs.isEnabled = true }
182196
assertTrue(sut.isEnabled)
183197
sut.close(true)
184198
verify(fixture.transport).flush(eq(0))
199+
verify(fixture.loggerBatchProcessor).flush(eq(0))
200+
verify(fixture.metricsBatchProcessor).flush(eq(0))
185201
verify(fixture.transport).close(eq(true))
202+
verify(fixture.loggerBatchProcessor).close(eq(true))
203+
verify(fixture.metricsBatchProcessor).close(eq(true))
186204
}
187205

188206
@Test

0 commit comments

Comments
 (0)