Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create timer in TransactionPerformanceCollector lazily #2478

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Create timer in `TransactionPerformanceCollector` lazily ([#2478](https://github.com/getsentry/sentry-java/pull/2478))

## 6.12.0

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class TransactionPerformanceCollector {
private static final long TRANSACTION_COLLECTION_INTERVAL_MILLIS = 100;
private static final long TRANSACTION_COLLECTION_TIMEOUT_MILLIS = 30000;
private final @NotNull Object timerLock = new Object();
private volatile @NotNull Timer timer = new Timer();
private volatile @Nullable Timer timer = null;
private final @NotNull Map<String, ArrayList<MemoryCollectionData>> memoryMap =
new ConcurrentHashMap<>();
private @Nullable IMemoryCollector memoryCollector = null;
Expand Down Expand Up @@ -60,6 +60,9 @@ public void start(final @NotNull ITransaction transaction) {
}
if (!isStarted.getAndSet(true)) {
synchronized (timerLock) {
if (timer == null) {
timer = new Timer(true);
}
timer.scheduleAtFixedRate(
new TimerTask() {
@Override
Expand Down Expand Up @@ -87,8 +90,10 @@ public void run() {
synchronized (timerLock) {
List<MemoryCollectionData> memoryData = memoryMap.remove(transaction.getEventId().toString());
if (memoryMap.isEmpty() && isStarted.getAndSet(false)) {
timer.cancel();
timer = new Timer();
if (timer != null) {
timer.cancel();
timer = null;
}
}
return memoryData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TransactionPerformanceCollectorTest {
lateinit var transaction2: ITransaction
val hub: IHub = mock()
val options = SentryOptions()
lateinit var mockTimer: Timer
var mockTimer: Timer? = null
var lastScheduledRunnable: Runnable? = null

val mockExecutorService = object : ISentryExecutorService {
Expand All @@ -56,7 +56,7 @@ class TransactionPerformanceCollectorTest {
transaction1 = SentryTracer(TransactionContext("", ""), hub)
transaction2 = SentryTracer(TransactionContext("", ""), hub)
val collector = TransactionPerformanceCollector(options)
val timer: Timer = collector.getProperty("timer")
val timer: Timer? = collector.getProperty("timer") ?: Timer(true)
mockTimer = spy(timer)
collector.injectForField("timer", mockTimer)
return collector
Expand All @@ -77,31 +77,31 @@ class TransactionPerformanceCollectorTest {
val collector = fixture.getSut(NoOpMemoryCollector.getInstance())
assertIs<NoOpMemoryCollector>(fixture.options.memoryCollector)
collector.start(fixture.transaction1)
verify(fixture.mockTimer, never()).scheduleAtFixedRate(any(), any<Long>(), any())
verify(fixture.mockTimer, never())!!.scheduleAtFixedRate(any(), any<Long>(), any())
}

@Test
fun `when start, timer is scheduled every 100 milliseconds`() {
val collector = fixture.getSut()
collector.start(fixture.transaction1)
verify(fixture.mockTimer).scheduleAtFixedRate(any(), any<Long>(), eq(100))
verify(fixture.mockTimer)!!.scheduleAtFixedRate(any(), any<Long>(), eq(100))
}

@Test
fun `when stop, timer is stopped`() {
val collector = fixture.getSut()
collector.start(fixture.transaction1)
collector.stop(fixture.transaction1)
verify(fixture.mockTimer).scheduleAtFixedRate(any(), any<Long>(), eq(100))
verify(fixture.mockTimer).cancel()
verify(fixture.mockTimer)!!.scheduleAtFixedRate(any(), any<Long>(), eq(100))
verify(fixture.mockTimer)!!.cancel()
}

@Test
fun `stopping a not collected transaction return null`() {
val collector = fixture.getSut()
val data = collector.stop(fixture.transaction1)
verify(fixture.mockTimer, never()).scheduleAtFixedRate(any(), any<Long>(), eq(100))
verify(fixture.mockTimer, never()).cancel()
verify(fixture.mockTimer, never())!!.scheduleAtFixedRate(any(), any<Long>(), eq(100))
verify(fixture.mockTimer, never())!!.cancel()
assertNull(data)
}

Expand All @@ -115,11 +115,11 @@ class TransactionPerformanceCollectorTest {

val data1 = collector.stop(fixture.transaction1)
// There is still a transaction running: the timer shouldn't stop now
verify(fixture.mockTimer, never()).cancel()
verify(fixture.mockTimer, never())!!.cancel()

val data2 = collector.stop(fixture.transaction2)
// There are no more transactions running: the time should stop now
verify(fixture.mockTimer).cancel()
verify(fixture.mockTimer)!!.cancel()

// The data returned by the collector is not empty
assertFalse(data1!!.isEmpty())
Expand All @@ -132,11 +132,11 @@ class TransactionPerformanceCollectorTest {
collector.start(fixture.transaction1)
// Let's sleep to make the collector get values
Thread.sleep(200)
verify(fixture.mockTimer, never()).cancel()
verify(fixture.mockTimer, never())!!.cancel()

// Let the timeout job stop the collector
fixture.lastScheduledRunnable?.run()
verify(fixture.mockTimer).cancel()
verify(fixture.mockTimer)!!.cancel()

// Data is returned even after the collector times out
val data1 = collector.stop(fixture.transaction1)
Expand Down