Skip to content

Add and populate appQualitySessionId in Crashlytics reports #5045

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

Merged
merged 4 commits into from
Jun 2, 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
7 changes: 3 additions & 4 deletions firebase-crashlytics/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ firebaseLibrary {
}

android {
compileSdkVersion project.targetSdkVersion
compileSdkVersion 33
defaultConfig {
minSdkVersion 16
minSdk 16
multiDexEnabled true
targetSdkVersion project.targetSdkVersion
targetSdk 33
versionName version
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -69,4 +69,3 @@ dependencies {
// ==========================================================================
ext.packageName = "com.google.firebase.crashlytics.ktx"
apply from: '../../gradle/googleServices.gradle'

Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public CrashlyticsCore build() {
new DisabledBreadcrumbSource(),
new UnavailableAnalyticsEventLogger(),
fileStore,
crashHandlerExecutor);
crashHandlerExecutor,
mock(CrashlyticsAppQualitySessionsSubscriber.class));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ CrashlyticsCore build(Context context) {
breadcrumbSource,
new UnavailableAnalyticsEventLogger(),
new FileStore(context),
new SameThreadExecutorService());
new SameThreadExecutorService(),
mock(CrashlyticsAppQualitySessionsSubscriber.class));
return crashlyticsCore;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.mockito.Mockito.when;

import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
import com.google.firebase.crashlytics.internal.common.CrashlyticsAppQualitySessionsSubscriber;
import com.google.firebase.crashlytics.internal.common.CrashlyticsReportWithSessionId;
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport.Session;
Expand All @@ -42,8 +43,8 @@
import org.mockito.internal.util.collections.Sets;

public class CrashlyticsReportPersistenceTest extends CrashlyticsTestCase {

private static final int VERY_LARGE_UPPER_LIMIT = 9999;
private static final String APP_QUALITY_SESSION_ID = "9";

private CrashlyticsReportPersistence reportPersistence;
private FileStore fileStore;
Expand All @@ -62,12 +63,22 @@ private static SettingsProvider createSettingsProviderMock(
return settingsProvider;
}

private static CrashlyticsAppQualitySessionsSubscriber createSessionsSubscriberMock(
String appQualitySessionId) {
CrashlyticsAppQualitySessionsSubscriber sessionsSubscriber =
mock(CrashlyticsAppQualitySessionsSubscriber.class);
when(sessionsSubscriber.getAppQualitySessionId()).thenReturn(appQualitySessionId);
return sessionsSubscriber;
}

@Override
public void setUp() throws Exception {
fileStore = new FileStore(getContext());
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, createSettingsProviderMock(VERY_LARGE_UPPER_LIMIT, VERY_LARGE_UPPER_LIMIT));
fileStore,
createSettingsProviderMock(VERY_LARGE_UPPER_LIMIT, VERY_LARGE_UPPER_LIMIT),
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));
}

public void testListSortedOpenSessionIds() {
Expand Down Expand Up @@ -147,6 +158,7 @@ public void testLoadFinalizedReports_reportThenEvent_returnsReportWithEvent() {
assertEquals(
testReport
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(APP_QUALITY_SESSION_ID)
.withEvents(ImmutableList.from(testEvent)),
finalizedReport);
}
Expand All @@ -172,6 +184,7 @@ public void testLoadFinalizedReports_reportThenMultipleEvents_returnsReportWithM
assertEquals(
testReport
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(APP_QUALITY_SESSION_ID)
.withEvents(ImmutableList.from(testEvent, testEvent2)),
finalizedReport);
}
Expand Down Expand Up @@ -201,12 +214,14 @@ public void testLoadFinalizedReports_reportThenMultipleEvents_returnsReportWithM
assertEquals(
testReport1
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(APP_QUALITY_SESSION_ID)
.withEvents(ImmutableList.from(testEvent1)),
finalizedReport1);
final CrashlyticsReport finalizedReport2 = finalizedReports.get(0).getReport();
assertEquals(
testReport2
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(APP_QUALITY_SESSION_ID)
.withEvents(ImmutableList.from(testEvent2)),
finalizedReport2);
}
Expand Down Expand Up @@ -274,7 +289,9 @@ public void testFinalizeReports_skipsCappingCurrentSession() throws IOException
public void testFinalizeReports_capsReports() {
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT));
fileStore,
createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT),
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));
for (int i = 0; i < 10; i++) {
persistReportWithEvent(reportPersistence, "testSession" + i, true);
}
Expand All @@ -298,7 +315,9 @@ public void testFinalizeReports_whenSettingsChanges_capsReports() throws IOExcep
new Settings(0, sessionData2, new FeatureFlagData(true, true, false), 3, 0, 1.0, 1.0, 1);

when(settingsProvider.getSettingsSync()).thenReturn(settings1);
reportPersistence = new CrashlyticsReportPersistence(fileStore, settingsProvider);
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, settingsProvider, createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));

