Skip to content

Commit 134398c

Browse files
committed
Add a SessionSubscriber
1 parent 9c03b7b commit 134398c

File tree

6 files changed

+68
-25
lines changed

6 files changed

+68
-25
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import com.google.firebase.perf.injection.modules.FirebasePerformanceModule;
3131
import com.google.firebase.platforminfo.LibraryVersionComponent;
3232
import com.google.firebase.remoteconfig.RemoteConfigComponent;
33+
import com.google.firebase.sessions.api.FirebaseSessionsDependencies;
34+
import com.google.firebase.sessions.api.SessionSubscriber;
35+
3336
import java.util.Arrays;
3437
import java.util.List;
3538
import java.util.concurrent.Executor;
@@ -47,6 +50,11 @@ public class FirebasePerfRegistrar implements ComponentRegistrar {
4750
private static final String LIBRARY_NAME = "fire-perf";
4851
private static final String EARLY_LIBRARY_NAME = "fire-perf-early";
4952

53+
static {
54+
// Add Firebase Performance as a dependency of Sessions when this class is loaded into memory.
55+
FirebaseSessionsDependencies.addDependency(SessionSubscriber.Name.PERFORMANCE);
56+
}
57+
5058
@Override
5159
@Keep
5260
public List<Component<?>> getComponents() {

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@
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;
4041
import com.google.firebase.perf.transport.TransportManager;
4142
import com.google.firebase.perf.util.Constants;
4243
import com.google.firebase.perf.util.ImmutableBundle;
4344
import com.google.firebase.perf.util.Timer;
4445
import com.google.firebase.remoteconfig.RemoteConfigComponent;
46+
import com.google.firebase.sessions.api.FirebaseSessionsDependencies;
47+
4548
import java.lang.annotation.Retention;
4649
import java.lang.annotation.RetentionPolicy;
4750
import java.net.URL;
@@ -52,6 +55,7 @@
5255
import javax.inject.Inject;
5356
import javax.inject.Singleton;
5457

58+
5559
/**
5660
* The Firebase Performance Monitoring API.
5761
*
@@ -92,7 +96,7 @@ public class FirebasePerformance implements FirebasePerformanceAttributable {
9296
// once during initialization and cache it.
9397
private final ImmutableBundle mMetadataBundle;
9498

95-
/** Valid HttpMethods for manual network APIs */
99+
/** Valid HttpMethods for manual network APIs */
96100
@StringDef({
97101
HttpMethod.GET,
98102
HttpMethod.PUT,
@@ -136,12 +140,7 @@ public static FirebasePerformance getInstance() {
136140
// to false if it's been force disabled or it is set to null if neither.
137141
@Nullable private Boolean mPerformanceCollectionForceEnabledState = null;
138142

139-
private final FirebaseApp firebaseApp;
140-
private final Provider<RemoteConfigComponent> firebaseRemoteConfigProvider;
141-
private final FirebaseInstallationsApi firebaseInstallationsApi;
142-
private final Provider<TransportFactory> transportFactoryProvider;
143-
144-
/**
143+
/**
145144
* Constructs the FirebasePerformance class and allows injecting dependencies.
146145
*
147146
* <p>TODO(b/172007278): Initialize SDK components in a background thread to avoid cases of cyclic
@@ -166,11 +165,6 @@ public static FirebasePerformance getInstance() {
166165
ConfigResolver configResolver,
167166
SessionManager sessionManager) {
168167

169-
this.firebaseApp = firebaseApp;
170-
this.firebaseRemoteConfigProvider = firebaseRemoteConfigProvider;
171-
this.firebaseInstallationsApi = firebaseInstallationsApi;
172-
this.transportFactoryProvider = transportFactoryProvider;
173-
174168
if (firebaseApp == null) {
175169
this.mPerformanceCollectionForceEnabledState = false;
176170
this.configResolver = configResolver;
@@ -191,6 +185,8 @@ public static FirebasePerformance getInstance() {
191185
sessionManager.setApplicationContext(appContext);
192186

193187
mPerformanceCollectionForceEnabledState = configResolver.getIsPerformanceCollectionEnabled();
188+
FirebaseSessionsDependencies.register(new FirebasePerformanceSessionSubscriber(isPerformanceCollectionEnabled()));
189+
194190
if (logger.isLogcatEnabled() && isPerformanceCollectionEnabled()) {
195191
logger.info(
196192
String.format(
@@ -281,7 +277,7 @@ public synchronized void setPerformanceCollectionEnabled(@Nullable Boolean enabl
281277
return;
282278
}
283279

284-
if (configResolver.getIsPerformanceCollectionDeactivated()) {
280+
if (Boolean.TRUE.equals(configResolver.getIsPerformanceCollectionDeactivated())) {
285281
logger.info("Firebase Performance is permanently disabled");
286282
return;
287283
}

firebase-perf/src/main/java/com/google/firebase/perf/config/ConfigResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void setMetadataBundle(ImmutableBundle bundle) {
116116
/** Default API to call for whether performance monitoring is currently silent. */
117117
public boolean isPerformanceMonitoringEnabled() {
118118
Boolean isPerformanceCollectionEnabled = getIsPerformanceCollectionEnabled();
119-
return (isPerformanceCollectionEnabled == null || isPerformanceCollectionEnabled == true)
119+
return (isPerformanceCollectionEnabled == null || isPerformanceCollectionEnabled)
120120
&& getIsServiceCollectionEnabled();
121121
}
122122

@@ -131,7 +131,7 @@ public Boolean getIsPerformanceCollectionEnabled() {
131131
// return developer config.
132132
// 4. Else, return null. Because Firebase Performance will read highlevel Firebase flag in this
133133
// case.
134-
if (getIsPerformanceCollectionDeactivated()) {
134+
if (Boolean.TRUE.equals(getIsPerformanceCollectionDeactivated())) {
135135
// 1. If developer has deactivated Firebase Performance in Manifest, return false.
136136
return false;
137137
}
@@ -186,7 +186,7 @@ public void setIsPerformanceCollectionEnabled(Boolean isEnabled) {
186186
// 2. Otherwise, save this configuration in device cache.
187187

188188
// If collection is deactivated, skip the action to save user configuration.
189-
if (getIsPerformanceCollectionDeactivated()) {
189+
if (Boolean.TRUE.equals(getIsPerformanceCollectionDeactivated())) {
190190
return;
191191
}
192192

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.google.firebase.perf.session
2+
3+
import com.google.firebase.perf.logging.AndroidLogger
4+
import com.google.firebase.perf.session.gauges.GaugeManager
5+
import com.google.firebase.perf.v1.ApplicationProcessState
6+
import com.google.firebase.sessions.api.SessionSubscriber
7+
import java.util.UUID
8+
9+
class FirebasePerformanceSessionSubscriber(private val dataCollectionEnabled: Boolean) : SessionSubscriber {
10+
override val isDataCollectionEnabled: Boolean
11+
get() = dataCollectionEnabled
12+
13+
override val sessionSubscriberName: SessionSubscriber.Name
14+
get() = SessionSubscriber.Name.PERFORMANCE
15+
16+
override fun onSessionChanged(sessionDetails: SessionSubscriber.SessionDetails) {
17+
val currentPerfSession = SessionManager.getInstance().perfSession()
18+
19+
// A [PerfSession] was created before a session was started.
20+
if (currentPerfSession.aqsSessionId() == null) {
21+
currentPerfSession.setAQSId(sessionDetails)
22+
GaugeManager.getInstance().logGaugeMetadata(currentPerfSession.aqsSessionId(), ApplicationProcessState.FOREGROUND)
23+
return
24+
}
25+
26+
val updatedSession = PerfSession.createWithId(UUID.randomUUID().toString());
27+
updatedSession.setAQSId(sessionDetails)
28+
SessionManager.getInstance().updatePerfSession(updatedSession)
29+
GaugeManager.getInstance().logGaugeMetadata(updatedSession.aqsSessionId(), ApplicationProcessState.FOREGROUND)
30+
}
31+
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.google.firebase.perf.util.Clock;
2424
import com.google.firebase.perf.util.Timer;
2525
import com.google.firebase.perf.v1.SessionVerbosity;
26+
import com.google.firebase.sessions.api.SessionSubscriber;
27+
2628
import java.util.List;
2729
import java.util.concurrent.TimeUnit;
2830

@@ -31,6 +33,7 @@ public class PerfSession implements Parcelable {
3133

3234
private final String sessionId;
3335
private final Timer creationTime;
36+
@Nullable private String aqsSessionId;
3437

3538
private boolean isGaugeAndEventCollectionEnabled = false;
3639

@@ -59,11 +62,23 @@ private PerfSession(@NonNull Parcel in) {
5962
creationTime = in.readParcelable(Timer.class.getClassLoader());
6063
}
6164

62-
/** Returns the sessionId of the object. */
65+
/** Returns the sessionId of the session. */
6366
public String sessionId() {
6467
return sessionId;
6568
}
6669

70+
/** Returns the AQS sessionId for the given session. */
71+
public String aqsSessionId() {
72+
return aqsSessionId;
73+
}
74+
75+
/** Returns the AQS sessionId for the given session. */
76+
public void setAQSId(SessionSubscriber.SessionDetails aqs) {
77+
if (aqsSessionId == null) {
78+
aqsSessionId = aqs.getSessionId();
79+
}
80+
}
81+
6782
/**
6883
* Returns a timer object that has been seeded with the system time at which the session began.
6984
*/

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,7 @@ public SessionManager(
7979
* (currently that is before onResume finishes) to ensure gauge collection starts on time.
8080
*/
8181
public void setApplicationContext(final Context appContext) {
82-
// TODO(b/258263016): Migrate to go/firebase-android-executors
83-
@SuppressLint("ThreadPoolCreation")
84-
ExecutorService executorService = Executors.newSingleThreadExecutor();
85-
syncInitFuture =
86-
executorService.submit(
87-
() -> {
88-
gaugeManager.initializeGaugeMetadataManager(appContext);
89-
});
82+
gaugeManager.initializeGaugeMetadataManager(appContext);
9083
}
9184

9285
@Override

0 commit comments

Comments
 (0)