Skip to content

Commit 6855a97

Browse files
committed
Update subscriber
1 parent a5249ac commit 6855a97

File tree

6 files changed

+64
-85
lines changed

6 files changed

+64
-85
lines changed
Submodule lss updated from ed31caa to 9719c1e

firebase-perf/src/main/java/com/google/firebase/perf/FirebasePerformance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import com.google.firebase.perf.logging.ConsoleUrlGenerator;
3737
import com.google.firebase.perf.metrics.HttpMetric;
3838
import com.google.firebase.perf.metrics.Trace;
39+
import com.google.firebase.perf.session.FirebasePerformanceSessionSubscriber;
3940
import com.google.firebase.perf.session.SessionManager;
40-
import com.google.firebase.perf.session.SessionManagerKt;
4141
import com.google.firebase.perf.transport.TransportManager;
4242
import com.google.firebase.perf.util.Constants;
4343
import com.google.firebase.perf.util.ImmutableBundle;
Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
1-
/*
2-
* Copyright 2025 Google LLC
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
171
package com.google.firebase.perf.session
182

19-
import com.google.firebase.perf.session.gauges.GaugeManager
20-
import com.google.firebase.perf.v1.ApplicationProcessState
3+
import com.google.firebase.perf.config.ConfigResolver
4+
import com.google.firebase.perf.logging.AndroidLogger
215
import com.google.firebase.sessions.api.SessionSubscriber
22-
import java.util.UUID
236

24-
class FirebasePerformanceSessionSubscriber(override val isDataCollectionEnabled: Boolean) :
7+
class FirebasePerformanceSessionSubscriber(private val dataCollectionEnabled: Boolean) :
258
SessionSubscriber {
9+
private val perfSessionToAqs: MutableMap<String, SessionSubscriber.SessionDetails?> =
10+
mutableMapOf()
11+
12+
override val isDataCollectionEnabled: Boolean
13+
get() = dataCollectionEnabled
2614

27-
override val sessionSubscriberName: SessionSubscriber.Name = SessionSubscriber.Name.PERFORMANCE
15+
override val sessionSubscriberName: SessionSubscriber.Name
16+
get() = SessionSubscriber.Name.PERFORMANCE
2817

2918
override fun onSessionChanged(sessionDetails: SessionSubscriber.SessionDetails) {
30-
val currentPerfSession = SessionManager.getInstance().perfSession()
31-
32-
// A [PerfSession] was created before a session was started.
33-
if (currentPerfSession.aqsSessionId() == null) {
34-
currentPerfSession.setAQSId(sessionDetails)
35-
GaugeManager.getInstance()
36-
.logGaugeMetadata(currentPerfSession.aqsSessionId(), ApplicationProcessState.FOREGROUND)
37-
return
19+
AndroidLogger.getInstance().debug("AQS Session Changed: $sessionDetails")
20+
val currentInternalSessionId = SessionManager.getInstance().perfSession().internalSessionId
21+
22+
// There can be situations where a new [PerfSession] was created, but an AQS wasn't
23+
// available (during cold start).
24+
if (perfSessionToAqs[currentInternalSessionId] == null) {
25+
perfSessionToAqs[currentInternalSessionId] = sessionDetails
26+
} else {
27+
val newSession = PerfSession.createNewSession()
28+
SessionManager.getInstance().updatePerfSession(newSession)
29+
perfSessionToAqs[newSession.internalSessionId] = sessionDetails
3830
}
31+
}
32+
33+
fun reportPerfSession(perfSessionId: String) {
34+
perfSessionToAqs[perfSessionId] = null
35+
}
3936

40-
val updatedSession = PerfSession.createWithId(UUID.randomUUID().toString())
41-
updatedSession.setAQSId(sessionDetails)
42-
SessionManager.getInstance().updatePerfSession(updatedSession)
43-
GaugeManager.getInstance()
44-
.logGaugeMetadata(updatedSession.aqsSessionId(), ApplicationProcessState.FOREGROUND)
37+
fun getAqsMappedToPerfSession(perfSessionId: String): String {
38+
AndroidLogger.getInstance()
39+
.debug("AQS for perf session $perfSessionId is ${perfSessionToAqs[perfSessionId]?.sessionId}")
40+
return perfSessionToAqs[perfSessionId]?.sessionId ?: perfSessionId
41+
}
42+
43+
fun clearSessionForTest() {
44+
perfSessionToAqs.clear()
45+
}
46+
47+
companion object {
48+
val instance: FirebasePerformanceSessionSubscriber by lazy {
49+
FirebasePerformanceSessionSubscriber(
50+
ConfigResolver.getInstance().isPerformanceMonitoringEnabled
51+
)
52+
}
4553
}
4654
}

firebase-perf/src/main/java/com/google/firebase/perf/session/PerfSession.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
import androidx.annotation.Nullable;
2121
import androidx.annotation.VisibleForTesting;
2222
import com.google.firebase.perf.config.ConfigResolver;
23-
import com.google.firebase.perf.logging.AndroidLogger;
2423
import com.google.firebase.perf.util.Clock;
2524
import com.google.firebase.perf.util.Timer;
2625
import com.google.firebase.perf.v1.SessionVerbosity;
27-
import com.google.firebase.sessions.api.SessionSubscriber;
2826
import java.util.List;
29-
import java.util.Objects;
3027
import java.util.UUID;
3128
import java.util.concurrent.TimeUnit;
3229

@@ -50,7 +47,7 @@ public static PerfSession createNewSession() {
5047
// SessionManagerKt verifies if this is an active session, and sets the AQS session ID.
5148
// The assumption is that new PerfSessions *should* be limited to either App Start, or through
5249
// AQS.
53-
SessionManagerKt.Companion.getPerfSessionToAqs().put(prunedSessionId, null);
50+
FirebasePerformanceSessionSubscriber.Companion.getInstance().reportPerfSession(prunedSessionId);
5451

5552
return session;
5653
}
@@ -71,15 +68,12 @@ private PerfSession(@NonNull Parcel in) {
7168

7269
/** Returns the sessionId of the object. */
7370
public String sessionId() {
74-
// TODO(b/394127311): Verify edge cases.
75-
SessionSubscriber.SessionDetails sessionDetails =
76-
SessionManagerKt.Companion.getPerfSessionToAqs().get(internalSessionId);
77-
AndroidLogger.getInstance()
78-
.debug("AQS for " + this.internalSessionId + " is " + sessionDetails);
79-
return Objects.requireNonNull(sessionDetails).getSessionId();
71+
return FirebasePerformanceSessionSubscriber.Companion.getInstance()
72+
.getAqsMappedToPerfSession(this.internalSessionId);
8073
}
8174

