Skip to content

Add more legacy session verification, move up gauge collection, and update comments. #7197

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 7 commits into from
Jul 30, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@ package com.google.firebase.perf.logging

import com.google.firebase.perf.session.PerfSession
import com.google.firebase.perf.session.isLegacy
import com.google.firebase.perf.v1.PerfSession as ProtoPerfSession

class FirebaseSessionsEnforcementCheck {
companion object {
/** When enabled, failed preconditions will cause assertion errors for debugging. */
@JvmStatic var enforcement: Boolean = false
private var logger: AndroidLogger = AndroidLogger.getInstance()

@JvmStatic
fun checkSession(sessions: List<ProtoPerfSession>, failureMessage: String) {
sessions.forEach { checkSession(it.sessionId, failureMessage) }
}

@JvmStatic
fun checkSession(session: PerfSession, failureMessage: String) {
if (session.isLegacy()) {
logger.debug("legacy session ${session.sessionId()}: $failureMessage")
checkSession(session.sessionId(), failureMessage)
}

@JvmStatic
fun checkSession(sessionId: String, failureMessage: String) {
if (sessionId.isLegacy()) {
logger.debug("legacy session ${sessionId}: $failureMessage")
assert(!enforcement) { failureMessage }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public final PerfSession perfSession() {
}

private SessionManager() {
// session should quickly updated by session subscriber.
// Creates a legacy session by default. This is a safety net to allow initializing
// SessionManager - but the current implementation replaces it immediately.
this(GaugeManager.getInstance(), PerfSession.createWithId(null));
FirebaseSessionsEnforcementCheck.checkSession(perfSession, "SessionManager()");
}
Expand Down Expand Up @@ -101,7 +102,11 @@ public void updatePerfSession(PerfSession perfSession) {

this.perfSession = perfSession;

// TODO(b/394127311): Update/verify behavior for Firebase Sessions.
// Start or stop the gauge data collection ASAP.
startOrStopCollectingGauges();

// Log gauge metadata.
logGaugeMetadataIfCollectionEnabled();

synchronized (clients) {
for (Iterator<WeakReference<SessionAwareObject>> i = clients.iterator(); i.hasNext(); ) {
Expand All @@ -115,12 +120,6 @@ public void updatePerfSession(PerfSession perfSession) {
}
}
}

// Log gauge metadata.
logGaugeMetadataIfCollectionEnabled();

// Start of stop the gauge data collection.
startOrStopCollectingGauges();
}

/**
Expand All @@ -129,6 +128,7 @@ public void updatePerfSession(PerfSession perfSession) {
* PerfSession} was already initialized a moment ago by getInstance(). Unlike updatePerfSession,
* this does not reset the perfSession.
*/
@Deprecated // TODO(b/394127311): Delete this. AQS early initialization updates the session ASAP.
public void initializeGaugeCollection() {
startOrStopCollectingGauges();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.firebase.perf.config.ConfigResolver;
import com.google.firebase.perf.logging.AndroidLogger;
import com.google.firebase.perf.logging.ConsoleUrlGenerator;
import com.google.firebase.perf.logging.FirebaseSessionsEnforcementCheck;
import com.google.firebase.perf.metrics.validator.PerfMetricValidator;
import com.google.firebase.perf.session.SessionManager;
import com.google.firebase.perf.util.Constants;
Expand Down Expand Up @@ -299,6 +300,8 @@ public void log(final TraceMetric traceMetric) {
* {@link #isAllowedToDispatch(PerfMetric)}).
*/
public void log(final TraceMetric traceMetric, final ApplicationProcessState appState) {
FirebaseSessionsEnforcementCheck.checkSession(
traceMetric.getPerfSessionsList(), "log TraceMetric");
executorService.execute(
() -> syncLog(PerfMetric.newBuilder().setTraceMetric(traceMetric), appState));
}
Expand Down Expand Up @@ -327,6 +330,8 @@ public void log(final NetworkRequestMetric networkRequestMetric) {
*/
public void log(
final NetworkRequestMetric networkRequestMetric, final ApplicationProcessState appState) {
FirebaseSessionsEnforcementCheck.checkSession(
networkRequestMetric.getPerfSessionsList(), "log NetworkRequestMetric");
executorService.execute(
() ->
syncLog(
Expand Down Expand Up @@ -356,6 +361,7 @@ public void log(final GaugeMetric gaugeMetric) {
* {@link #isAllowedToDispatch(PerfMetric)}).
*/
public void log(final GaugeMetric gaugeMetric, final ApplicationProcessState appState) {
FirebaseSessionsEnforcementCheck.checkSession(gaugeMetric.getSessionId(), "log GaugeMetric");
executorService.execute(
() -> syncLog(PerfMetric.newBuilder().setGaugeMetric(gaugeMetric), appState));
}
Expand Down
Loading