diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java index 961c03ae63..62589e3840 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidTransactionProfiler.java @@ -212,13 +212,6 @@ public synchronized void onTransactionStart(@NotNull ITransaction transaction) { return null; } - // We notify the data referring to this transaction that it finished - ProfilingTransactionData transactionData = - transactionMap.get(transaction.getEventId().toString()); - if (transactionData != null) { - transactionData.notifyFinish(SystemClock.elapsedRealtimeNanos(), transactionStartNanos); - } - if (transactionsCounter > 0) { transactionsCounter--; } @@ -233,6 +226,12 @@ public synchronized void onTransactionStart(@NotNull ITransaction transaction) { transactionsCounter); if (transactionsCounter != 0 && !isTimeout) { + // We notify the data referring to this transaction that it finished + ProfilingTransactionData transactionData = + transactionMap.get(transaction.getEventId().toString()); + if (transactionData != null) { + transactionData.notifyFinish(SystemClock.elapsedRealtimeNanos(), transactionStartNanos); + } return null; } diff --git a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/EnvelopeTests.kt b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/EnvelopeTests.kt index f67bc5fc0f..5d059c2053 100644 --- a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/EnvelopeTests.kt +++ b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/EnvelopeTests.kt @@ -109,19 +109,21 @@ class EnvelopeTests : BaseUiTest() { assertEquals(transaction3.eventId.toString(), transactionItem.eventId.toString()) assertEquals(profilingTraceData.transactionId, transactionItem.eventId.toString()) assertTrue(profilingTraceData.transactionName == "e2etests") + // Transaction timestamps should be all different from each other val transactions = profilingTraceData.transactions assertContains(transactions.map { t -> t.id }, transactionItem.eventId.toString()) val startTimes = transactions.map { t -> t.relativeStartNs } - val endTimes = transactions.map { t -> t.relativeEndNs } - assertEquals(0, startTimes[0]) + val endTimes = transactions.mapNotNull { t -> t.relativeEndNs } assertNotEquals(startTimes[0], startTimes[1]) assertNotEquals(startTimes[0], startTimes[2]) assertNotEquals(startTimes[1], startTimes[2]) - assertNotEquals(0, endTimes[0]) assertNotEquals(endTimes[0], endTimes[1]) assertNotEquals(endTimes[0], endTimes[2]) assertNotEquals(endTimes[1], endTimes[2]) + + // The first and last transactions should be aligned to the start/stop of profile + assertEquals(endTimes.maxOrNull()!! - startTimes.minOrNull()!!, profilingTraceData.durationNs.toLong()) } assertNoOtherEnvelopes() assertNoOtherRequests()