DecimalFormat format = new DecimalFormat("00");
for (int i = 0; i < 16; i++) {
Expand All @@ -324,7 +343,9 @@ public void testFinalizeReports_whenSettingsChanges_capsReports() throws IOExcep
public void testFinalizeReports_removesLowPriorityReportsFirst() throws IOException {
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT));
fileStore,
createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT),
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));

for (int i = 0; i < 10; i++) {
boolean priority = i >= 3 && i <= 8;
Expand All @@ -347,7 +368,9 @@ public void testFinalizeReports_prioritizesNativeAndNonnativeFatals() throws IOE
CrashlyticsReport.FilesPayload filesPayload = makeFilePayload();
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT));
fileStore,
createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT),
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));

persistReportWithEvent(reportPersistence, "testSession1", true);
reportPersistence.finalizeSessionWithNativeEvent("testSession1", filesPayload, null);
Expand All @@ -370,7 +393,9 @@ public void testFinalizeReports_prioritizesNativeAndNonnativeFatals() throws IOE
public void testFinalizeReports_removesOldestReportsFirst() throws IOException {
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT));
fileStore,
createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT),
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));
for (int i = 0; i < 8; i++) {
String sessionId = "testSession" + i;
persistReportWithEvent(reportPersistence, sessionId, true);
Expand Down Expand Up @@ -519,7 +544,9 @@ public void testDeleteAllReports_removesAllReports() {
public void testPersistEvent_keepsAppropriateNumberOfMostRecentEvents() throws IOException {
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, createSettingsProviderMock(VERY_LARGE_UPPER_LIMIT, 4));
fileStore,
createSettingsProviderMock(VERY_LARGE_UPPER_LIMIT, 4),
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));
final String sessionId = "testSession";
final CrashlyticsReport testReport = makeTestReport(sessionId);
final CrashlyticsReport.Session.Event testEvent1 = makeTestEvent("type1", "reason1");
Expand Down Expand Up @@ -547,6 +574,7 @@ public void testPersistEvent_keepsAppropriateNumberOfMostRecentEvents() throws I
assertEquals(
testReport
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(APP_QUALITY_SESSION_ID)
.withEvents(ImmutableList.from(testEvent2, testEvent3, testEvent4, testEvent5)),
finalizedReport);
}
Expand All @@ -563,7 +591,9 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
new Settings(0, sessionData2, new FeatureFlagData(true, true, false), 3, 0, 1.0, 1.0, 1);

when(settingsProvider.getSettingsSync()).thenReturn(settings1);
reportPersistence = new CrashlyticsReportPersistence(fileStore, settingsProvider);
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, settingsProvider, createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));

final String sessionId = "testSession";
final CrashlyticsReport testReport = makeTestReport(sessionId);
Expand Down Expand Up @@ -592,6 +622,7 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
assertEquals(
testReport
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(APP_QUALITY_SESSION_ID)
.withEvents(ImmutableList.from(testEvent2, testEvent3, testEvent4, testEvent5)),
finalizedReport);

