Skip to content

Commit

Permalink
Update profiling release field (#2371)
Browse files Browse the repository at this point in the history
* updated profile schema:
- replaced versionCode with an empty string on new envelopes
- replaced versionName with release string
* added test
  • Loading branch information
stefanosiano committed Dec 2, 2022
1 parent 31f3e4c commit 34a8bfd
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 66 deletions.
Expand Up @@ -6,7 +6,6 @@
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Debug;
import android.os.Process;
Expand Down Expand Up @@ -61,7 +60,6 @@ final class AndroidTransactionProfiler implements ITransactionProfiler {
private final @NotNull SentryAndroidOptions options;
private final @NotNull IHub hub;
private final @NotNull BuildInfoProvider buildInfoProvider;
private final @Nullable PackageInfo packageInfo;
private long transactionStartNanos = 0;
private long profileStartCpuMillis = 0;
private boolean isInitialized = false;
Expand Down Expand Up @@ -103,7 +101,6 @@ public AndroidTransactionProfiler(
Objects.requireNonNull(frameMetricsCollector, "SentryFrameMetricsCollector is required");
this.buildInfoProvider =
Objects.requireNonNull(buildInfoProvider, "The BuildInfoProvider is required.");
this.packageInfo = ContextUtils.getPackageInfo(context, options.getLogger(), buildInfoProvider);
}

private void init() {
Expand Down Expand Up @@ -316,14 +313,8 @@ private void onLastTransactionFinished(final ITransaction transaction, final boo
return;
}

String versionName = "";
String versionCode = "";
String totalMem = "0";
ActivityManager.MemoryInfo memInfo = getMemInfo();
if (packageInfo != null) {
versionName = ContextUtils.getVersionName(packageInfo);
versionCode = ContextUtils.getVersionCode(packageInfo, buildInfoProvider);
}
if (memInfo != null) {
totalMem = Long.toString(memInfo.totalMem);
}
Expand Down Expand Up @@ -373,8 +364,7 @@ private void onLastTransactionFinished(final ITransaction transaction, final boo
buildInfoProvider.isEmulator(),
totalMem,
options.getProguardUuid(),
versionName,
versionCode,
options.getRelease(),
options.getEnvironment(),
isTimeout
? ProfilingTraceData.TRUNCATION_REASON_TIMEOUT
Expand Down
Expand Up @@ -368,6 +368,20 @@ class AndroidTransactionProfilerTest {
}

@Test
fun `profiling trace data contains release field`() {
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
profiler.onTransactionFinish(fixture.transaction1)
verify(fixture.hub).captureEnvelope(
check {
assertEnvelopeItem<ProfilingTraceData>(it.items.toList()) { _, item ->
assertEquals(fixture.options.release, item.release)
assertNotNull(item.release)
}
}
)
}

fun `profiler starts collecting frame metrics when the first transaction starts`() {
val profiler = fixture.getSut(context)
profiler.onTransactionStart(fixture.transaction1)
Expand Down
10 changes: 4 additions & 6 deletions sentry/api/sentry.api
Expand Up @@ -836,7 +836,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;Lio/sentry/ITransaction;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/lang/String;Ljava/util/Map;)V
public fun <init> (Ljava/io/File;Ljava/util/List;Lio/sentry/ITransaction;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 @@ -853,6 +853,7 @@ public final class io/sentry/ProfilingTraceData : io/sentry/JsonSerializable, io
public fun getMeasurementsMap ()Ljava/util/Map;
public fun getPlatform ()Ljava/lang/String;
public fun getProfileId ()Ljava/lang/String;
public fun getRelease ()Ljava/lang/String;
public fun getSampledProfile ()Ljava/lang/String;
public fun getTraceFile ()Ljava/io/File;
public fun getTraceId ()Ljava/lang/String;
Expand All @@ -861,8 +862,6 @@ public final class io/sentry/ProfilingTraceData : io/sentry/JsonSerializable, io
public fun getTransactions ()Ljava/util/List;
public fun getTruncationReason ()Ljava/lang/String;
public fun getUnknown ()Ljava/util/Map;
public fun getVersionCode ()Ljava/lang/String;
public fun getVersionName ()Ljava/lang/String;
public fun isDeviceIsEmulator ()Z
public fun readDeviceCpuFrequencies ()V
public fun serialize (Lio/sentry/JsonObjectWriter;Lio/sentry/ILogger;)V
Expand All @@ -880,15 +879,14 @@ public final class io/sentry/ProfilingTraceData : io/sentry/JsonSerializable, io
public fun setDurationNs (Ljava/lang/String;)V
public fun setEnvironment (Ljava/lang/String;)V
public fun setProfileId (Ljava/lang/String;)V
public fun setRelease (Ljava/lang/String;)V
public fun setSampledProfile (Ljava/lang/String;)V
public fun setTraceId (Ljava/lang/String;)V
public fun setTransactionId (Ljava/lang/String;)V
public fun setTransactionName (Ljava/lang/String;)V
public fun setTransactions (Ljava/util/List;)V
public fun setTruncationReason (Ljava/lang/String;)V
public fun setUnknown (Ljava/util/Map;)V
public fun setVersionCode (Ljava/lang/String;)V
public fun setVersionName (Ljava/lang/String;)V
}

public final class io/sentry/ProfilingTraceData$Deserializer : io/sentry/JsonDeserializer {
Expand All @@ -915,14 +913,14 @@ public final class io/sentry/ProfilingTraceData$JsonKeys {
public static final field MEASUREMENTS Ljava/lang/String;
public static final field PLATFORM Ljava/lang/String;
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 TRACE_ID Ljava/lang/String;
public static final field TRANSACTION_ID Ljava/lang/String;
public static final field TRANSACTION_LIST Ljava/lang/String;
public static final field TRANSACTION_NAME Ljava/lang/String;
public static final field TRUNCATION_REASON Ljava/lang/String;
public static final field VERSION_CODE Ljava/lang/String;
public static final field VERSION_NAME Ljava/lang/String;
public fun <init> ()V
}

Expand Down
72 changes: 31 additions & 41 deletions sentry/src/main/java/io/sentry/ProfilingTraceData.java
Expand Up @@ -55,8 +55,8 @@ public final class ProfilingTraceData implements JsonUnknown, JsonSerializable {
private @NotNull String durationNs;

// App info
private @NotNull String versionName;
private @NotNull String versionCode;
private @NotNull String release;

// Stacktrace context
private @NotNull String transactionId;
Expand Down Expand Up @@ -93,29 +93,27 @@ public ProfilingTraceData(
null,
null,
null,
null,
TRUNCATION_REASON_NORMAL,
new HashMap<>());
}

public ProfilingTraceData(
final @NotNull File traceFile,
final @NotNull List<ProfilingTransactionData> transactions,
final @NotNull ITransaction transaction,
final @NotNull String durationNanos,
final int sdkInt,
final @NotNull String cpuArchitecture,
final @NotNull Callable<List<Integer>> deviceCpuFrequenciesReader,
final @Nullable String deviceManufacturer,
final @Nullable String deviceModel,
final @Nullable String deviceOsVersion,
final @Nullable Boolean deviceIsEmulator,
final @Nullable String devicePhysicalMemoryBytes,
final @Nullable String buildId,
final @Nullable String versionName,
final @Nullable String versionCode,
final @Nullable String environment,
final @NotNull String truncationReason,
@NotNull File traceFile,
@NotNull List<ProfilingTransactionData> transactions,
@NotNull ITransaction transaction,
@NotNull String durationNanos,
int sdkInt,
@NotNull String cpuArchitecture,
@NotNull Callable<List<Integer>> deviceCpuFrequenciesReader,
@Nullable String deviceManufacturer,
@Nullable String deviceModel,
@Nullable String deviceOsVersion,
@Nullable Boolean deviceIsEmulator,
@Nullable String devicePhysicalMemoryBytes,
@Nullable String buildId,
@Nullable String release,
@Nullable String environment,
@NotNull String truncationReason,
final @NotNull Map<String, ProfileMeasurement> measurementsMap) {
this.traceFile = traceFile;
this.cpuArchitecture = cpuArchitecture;
Expand All @@ -141,8 +139,8 @@ public ProfilingTraceData(
this.durationNs = durationNanos;

// App info
this.versionName = versionName != null ? versionName : "";
this.versionCode = versionCode != null ? versionCode : "";
this.versionCode = "";
this.release = release != null ? release : "";

// Stacktrace context
this.transactionId = transaction.getEventId().toString();
Expand Down Expand Up @@ -216,12 +214,8 @@ public boolean isDeviceIsEmulator() {
return transactionName;
}

public @NotNull String getVersionName() {
return versionName;
}

public @NotNull String getVersionCode() {
return versionCode;
public @NotNull String getRelease() {
return release;
}

public @NotNull String getTransactionId() {
Expand Down Expand Up @@ -328,12 +322,8 @@ public void setDurationNs(final @NotNull String durationNs) {
this.durationNs = durationNs;
}

public void setVersionName(final @NotNull String versionName) {
this.versionName = versionName;
}

public void setVersionCode(final @NotNull String versionCode) {
this.versionCode = versionCode;
public void setRelease(@NotNull String release) {
this.release = release;
}

public void setTransactionId(final @NotNull String transactionId) {
Expand Down Expand Up @@ -382,7 +372,7 @@ public static final class JsonKeys {
public static final String BUILD_ID = "build_id";
public static final String TRANSACTION_NAME = "transaction_name";
public static final String DURATION_NS = "duration_ns";
public static final String VERSION_NAME = "version_name";
public static final String RELEASE = "version_name";
public static final String VERSION_CODE = "version_code";
public static final String TRANSACTION_LIST = "transactions";
public static final String TRANSACTION_ID = "transaction_id";
Expand Down Expand Up @@ -414,7 +404,7 @@ public void serialize(final @NotNull JsonObjectWriter writer, final @NotNull ILo
writer.name(JsonKeys.BUILD_ID).value(buildId);
writer.name(JsonKeys.TRANSACTION_NAME).value(transactionName);
writer.name(JsonKeys.DURATION_NS).value(durationNs);
writer.name(JsonKeys.VERSION_NAME).value(versionName);
writer.name(JsonKeys.RELEASE).value(release);
writer.name(JsonKeys.VERSION_CODE).value(versionCode);
if (!transactions.isEmpty()) {
writer.name(JsonKeys.TRANSACTION_LIST).value(logger, transactions);
Expand Down Expand Up @@ -552,18 +542,18 @@ public static final class Deserializer implements JsonDeserializer<ProfilingTrac
data.durationNs = durationNs;
}
break;
case JsonKeys.VERSION_NAME:
String versionName = reader.nextStringOrNull();
if (versionName != null) {
data.versionName = versionName;
}
break;
case JsonKeys.VERSION_CODE:
String versionCode = reader.nextStringOrNull();
if (versionCode != null) {
data.versionCode = versionCode;
}
break;
case JsonKeys.RELEASE:
String versionName = reader.nextStringOrNull();
if (versionName != null) {
data.release = versionName;
}
break;
case JsonKeys.TRANSACTION_LIST:
List<ProfilingTransactionData> transactions =
reader.nextList(logger, new ProfilingTransactionData.Deserializer());
Expand Down
14 changes: 6 additions & 8 deletions sentry/src/test/java/io/sentry/JsonSerializerTest.kt
Expand Up @@ -509,8 +509,7 @@ class JsonSerializerTest {
)
profilingTraceData.transactionName = "transactionName"
profilingTraceData.durationNs = "100"
profilingTraceData.versionName = "versionName"
profilingTraceData.versionCode = "versionCode"
profilingTraceData.release = "release"
profilingTraceData.transactionId = "transactionId"
profilingTraceData.traceId = "traceId"
profilingTraceData.profileId = "profileId"
Expand Down Expand Up @@ -585,8 +584,8 @@ class JsonSerializerTest {
)
assertEquals("transactionName", element["transaction_name"] as String)
assertEquals("100", element["duration_ns"] as String)
assertEquals("versionName", element["version_name"] as String)
assertEquals("versionCode", element["version_code"] as String)
assertEquals("release", element["version_name"] as String)
assertEquals("", element["version_code"] as String)
assertEquals("transactionId", element["transaction_id"] as String)
assertEquals("traceId", element["trace_id"] as String)
assertEquals("profileId", element["profile_id"] as String)
Expand Down Expand Up @@ -643,8 +642,8 @@ class JsonSerializerTest {
},
"transaction_name":"transactionName",
"duration_ns":"100",
"version_name":"versionName",
"version_code":"versionCode",
"version_name":"release",
"version_code":"",
"transaction_id":"transactionId",
"trace_id":"traceId",
"profile_id":"profileId",
Expand Down Expand Up @@ -697,8 +696,7 @@ class JsonSerializerTest {
assertEquals(expectedMeasurements, profilingTraceData.measurementsMap)
assertEquals("transactionName", profilingTraceData.transactionName)
assertEquals("100", profilingTraceData.durationNs)
assertEquals("versionName", profilingTraceData.versionName)
assertEquals("versionCode", profilingTraceData.versionCode)
assertEquals("release", profilingTraceData.release)
assertEquals("transactionId", profilingTraceData.transactionId)
assertEquals("traceId", profilingTraceData.traceId)
assertEquals("profileId", profilingTraceData.profileId)
Expand Down

0 comments on commit 34a8bfd

Please sign in to comment.