Skip to content

Commit

Permalink
Add timestamp when a profile starts (#3442)
Browse files Browse the repository at this point in the history
* added start timestamp in ProfileStartData when a profile starts
* added the timestamp in ProfilingTraceData and send it in the json payload
  • Loading branch information
stefanosiano committed Jun 18, 2024
1 parent 5ad752a commit c17f259
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 4 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

- Add timestamp when a profile starts ([#3442](https://github.com/getsentry/sentry-java/pull/3442))
- Move fragment auto span finish to onFragmentStarted ([#3424](https://github.com/getsentry/sentry-java/pull/3424))
- Remove profiling timeout logic and disable profiling on API 21 ([#3478](https://github.com/getsentry/sentry-java/pull/3478))
- Properly reset metric flush flag on metric emission ([#3493](https://github.com/getsentry/sentry-java/pull/3493))
Expand Down
3 changes: 2 additions & 1 deletion sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public class io/sentry/android/core/AndroidProfiler$ProfileEndData {
public class io/sentry/android/core/AndroidProfiler$ProfileStartData {
public final field startCpuMillis J
public final field startNanos J
public fun <init> (JJ)V
public final field startTimestamp Ljava/util/Date;
public fun <init> (JJLjava/util/Date;)V
}

public final class io/sentry/android/core/AnrIntegration : io/sentry/Integration, java/io/Closeable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Process;
import android.os.SystemClock;
import io.sentry.CpuCollectionData;
import io.sentry.DateUtils;
import io.sentry.ILogger;
import io.sentry.ISentryExecutorService;
import io.sentry.MemoryCollectionData;
Expand All @@ -17,6 +18,7 @@
import io.sentry.util.Objects;
import java.io.File;
import java.util.ArrayDeque;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,10 +35,13 @@ public class AndroidProfiler {
public static class ProfileStartData {
public final long startNanos;
public final long startCpuMillis;
public final @NotNull Date startTimestamp;

public ProfileStartData(final long startNanos, final long startCpuMillis) {
public ProfileStartData(
final long startNanos, final long startCpuMillis, final @NotNull Date startTimestamp) {
this.startNanos = startNanos;
this.startCpuMillis = startCpuMillis;
this.startTimestamp = startTimestamp;
}
}

Expand Down Expand Up @@ -190,6 +195,7 @@ public void onFrameMetricCollected(
}

profileStartNanos = SystemClock.elapsedRealtimeNanos();
final @NotNull Date profileStartTimestamp = DateUtils.getCurrentDateTime();
long profileStartCpuMillis = Process.getElapsedCpuTime();

// We don't make any check on the file existence or writeable state, because we don't want to
Expand All @@ -201,7 +207,7 @@ public void onFrameMetricCollected(
// tests)
Debug.startMethodTracingSampling(traceFile.getPath(), BUFFER_SIZE_BYTES, intervalUs);
isRunning = true;
return new ProfileStartData(profileStartNanos, profileStartCpuMillis);
return new ProfileStartData(profileStartNanos, profileStartCpuMillis, profileStartTimestamp);
} catch (Throwable e) {
endAndCollect(false, null);
logger.log(SentryLevel.ERROR, "Unable to start a profile: ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build;
import android.os.Process;
import android.os.SystemClock;
import io.sentry.DateUtils;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.ILogger;
Expand All @@ -24,6 +25,7 @@
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
import io.sentry.util.Objects;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -44,6 +46,7 @@ final class AndroidTransactionProfiler implements ITransactionProfiler {
private @Nullable AndroidProfiler profiler = null;
private long profileStartNanos;
private long profileStartCpuMillis;
private @NotNull Date profileStartTimestamp;

/**
* @deprecated please use a constructor that doesn't takes a {@link IHub} instead, as it would be
Expand Down Expand Up @@ -95,6 +98,7 @@ public AndroidTransactionProfiler(
this.profilingTracesHz = profilingTracesHz;
this.executorService =
Objects.requireNonNull(executorService, "The ISentryExecutorService is required.");
this.profileStartTimestamp = DateUtils.getCurrentDateTime();
}

private void init() {
Expand Down Expand Up @@ -165,6 +169,7 @@ private boolean onFirstStart() {
}
profileStartNanos = startData.startNanos;
profileStartCpuMillis = startData.startCpuMillis;
profileStartTimestamp = startData.startTimestamp;
return true;
}

Expand Down Expand Up @@ -275,6 +280,7 @@ public synchronized void bindTransaction(final @NotNull ITransaction transaction
// done in the background when the trace file is read
return new ProfilingTraceData(
endData.traceFile,
profileStartTimestamp,
transactionList,
transactionName,
transactionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class AndroidProfilerTest {
val endData = profiler.endAndCollect(false, null)
assertNotNull(startData?.startNanos)
assertNotNull(startData?.startCpuMillis)
assertNotNull(startData?.startTimestamp)
assertNotNull(endData?.endNanos)
assertNotNull(endData?.endCpuMillis)
}
Expand Down
5 changes: 4 additions & 1 deletion sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ public final class io/sentry/ProfilingTraceData : io/sentry/JsonSerializable, io
public static final field TRUNCATION_REASON_NORMAL Ljava/lang/String;
public static final field TRUNCATION_REASON_TIMEOUT Ljava/lang/String;
public fun <init> (Ljava/io/File;Lio/sentry/ITransaction;)V
public fun <init> (Ljava/io/File;Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/util/concurrent/Callable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
public fun <init> (Ljava/io/File;Ljava/util/Date;Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/util/concurrent/Callable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
public fun getAndroidApiLevel ()I
public fun getBuildId ()Ljava/lang/String;
public fun getCpuArchitecture ()Ljava/lang/String;
Expand All @@ -1439,6 +1439,7 @@ public final class io/sentry/ProfilingTraceData : io/sentry/JsonSerializable, io
public fun getProfileId ()Ljava/lang/String;
public fun getRelease ()Ljava/lang/String;
public fun getSampledProfile ()Ljava/lang/String;
public fun getTimestamp ()Ljava/util/Date;
public fun getTraceFile ()Ljava/io/File;
public fun getTraceId ()Ljava/lang/String;
public fun getTransactionId ()Ljava/lang/String;
Expand All @@ -1465,6 +1466,7 @@ public final class io/sentry/ProfilingTraceData : io/sentry/JsonSerializable, io
public fun setProfileId (Ljava/lang/String;)V
public fun setRelease (Ljava/lang/String;)V
public fun setSampledProfile (Ljava/lang/String;)V
public fun setTimestamp (Ljava/util/Date;)V
public fun setTraceId (Ljava/lang/String;)V
public fun setTransactionId (Ljava/lang/String;)V
public fun setTransactionName (Ljava/lang/String;)V
Expand Down Expand Up @@ -1499,6 +1501,7 @@ public final class io/sentry/ProfilingTraceData$JsonKeys {
public static final field PROFILE_ID Ljava/lang/String;
public static final field RELEASE Ljava/lang/String;
public static final field SAMPLED_PROFILE Ljava/lang/String;
public static final field TIMESTAMP Ljava/lang/String;
public static final field TRACE_ID Ljava/lang/String;
public static final field TRANSACTION_ID Ljava/lang/String;
public static final field TRANSACTION_LIST Ljava/lang/String;
Expand Down
21 changes: 21 additions & 0 deletions sentry/src/main/java/io/sentry/ProfilingTraceData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -64,6 +65,7 @@ public final class ProfilingTraceData implements JsonUnknown, JsonSerializable {
private @NotNull String profileId;
private @NotNull String environment;
private @NotNull String truncationReason;
private @NotNull Date timestamp;
private final @NotNull Map<String, ProfileMeasurement> measurementsMap;

// Stacktrace (file)
Expand All @@ -78,6 +80,7 @@ public ProfilingTraceData(
final @NotNull File traceFile, final @NotNull ITransaction transaction) {
this(
traceFile,
DateUtils.getCurrentDateTime(),
new ArrayList<>(),
transaction.getName(),
transaction.getEventId().toString(),
Expand All @@ -101,6 +104,7 @@ public ProfilingTraceData(

public ProfilingTraceData(
@NotNull File traceFile,
@NotNull Date profileStartTimestamp,
@NotNull List<ProfilingTransactionData> transactions,
@NotNull String transactionName,
@NotNull String transactionId,
Expand All @@ -120,6 +124,7 @@ public ProfilingTraceData(
@NotNull String truncationReason,
final @NotNull Map<String, ProfileMeasurement> measurementsMap) {
this.traceFile = traceFile;
this.timestamp = profileStartTimestamp;
this.cpuArchitecture = cpuArchitecture;
this.deviceCpuFrequenciesReader = deviceCpuFrequenciesReader;

Expand Down Expand Up @@ -262,6 +267,10 @@ public boolean isDeviceIsEmulator() {
return truncationReason;
}

public @NotNull Date getTimestamp() {
return timestamp;
}

public @NotNull Map<String, ProfileMeasurement> getMeasurementsMap() {
return measurementsMap;
}
Expand Down Expand Up @@ -306,6 +315,10 @@ public void setDevicePhysicalMemoryBytes(final @NotNull String devicePhysicalMem
this.devicePhysicalMemoryBytes = devicePhysicalMemoryBytes;
}

public void setTimestamp(final @NotNull Date timestamp) {
this.timestamp = timestamp;
}

public void setTruncationReason(final @NotNull String truncationReason) {
this.truncationReason = truncationReason;
}
Expand Down Expand Up @@ -386,6 +399,7 @@ public static final class JsonKeys {
public static final String SAMPLED_PROFILE = "sampled_profile";
public static final String TRUNCATION_REASON = "truncation_reason";
public static final String MEASUREMENTS = "measurements";
public static final String TIMESTAMP = "timestamp";
}

@Override
Expand Down Expand Up @@ -422,6 +436,7 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger
writer.name(JsonKeys.SAMPLED_PROFILE).value(sampledProfile);
}
writer.name(JsonKeys.MEASUREMENTS).value(logger, measurementsMap);
writer.name(JsonKeys.TIMESTAMP).value(logger, timestamp);
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
Expand Down Expand Up @@ -602,6 +617,12 @@ public static final class Deserializer implements JsonDeserializer<ProfilingTrac
data.measurementsMap.putAll(measurements);
}
break;
case JsonKeys.TIMESTAMP:
Date timestamp = reader.nextDateOrNull(logger);
if (timestamp != null) {
data.timestamp = timestamp;
}
break;
case JsonKeys.SAMPLED_PROFILE:
String sampledProfile = reader.nextStringOrNull();
if (sampledProfile != null) {
Expand Down
5 changes: 5 additions & 0 deletions sentry/src/test/java/io/sentry/JsonSerializerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ class JsonSerializerTest {
@Test
fun `serializes profilingTraceData`() {
val profilingTraceData = ProfilingTraceData(fixture.traceFile, NoOpTransaction.getInstance())
val now = Date()
profilingTraceData.androidApiLevel = 21
profilingTraceData.deviceLocale = "deviceLocale"
profilingTraceData.deviceManufacturer = "deviceManufacturer"
Expand All @@ -503,6 +504,7 @@ class JsonSerializerTest {
profilingTraceData.deviceCpuFrequencies = listOf(1, 2, 3, 4)
profilingTraceData.devicePhysicalMemoryBytes = "2000000"
profilingTraceData.buildId = "buildId"
profilingTraceData.timestamp = now
profilingTraceData.transactions = listOf(
ProfilingTransactionData(NoOpTransaction.getInstance(), 1, 2),
ProfilingTransactionData(NoOpTransaction.getInstance(), 2, 3)
Expand Down Expand Up @@ -559,6 +561,7 @@ class JsonSerializerTest {
assertEquals("2000000", element["device_physical_memory_bytes"] as String)
assertEquals("android", element["platform"] as String)
assertEquals("buildId", element["build_id"] as String)
assertEquals(DateUtils.getTimestamp(now), element["timestamp"] as String)
assertEquals(
listOf(
mapOf(
Expand Down Expand Up @@ -655,6 +658,7 @@ class JsonSerializerTest {
"device_physical_memory_bytes":"2000000",
"platform":"android",
"build_id":"buildId",
"timestamp":"2024-05-24T12:52:03.561Z",
"transactions":[
{
"id":"id",
Expand Down Expand Up @@ -729,6 +733,7 @@ class JsonSerializerTest {
assertEquals("2000000", profilingTraceData.devicePhysicalMemoryBytes)
assertEquals("android", profilingTraceData.platform)
assertEquals("buildId", profilingTraceData.buildId)
assertEquals(DateUtils.getDateTime("2024-05-24T12:52:03.561Z"), profilingTraceData.timestamp)
val expectedTransactions = listOf(
ProfilingTransactionData().apply {
id = "id"
Expand Down

0 comments on commit c17f259

Please sign in to comment.