Skip to content

Commit

Permalink
Merge 0656c21 into c1399c1
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanosiano authored Jan 31, 2023
2 parents c1399c1 + 0656c21 commit ddb73f9
Show file tree
Hide file tree
Showing 28 changed files with 226 additions and 159 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Fix transaction performance collector oom ([#2505](https://github.com/getsentry/sentry-java/pull/2505))
- Remove authority from URLs sent to Sentry ([#2366](https://github.com/getsentry/sentry-java/pull/2366))
- Fix `sentry-bom` containing incorrect artifacts ([#2504](https://github.com/getsentry/sentry-java/pull/2504))

Expand Down
4 changes: 2 additions & 2 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class io/sentry/android/core/ActivityLifecycleIntegration : android

public final class io/sentry/android/core/AndroidCpuCollector : io/sentry/ICollector {
public fun <init> (Lio/sentry/ILogger;Lio/sentry/android/core/BuildInfoProvider;)V
public fun collect (Ljava/lang/Iterable;)V
public fun collect (Lio/sentry/PerformanceCollectionData;)V
public fun setup ()V
}

Expand All @@ -45,7 +45,7 @@ public final class io/sentry/android/core/AndroidLogger : io/sentry/ILogger {

public class io/sentry/android/core/AndroidMemoryCollector : io/sentry/ICollector {
public fun <init> ()V
public fun collect (Ljava/lang/Iterable;)V
public fun collect (Lio/sentry/PerformanceCollectionData;)V
public fun setup ()V
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void setup() {

@SuppressLint("NewApi")
@Override
public void collect(
@NotNull final Iterable<PerformanceCollectionData> performanceCollectionData) {
public void collect(final @NotNull PerformanceCollectionData performanceCollectionData) {
if (buildInfoProvider.getSdkInfoVersion() < Build.VERSION_CODES.LOLLIPOP || !isEnabled) {
return;
}
Expand All @@ -86,9 +85,7 @@ public void collect(
new CpuCollectionData(
System.currentTimeMillis(), (cpuUsagePercentage / (double) numCores) * 100.0);

for (PerformanceCollectionData data : performanceCollectionData) {
data.addCpuData(cpuData);
}
performanceCollectionData.addCpuData(cpuData);
}

/** Read the /proc/self/stat file and parses the result. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ public class AndroidMemoryCollector implements ICollector {
public void setup() {}

@Override
public void collect(@NotNull Iterable<PerformanceCollectionData> performanceCollectionData) {
public void collect(final @NotNull PerformanceCollectionData performanceCollectionData) {
long now = System.currentTimeMillis();
long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
long usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize();
MemoryCollectionData memoryData = new MemoryCollectionData(now, usedMemory, usedNativeMemory);
for (PerformanceCollectionData data : performanceCollectionData) {
data.addMemoryData(memoryData);
}
performanceCollectionData.addMemoryData(memoryData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.pm.PackageInfo;
import android.content.res.AssetManager;
import android.os.Build;
import io.sentry.DefaultTransactionPerformanceCollector;
import io.sentry.ILogger;
import io.sentry.SendFireAndForgetEnvelopeSender;
import io.sentry.SendFireAndForgetOutboxSender;
Expand Down Expand Up @@ -168,6 +169,7 @@ static void initializeIntegrationsAndProcessors(
options.addCollector(new AndroidMemoryCollector());
options.addCollector(new AndroidCpuCollector(options.getLogger(), buildInfoProvider));
}
options.setTransactionPerformanceCollector(new DefaultTransactionPerformanceCollector(options));
}

private static void installDefaultIntegrations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void onFrameMetricCollected(
@Override
public @Nullable synchronized ProfilingTraceData onTransactionFinish(
final @NotNull ITransaction transaction,
final @Nullable PerformanceCollectionData performanceCollectionData) {
final @Nullable List<PerformanceCollectionData> performanceCollectionData) {
try {
return options
.getExecutorService()
Expand All @@ -269,7 +269,7 @@ public void onFrameMetricCollected(
private @Nullable ProfilingTraceData onTransactionFinish(
final @NotNull ITransaction transaction,
final boolean isTimeout,
final @Nullable PerformanceCollectionData performanceCollectionData) {
final @Nullable List<PerformanceCollectionData> performanceCollectionData) {

// onTransactionStart() is only available since Lollipop
// and SystemClock.elapsedRealtimeNanos() since Jelly Bean
Expand Down Expand Up @@ -416,28 +416,32 @@ public void onFrameMetricCollected(
}

private void putPerformanceCollectionDataInMeasurements(
final @Nullable PerformanceCollectionData performanceCollectionData) {
final @Nullable List<PerformanceCollectionData> performanceCollectionData) {
if (performanceCollectionData != null) {
final @NotNull ArrayDeque<ProfileMeasurementValue> memoryUsageMeasurements =
new ArrayDeque<>();
new ArrayDeque<>(performanceCollectionData.size());
final @NotNull ArrayDeque<ProfileMeasurementValue> nativeMemoryUsageMeasurements =
new ArrayDeque<>();
final @NotNull ArrayDeque<ProfileMeasurementValue> cpuUsageMeasurements = new ArrayDeque<>();
for (CpuCollectionData cpuData : performanceCollectionData.getCpuData()) {
cpuUsageMeasurements.add(
new ProfileMeasurementValue(
TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis()) - transactionStartNanos,
cpuData.getCpuUsagePercentage()));
}
for (MemoryCollectionData memoryData : performanceCollectionData.getMemoryData()) {
if (memoryData.getUsedHeapMemory() > -1) {
new ArrayDeque<>(performanceCollectionData.size());
final @NotNull ArrayDeque<ProfileMeasurementValue> cpuUsageMeasurements =
new ArrayDeque<>(performanceCollectionData.size());
for (PerformanceCollectionData performanceData : performanceCollectionData) {
CpuCollectionData cpuData = performanceData.getCpuData();
MemoryCollectionData memoryData = performanceData.getMemoryData();
if (cpuData != null) {
cpuUsageMeasurements.add(
new ProfileMeasurementValue(
TimeUnit.MILLISECONDS.toNanos(cpuData.getTimestampMillis())
- transactionStartNanos,
cpuData.getCpuUsagePercentage()));
}
if (memoryData != null && memoryData.getUsedHeapMemory() > -1) {
memoryUsageMeasurements.add(
new ProfileMeasurementValue(
TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis())
- transactionStartNanos,
memoryData.getUsedHeapMemory()));
}
if (memoryData.getUsedNativeMemory() > -1) {
if (memoryData != null && memoryData.getUsedNativeMemory() > -1) {
nativeMemoryUsageMeasurements.add(
new ProfileMeasurementValue(
TimeUnit.MILLISECONDS.toNanos(memoryData.getTimestampMillis())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.test.assertNull

class AndroidCpuCollectorTest {

Expand Down Expand Up @@ -43,23 +42,22 @@ class AndroidCpuCollectorTest {
@Test
fun `collect works only after setup`() {
val data = PerformanceCollectionData()
fixture.getSut().collect(listOf(data))
fixture.getSut().collect(data)
data.commitData()
assertTrue(data.cpuData.isEmpty())
assertNull(data.cpuData)
}

@Test
fun `when collect cpu is collected`() {
val data = PerformanceCollectionData()
val collector = fixture.getSut()
collector.setup()
collector.collect(listOf(data))
collector.collect(data)
data.commitData()
val cpuData = data.cpuData
assertNotNull(data.cpuData)
assertFalse(data.cpuData.isEmpty())
assertNotEquals(0.0, cpuData[0].cpuUsagePercentage)
assertNotEquals(0, cpuData[0].timestampMillis)
assertNotNull(cpuData)
assertNotEquals(0.0, cpuData.cpuUsagePercentage)
assertNotEquals(0, cpuData.timestampMillis)
}

@Test
Expand All @@ -69,8 +67,8 @@ class AndroidCpuCollectorTest {
whenever(mockBuildInfoProvider.sdkInfoVersion).thenReturn(Build.VERSION_CODES.KITKAT)
val collector = fixture.getSut(mockBuildInfoProvider)
collector.setup()
collector.collect(listOf(data))
collector.collect(data)
data.commitData()
assertTrue(data.cpuData.isEmpty())
assertNull(data.cpuData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ class AndroidMemoryCollectorTest {

@Test
fun `when collect, both native and heap memory are collected`() {
val performanceCollectionData = PerformanceCollectionData()
val data = listOf(performanceCollectionData)
val data = PerformanceCollectionData()
val usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize()
val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory()
fixture.collector.collect(data)
performanceCollectionData.commitData()
val memoryData = performanceCollectionData.memoryData.firstOrNull()
data.commitData()
val memoryData = data.memoryData
assertNotNull(memoryData)
assertNotEquals(-1, memoryData.usedNativeMemory)
assertEquals(usedNativeMemory, memoryData.usedNativeMemory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.os.Bundle
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.DefaultTransactionPerformanceCollector
import io.sentry.ILogger
import io.sentry.MainEventProcessor
import io.sentry.SentryOptions
Expand All @@ -24,6 +25,7 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -525,4 +527,11 @@ class AndroidOptionsInitializerTest {

assertTrue { fixture.sentryOptions.collectors.any { it is AndroidCpuCollector } }
}

@Test
fun `DefaultTransactionPerformanceCollector is set to options`() {
fixture.initSut()

assertIs<DefaultTransactionPerformanceCollector>(fixture.sentryOptions.transactionPerformanceCollector)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,20 @@ class AndroidTransactionProfilerTest {
@Test
fun `profiler includes performance measurements when passed on transaction finish`() {
val profiler = fixture.getSut(context)
val memoryCollectionData = PerformanceCollectionData()
memoryCollectionData.addMemoryData(MemoryCollectionData(1, 2, 3))
memoryCollectionData.addCpuData(CpuCollectionData(1, 1.4))
memoryCollectionData.commitData()
memoryCollectionData.addMemoryData(MemoryCollectionData(2, 3, 4))
memoryCollectionData.commitData()
val performanceCollectionData = ArrayList<PerformanceCollectionData>()
var singleData = PerformanceCollectionData()
singleData.addMemoryData(MemoryCollectionData(1, 2, 3))
singleData.addCpuData(CpuCollectionData(1, 1.4))
singleData.commitData()
performanceCollectionData.add(singleData)

singleData = PerformanceCollectionData()
singleData.addMemoryData(MemoryCollectionData(2, 3, 4))
singleData.commitData()
performanceCollectionData.add(singleData)

profiler.onTransactionStart(fixture.transaction1)
val data = profiler.onTransactionFinish(fixture.transaction1, memoryCollectionData)
val data = profiler.onTransactionFinish(fixture.transaction1, performanceCollectionData)
assertContentEquals(
listOf(1.4),
data!!.measurementsMap[ProfileMeasurement.ID_CPU_USAGE]!!.values.map { it.value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class SentryAndroidOptionsTest {
override fun onTransactionStart(transaction: ITransaction) {}
override fun onTransactionFinish(
transaction: ITransaction,
memoryCollectionData: PerformanceCollectionData?
performanceCollectionData: List<PerformanceCollectionData>?
): ProfilingTraceData? = null
}
}
19 changes: 10 additions & 9 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public final class io/sentry/DateUtils {
public final class io/sentry/DefaultTransactionPerformanceCollector : io/sentry/TransactionPerformanceCollector {
public fun <init> (Lio/sentry/SentryOptions;)V
public fun start (Lio/sentry/ITransaction;)V
public fun stop (Lio/sentry/ITransaction;)Lio/sentry/PerformanceCollectionData;
public fun stop (Lio/sentry/ITransaction;)Ljava/util/List;
}

public final class io/sentry/DiagnosticLogger : io/sentry/ILogger {
Expand Down Expand Up @@ -391,7 +391,7 @@ public final class io/sentry/HubAdapter : io/sentry/IHub {
}

public abstract interface class io/sentry/ICollector {
public abstract fun collect (Ljava/lang/Iterable;)V
public abstract fun collect (Lio/sentry/PerformanceCollectionData;)V
public abstract fun setup ()V
}

Expand Down Expand Up @@ -571,7 +571,7 @@ public abstract interface class io/sentry/ITransaction : io/sentry/ISpan {
}

public abstract interface class io/sentry/ITransactionProfiler {
public abstract fun onTransactionFinish (Lio/sentry/ITransaction;Lio/sentry/PerformanceCollectionData;)Lio/sentry/ProfilingTraceData;
public abstract fun onTransactionFinish (Lio/sentry/ITransaction;Ljava/util/List;)Lio/sentry/ProfilingTraceData;
public abstract fun onTransactionStart (Lio/sentry/ITransaction;)V
}

Expand All @@ -596,7 +596,7 @@ public final class io/sentry/IpAddressUtils {

public final class io/sentry/JavaMemoryCollector : io/sentry/ICollector {
public fun <init> ()V
public fun collect (Ljava/lang/Iterable;)V
public fun collect (Lio/sentry/PerformanceCollectionData;)V
public fun setup ()V
}

Expand Down Expand Up @@ -862,12 +862,12 @@ public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
public final class io/sentry/NoOpTransactionPerformanceCollector : io/sentry/TransactionPerformanceCollector {
public static fun getInstance ()Lio/sentry/NoOpTransactionPerformanceCollector;
public fun start (Lio/sentry/ITransaction;)V
public fun stop (Lio/sentry/ITransaction;)Lio/sentry/PerformanceCollectionData;
public fun stop (Lio/sentry/ITransaction;)Ljava/util/List;
}

public final class io/sentry/NoOpTransactionProfiler : io/sentry/ITransactionProfiler {
public static fun getInstance ()Lio/sentry/NoOpTransactionProfiler;
public fun onTransactionFinish (Lio/sentry/ITransaction;Lio/sentry/PerformanceCollectionData;)Lio/sentry/ProfilingTraceData;
public fun onTransactionFinish (Lio/sentry/ITransaction;Ljava/util/List;)Lio/sentry/ProfilingTraceData;
public fun onTransactionStart (Lio/sentry/ITransaction;)V
}

Expand All @@ -892,8 +892,8 @@ public final class io/sentry/PerformanceCollectionData {
public fun addCpuData (Lio/sentry/CpuCollectionData;)V
public fun addMemoryData (Lio/sentry/MemoryCollectionData;)V
public fun commitData ()V
public fun getCpuData ()Ljava/util/List;
public fun getMemoryData ()Ljava/util/List;
public fun getCpuData ()Lio/sentry/CpuCollectionData;
public fun getMemoryData ()Lio/sentry/MemoryCollectionData;
}

public final class io/sentry/ProfilingTraceData : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
Expand Down Expand Up @@ -1646,6 +1646,7 @@ public class io/sentry/SentryOptions {
public fun setTracesSampleRate (Ljava/lang/Double;)V
public fun setTracesSampler (Lio/sentry/SentryOptions$TracesSamplerCallback;)V
public fun setTracingOrigins (Ljava/util/List;)V
public fun setTransactionPerformanceCollector (Lio/sentry/TransactionPerformanceCollector;)V
public fun setTransactionProfiler (Lio/sentry/ITransactionProfiler;)V
public fun setTransportFactory (Lio/sentry/ITransportFactory;)V
public fun setTransportGate (Lio/sentry/transport/ITransportGate;)V
Expand Down Expand Up @@ -2069,7 +2070,7 @@ public final class io/sentry/TransactionOptions {

public abstract interface class io/sentry/TransactionPerformanceCollector {
public abstract fun start (Lio/sentry/ITransaction;)V
public abstract fun stop (Lio/sentry/ITransaction;)Lio/sentry/PerformanceCollectionData;
public abstract fun stop (Lio/sentry/ITransaction;)Ljava/util/List;
}

public final class io/sentry/TypeCheckHint {
Expand Down
Loading

0 comments on commit ddb73f9

Please sign in to comment.