Expand Down Expand Up @@ -629,6 +660,7 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
assertEquals(
testReport2
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(APP_QUALITY_SESSION_ID)
.withEvents(
ImmutableList.from(
testEvent3,
Expand All @@ -645,7 +677,9 @@ public void testPersistEvent_whenSettingsChanges_keepsAppropriateNumberOfMostRec
public void testPersistReportWithAnrEvent() throws IOException {
reportPersistence =
new CrashlyticsReportPersistence(
fileStore, createSettingsProviderMock(VERY_LARGE_UPPER_LIMIT, 4));
fileStore,
createSettingsProviderMock(VERY_LARGE_UPPER_LIMIT, 4),
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID));
final String sessionId = "testSession";
final CrashlyticsReport testReport = makeTestReport(sessionId);
final Event testEvent = makeTestAnrEvent();
Expand All @@ -663,6 +697,81 @@ public void testPersistReportWithAnrEvent() throws IOException {
assertEquals(1, finalizedReport.getSession().getEvents().size());
}

public void testFinalizeReports_missingAppQualitySessionId() {
reportPersistence =
new CrashlyticsReportPersistence(
fileStore,
createSettingsProviderMock(4, VERY_LARGE_UPPER_LIMIT),
// Simulate Sessions subscriber failure by setting appQualitySessionId to null.
createSessionsSubscriberMock(/* appQualitySessionId= */ null));

String sessionId = "testSession";
CrashlyticsReport testReport = makeTestReport(sessionId);
CrashlyticsReport.Session.Event testEvent = makeTestEvent();

reportPersistence.persistReport(testReport);
reportPersistence.persistEvent(testEvent, sessionId);

long endedAt = System.currentTimeMillis();

reportPersistence.finalizeReports("skippedSession", endedAt);

List<CrashlyticsReportWithSessionId> finalizedReports =
reportPersistence.loadFinalizedReports();
assertEquals(1, finalizedReports.size());
CrashlyticsReport finalizedReport = finalizedReports.get(0).getReport();
assertNotNull(finalizedReport.getSession());
assertEquals(
testReport
.withSessionEndFields(endedAt, false, null)
.withEvents(ImmutableList.from(testEvent)),
finalizedReport);

// getAppQualitySessionId should return null since sessions subscriber never got an id.
assertNull(finalizedReport.getSession().getAppQualitySessionId());
}

public void testPersistEvent_updatesLatestAppQualitySession() {
CrashlyticsAppQualitySessionsSubscriber mockSessionsSubscriber =
createSessionsSubscriberMock(APP_QUALITY_SESSION_ID);
CrashlyticsReportPersistence reportPersistence =
new CrashlyticsReportPersistence(
fileStore,
createSettingsProviderMock(VERY_LARGE_UPPER_LIMIT, VERY_LARGE_UPPER_LIMIT),
mockSessionsSubscriber);

String sessionId = "testSession";
CrashlyticsReport testReport = makeTestReport(sessionId);
CrashlyticsReport.Session.Event testEvent1 = makeTestEvent("type1", "reason1");
CrashlyticsReport.Session.Event testEvent2 = makeTestEvent("type2", "reason2");
CrashlyticsReport.Session.Event testEvent3 = makeTestEvent("type3", "reason3");

reportPersistence.persistReport(testReport);
reportPersistence.persistEvent(testEvent1, sessionId);
reportPersistence.persistEvent(testEvent2, sessionId);

// Simulate a new app quality sessions session before the last event.
String latestAppQualitySessionId = "300";
when(mockSessionsSubscriber.getAppQualitySessionId()).thenReturn(latestAppQualitySessionId);
reportPersistence.persistEvent(testEvent3, sessionId);

long endedAt = System.currentTimeMillis();

reportPersistence.finalizeReports("skippedSession", endedAt);

List<CrashlyticsReportWithSessionId> finalizedReports =
reportPersistence.loadFinalizedReports();
assertEquals(1, finalizedReports.size());
CrashlyticsReport finalizedReport = finalizedReports.get(0).getReport();
assertNotNull(finalizedReport.getSession());
assertEquals(
testReport
.withSessionEndFields(endedAt, false, null)
.withAppQualitySessionId(latestAppQualitySessionId)
.withEvents(ImmutableList.from(testEvent1, testEvent2, testEvent3)),
finalizedReport);
}

private static void persistReportWithEvent(
CrashlyticsReportPersistence reportPersistence, String sessionId, boolean isHighPriority) {
CrashlyticsReport testReport = makeTestReport(sessionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.firebase.crashlytics.internal.common.AppData;
import com.google.firebase.crashlytics.internal.common.BuildIdInfo;
import com.google.firebase.crashlytics.internal.common.CommonUtils;
import com.google.firebase.crashlytics.internal.common.CrashlyticsAppQualitySessionsSubscriber;
import com.google.firebase.crashlytics.internal.common.CrashlyticsCore;
import com.google.firebase.crashlytics.internal.common.DataCollectionArbiter;
import com.google.firebase.crashlytics.internal.common.ExecutorUtils;
Expand All @@ -41,7 +42,6 @@
import com.google.firebase.inject.Deferred;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.sessions.FirebaseSessions;
import com.google.firebase.sessions.api.SessionSubscriber;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -91,6 +91,10 @@ public class FirebaseCrashlytics {
final ExecutorService crashHandlerExecutor =
ExecutorUtils.buildSingleThreadExecutorService("Crashlytics Exception Handler");

CrashlyticsAppQualitySessionsSubscriber sessionsSubscriber =
new CrashlyticsAppQualitySessionsSubscriber(arbiter);
firebaseSessions.register(sessionsSubscriber);

final CrashlyticsCore core =
new CrashlyticsCore(
app,
Expand All @@ -100,7 +104,8 @@ public class FirebaseCrashlytics {
analyticsDeferredProxy.getDeferredBreadcrumbSource(),
analyticsDeferredProxy.getAnalyticsEventLogger(),
fileStore,
crashHandlerExecutor);
crashHandlerExecutor,
sessionsSubscriber);

final String googleAppId = app.getOptions().getApplicationId();
final String mappingFileId = CommonUtils.getMappingFileId(context);
Expand Down Expand Up @@ -179,28 +184,6 @@ public Void call() throws Exception {
}
});

// TODO(mrober): Replace with a real session implementation.
firebaseSessions.register(
new SessionSubscriber() {
@Override
public void onSessionChanged(@NonNull SessionDetails sessionDetails) {
Logger.getLogger().d("onSessionChanged: " + sessionDetails);
// TODO(mrober): Set new field in report and remove this.
core.setInternalKey("sessionId", sessionDetails.getSessionId());
}

@Override
public boolean isDataCollectionEnabled() {
return arbiter.isAutomaticDataCollectionEnabled();
}

@NonNull
@Override
public SessionSubscriber.Name getSessionSubscriberName() {
return SessionSubscriber.Name.CRASHLYTICS;
}
});

return new FirebaseCrashlytics(core);
}

Expand Down
Loading