Skip to content

Commit 45d094f

Browse files
authored
Merge 8193be9 into 316e168
2 parents 316e168 + 8193be9 commit 45d094f

37 files changed

+926
-902
lines changed

firebase-perf/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Unreleased
22
* [fixed] Fixed an ANR on app launch. [#4831]
3+
* [changed] Updated `firebase-sessions` dependency to v3.0.0
4+
* [fixed] Fixed the issues around unifying the sessions in `firebase-sessions`
5+
and`firebase-performance`.
36

47
# 22.0.0
58
* [changed] **Breaking Change**: Updated minSdkVersion to API level 23 or higher.
@@ -442,4 +445,3 @@ updates.
442445

443446
# 16.1.0
444447
* [fixed] Fixed a `SecurityException` crash on certain devices that do not have Google Play Services on them.
445-

firebase-perf/firebase-perf.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ android {
7070
buildConfigField("String", "TRANSPORT_LOG_SRC", "String.valueOf(\"FIREPERF\")")
7171
buildConfigField("Boolean", "ENFORCE_DEFAULT_LOG_SRC", "Boolean.valueOf(false)")
7272
buildConfigField("String", "FIREPERF_VERSION_NAME", "String.valueOf(\"" + property("version") + "\")")
73+
buildConfigField("Boolean", "ENFORCE_LEGACY_SESSIONS", "Boolean.valueOf(false)")
7374

7475
if (project.hasProperty("fireperfBuildForAutopush")) {
7576
// This allows the SDK to be built for "Autopush" env when the mentioned flag
7677
// (-PfireperfBuildForAutopush) is passed in the gradle build command (of either the
7778
// SDK or the Test App).
7879
buildConfigField("String", "TRANSPORT_LOG_SRC", "String.valueOf(\"FIREPERF_AUTOPUSH\")")
7980
buildConfigField("Boolean", "ENFORCE_DEFAULT_LOG_SRC", "Boolean.valueOf(true)")
81+
buildConfigField("Boolean", "ENFORCE_LEGACY_SESSIONS", "Boolean.valueOf(true)")
8082
}
8183

8284
minSdkVersion project.minSdkVersion
@@ -121,7 +123,7 @@ dependencies {
121123
api("com.google.firebase:firebase-installations:18.0.0") {
122124
exclude group: 'com.google.firebase', module: 'firebase-common-ktx'
123125
}
124-
api("com.google.firebase:firebase-sessions:2.0.7") {
126+
api("com.google.firebase:firebase-sessions:3.0.0") {
125127
exclude group: 'com.google.firebase', module: 'firebase-common'
126128
exclude group: 'com.google.firebase', module: 'firebase-common-ktx'
127129
exclude group: 'com.google.firebase', module: 'firebase-components'

firebase-perf/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
#
1717

18-
version=22.0.1
18+
version=22.1.0
1919
latestReleasedVersion=22.0.0
2020
android.enableUnitTestBinaryResources=true
2121

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import com.google.firebase.perf.application.AppStateMonitor;
2222
import com.google.firebase.perf.config.ConfigResolver;
2323
import com.google.firebase.perf.metrics.AppStartTrace;
24-
import com.google.firebase.perf.session.SessionManager;
24+
import com.google.firebase.perf.session.FirebasePerformanceSessionSubscriber;
25+
import com.google.firebase.sessions.api.FirebaseSessionsDependencies;
2526
import java.util.concurrent.Executor;
2627

2728
/**
@@ -41,6 +42,10 @@ public FirebasePerfEarly(
4142
ConfigResolver configResolver = ConfigResolver.getInstance();
4243
configResolver.setApplicationContext(context);
4344

45+
// Register FirebasePerformance as a subscriber ASAP - which will start collecting gauges if the
46+
// FirebaseSession is verbose.
47+
FirebaseSessionsDependencies.register(new FirebasePerformanceSessionSubscriber(configResolver));
48+
4449
AppStateMonitor appStateMonitor = AppStateMonitor.getInstance();
4550
appStateMonitor.registerActivityLifecycleCallbacks(context);
4651
appStateMonitor.registerForAppColdStart(new FirebasePerformanceInitializer());
@@ -50,13 +55,5 @@ public FirebasePerfEarly(
5055
appStartTrace.registerActivityLifecycleCallbacks(context);
5156
uiExecutor.execute(new AppStartTrace.StartFromBackgroundRunnable(appStartTrace));
5257
}
53-
54-
// TODO: Bring back Firebase Sessions dependency to watch for updates to sessions.
55-
56-
// In the case of cold start, we create a session and start collecting gauges as early as
57-
// possible.
58-
// There is code in SessionManager that prevents us from resetting the session twice in case
59-
// of app cold start.
60-
SessionManager.getInstance().initializeGaugeCollection();
6158
}
6259
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
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;
3335
import java.util.Arrays;
3436
import java.util.List;
3537
import java.util.concurrent.Executor;
@@ -47,6 +49,11 @@ public class FirebasePerfRegistrar implements ComponentRegistrar {
4749
private static final String LIBRARY_NAME = "fire-perf";
4850
private static final String EARLY_LIBRARY_NAME = "fire-perf-early";
4951

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

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.firebase.perf.config.RemoteConfigManager;
3535
import com.google.firebase.perf.logging.AndroidLogger;
3636
import com.google.firebase.perf.logging.ConsoleUrlGenerator;
37+
import com.google.firebase.perf.logging.FirebaseSessionsEnforcementCheck;
3738
import com.google.firebase.perf.metrics.HttpMetric;
3839
import com.google.firebase.perf.metrics.Trace;
3940
import com.google.firebase.perf.session.SessionManager;
@@ -136,11 +137,6 @@ public static FirebasePerformance getInstance() {
136137
// to false if it's been force disabled or it is set to null if neither.
137138
@Nullable private Boolean mPerformanceCollectionForceEnabledState = null;
138139

139-
private final FirebaseApp firebaseApp;
140-
private final Provider<RemoteConfigComponent> firebaseRemoteConfigProvider;
141-
private final FirebaseInstallationsApi firebaseInstallationsApi;
142-
private final Provider<TransportFactory> transportFactoryProvider;
143-
144140
/**
145141
* Constructs the FirebasePerformance class and allows injecting dependencies.
146142
*
@@ -166,23 +162,18 @@ public static FirebasePerformance getInstance() {
166162
ConfigResolver configResolver,
167163
SessionManager sessionManager) {
168164

169-
this.firebaseApp = firebaseApp;
170-
this.firebaseRemoteConfigProvider = firebaseRemoteConfigProvider;
171-
this.firebaseInstallationsApi = firebaseInstallationsApi;
172-
this.transportFactoryProvider = transportFactoryProvider;
173-
174165
if (firebaseApp == null) {
175166
this.mPerformanceCollectionForceEnabledState = false;
176167
this.configResolver = configResolver;
177168
this.mMetadataBundle = new ImmutableBundle(new Bundle());
178169
return;
179170
}
171+
FirebaseSessionsEnforcementCheck.setEnforcement(BuildConfig.ENFORCE_LEGACY_SESSIONS);
180172

181173
TransportManager.getInstance()
182174
.initialize(firebaseApp, firebaseInstallationsApi, transportFactoryProvider);
183175

184176
Context appContext = firebaseApp.getApplicationContext();
185-
// TODO(b/110178816): Explore moving off of main thread.
186177
mMetadataBundle = extractMetadata(appContext);
187178

188179
remoteConfigManager.setFirebaseRemoteConfigProvider(firebaseRemoteConfigProvider);
@@ -192,6 +183,7 @@ public static FirebasePerformance getInstance() {
192183
sessionManager.setApplicationContext(appContext);
193184

194185
mPerformanceCollectionForceEnabledState = configResolver.getIsPerformanceCollectionEnabled();
186+
195187
if (logger.isLogcatEnabled() && isPerformanceCollectionEnabled()) {
196188
logger.info(
197189
String.format(
@@ -282,7 +274,7 @@ public synchronized void setPerformanceCollectionEnabled(@Nullable Boolean enabl
282274
return;
283275
}
284276

285-
if (configResolver.getIsPerformanceCollectionDeactivated()) {
277+
if (Boolean.TRUE.equals(configResolver.getIsPerformanceCollectionDeactivated())) {
286278
logger.info("Firebase Performance is permanently disabled");
287279
return;
288280
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
17+
package com.google.firebase.perf.logging
18+
19+
import com.google.firebase.perf.session.PerfSession
20+
import com.google.firebase.perf.session.isLegacy
21+
import com.google.firebase.perf.v1.PerfSession as ProtoPerfSession
22+
23+
class FirebaseSessionsEnforcementCheck {
24+
companion object {
25+
/** When enabled, failed preconditions will cause assertion errors for debugging. */
26+
@JvmStatic var enforcement: Boolean = false
27+
private var logger: AndroidLogger = AndroidLogger.getInstance()
28+
29+
@JvmStatic
30+
fun checkSession(sessions: List<ProtoPerfSession>, failureMessage: String) {
31+
sessions.forEach { checkSession(it.sessionId, failureMessage) }
32+
}
33+
34+
@JvmStatic
35+
fun checkSession(session: PerfSession, failureMessage: String) {
36+
checkSession(session.sessionId(), failureMessage)
37+
}
38+
39+
@JvmStatic
40+
fun checkSession(sessionId: String, failureMessage: String) {
41+
if (sessionId.isLegacy()) {
42+
logger.debug("legacy session ${sessionId}: $failureMessage")
43+
assert(!enforcement) { failureMessage }
44+
}
45+
}
46+
}
47+
}

firebase-perf/src/main/java/com/google/firebase/perf/metrics/AppStartTrace.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.firebase.perf.metrics;
1616

17+
import static com.google.firebase.perf.util.AppProcessesProvider.getAppProcesses;
18+
1719
import android.annotation.SuppressLint;
1820
import android.app.Activity;
1921
import android.app.ActivityManager;
@@ -509,17 +511,10 @@ public static boolean isAnyAppProcessInForeground(Context appContext) {
509511
if (activityManager == null) {
510512
return true;
511513
}
512-
List<ActivityManager.RunningAppProcessInfo> appProcesses =
513-
activityManager.getRunningAppProcesses();
514-
if (appProcesses != null) {
515-
String appProcessName = appContext.getPackageName();
516-
String allowedAppProcessNamePrefix = appProcessName + ":";
514+
List<ActivityManager.RunningAppProcessInfo> appProcesses = getAppProcesses(appContext);
515+
if (!appProcesses.isEmpty()) {
517516
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
518-
if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
519-
continue;
520-
}
521-
if (appProcess.processName.equals(appProcessName)
522-
|| appProcess.processName.startsWith(allowedAppProcessNamePrefix)) {
517+
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
523518
boolean isAppInForeground = true;
524519

525520
// For the case when the app is in foreground and the device transitions to sleep mode,

firebase-perf/src/main/java/com/google/firebase/perf/metrics/NetworkRequestMetricBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public NetworkRequestMetricBuilder setCustomAttributes(Map<String, String> attri
224224
* point depending upon the current {@link PerfSession} verbosity.
225225
*
226226
* @see GaugeManager#collectGaugeMetricOnce(Timer)
227-
* @see PerfSession#isGaugeAndEventCollectionEnabled()
227+
* @see PerfSession#isVerbose()
228228
*/
229229
public NetworkRequestMetricBuilder setRequestStartTimeMicros(long time) {
230230
SessionManager sessionManager = SessionManager.getInstance();
@@ -234,7 +234,7 @@ public NetworkRequestMetricBuilder setRequestStartTimeMicros(long time) {
234234
builder.setClientStartTimeUs(time);
235235
updateSession(perfSession);
236236

237-
if (perfSession.isGaugeAndEventCollectionEnabled()) {
237+
if (perfSession.isVerbose()) {
238238
gaugeManager.collectGaugeMetricOnce(perfSession.getTimer());
239239
}
240240

@@ -265,12 +265,12 @@ public long getTimeToResponseInitiatedMicros() {
265265
* point depending upon the current {@link PerfSession} Verbosity.
266266
*
267267
* @see GaugeManager#collectGaugeMetricOnce(Timer)
268-
* @see PerfSession#isGaugeAndEventCollectionEnabled()
268+
* @see PerfSession#isVerbose()
269269
*/
270270
public NetworkRequestMetricBuilder setTimeToResponseCompletedMicros(long time) {
271271
builder.setTimeToResponseCompletedUs(time);
272272

273-
if (SessionManager.getInstance().perfSession().isGaugeAndEventCollectionEnabled()) {
273+
if (SessionManager.getInstance().perfSession().isVerbose()) {
274274
gaugeManager.collectGaugeMetricOnce(SessionManager.getInstance().perfSession().getTimer());
275275
}
276276

firebase-perf/src/main/java/com/google/firebase/perf/metrics/Trace.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public void start() {
233233

234234
updateSession(perfSession);
235235

236-
if (perfSession.isGaugeAndEventCollectionEnabled()) {
236+
if (perfSession.isVerbose()) {
237237
gaugeManager.collectGaugeMetricOnce(perfSession.getTimer());
238238
}
239239
}
@@ -259,7 +259,7 @@ public void stop() {
259259
if (!name.isEmpty()) {
260260
transportManager.log(new TraceMetricBuilder(this).build(), getAppState());
261261

262-
if (SessionManager.getInstance().perfSession().isGaugeAndEventCollectionEnabled()) {
262+
if (SessionManager.getInstance().perfSession().isVerbose()) {
263263
gaugeManager.collectGaugeMetricOnce(
264264
SessionManager.getInstance().perfSession().getTimer());
265265
}

0 commit comments

Comments
 (0)