82-
protected String getInternalSessionId() {
75+
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
76+
public String getInternalSessionId() {
8377
return internalSessionId;
8478
}
8579

@@ -133,7 +127,7 @@ public boolean isSessionRunningTooLong() {
133127
/** Creates and returns the proto object for PerfSession object. */
134128
public com.google.firebase.perf.v1.PerfSession build() {
135129
com.google.firebase.perf.v1.PerfSession.Builder sessionMetric =
136-
com.google.firebase.perf.v1.PerfSession.newBuilder().setSessionId(internalSessionId);
130+
com.google.firebase.perf.v1.PerfSession.newBuilder().setSessionId(sessionId());
137131

138132
// If gauge collection is enabled, enable gauge collection verbosity.
139133
if (isGaugeAndEventCollectionEnabled) {

firebase-perf/src/main/java/com/google/firebase/perf/session/SessionManagerKt.kt

Lines changed: 0 additions & 33 deletions
This file was deleted.

firebase-perf/src/test/java/com/google/firebase/perf/FirebasePerformanceTestBase.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import com.google.firebase.FirebaseApp;
2424
import com.google.firebase.FirebaseOptions;
2525
import com.google.firebase.perf.config.ConfigResolver;
26-
import com.google.firebase.perf.session.PerfSession;
27-
import com.google.firebase.perf.session.SessionManager;
26+
import com.google.firebase.perf.session.FirebasePerformanceSessionSubscriber;
2827
import com.google.firebase.perf.util.ImmutableBundle;
28+
import com.google.firebase.sessions.api.SessionSubscriber;
29+
import java.util.UUID;
2930
import org.junit.After;
3031
import org.junit.Before;
3132
import org.robolectric.shadows.ShadowPackageManager;
@@ -52,6 +53,8 @@ public class FirebasePerformanceTestBase {
5253
protected static final String FAKE_FIREBASE_DB_URL = "https://fir-perftestapp.firebaseio.com";
5354
protected static final String FAKE_FIREBASE_PROJECT_ID = "fir-perftestapp";
5455

56+
protected static final String FAKE_AQS_SESSION_PREFIX = "AIzaSyBcE";
57+
5558
protected Context appContext;
5659

5760
@Before
@@ -72,11 +75,13 @@ public void setUpFirebaseApp() {
7275
.setProjectId(FAKE_FIREBASE_PROJECT_ID)
7376
.build();
7477
FirebaseApp.initializeApp(appContext, options);
78+
triggerAqsSession();
7579
}
7680

7781
@After
7882
public void tearDownFirebaseApp() {
7983
FirebaseApp.clearInstancesForTest();
84+
FirebasePerformanceSessionSubscriber.Companion.getInstance().clearSessionForTest();
8085
}
8186

8287
protected static void forceSessionsFeatureDisabled() {
@@ -93,11 +98,16 @@ protected static void forceNonVerboseSession() {
9398
forceVerboseSessionWithSamplingPercentage(0);
9499
}
95100

101+
protected static void triggerAqsSession() {
102+
FirebasePerformanceSessionSubscriber.Companion.getInstance()
103+
.onSessionChanged(
104+
new SessionSubscriber.SessionDetails(FAKE_AQS_SESSION_PREFIX + UUID.randomUUID()));
105+
}
106+
96107
private static void forceVerboseSessionWithSamplingPercentage(long samplingPercentage) {
97108
Bundle bundle = new Bundle();
98109
bundle.putFloat("sessions_sampling_percentage", samplingPercentage);
99110
ConfigResolver.getInstance().setMetadataBundle(new ImmutableBundle(bundle));
100-
101-
SessionManager.getInstance().setPerfSession(PerfSession.createNewSession());
111+
triggerAqsSession();
102112
}
103113
}

0 commit comments

Comments
 (0)