Skip to content

Commit 4c637aa

Browse files
committed
Re-introduce gauge metadata collection
1 parent 3e03785 commit 4c637aa

File tree

5 files changed

+22
-62
lines changed

5 files changed

+22
-62
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.google.firebase.perf.session
22

33
import com.google.firebase.perf.config.ConfigResolver
44
import com.google.firebase.perf.logging.AndroidLogger
5+
import com.google.firebase.perf.session.gauges.GaugeManager
56
import com.google.firebase.sessions.api.SessionSubscriber
67

78
class FirebasePerformanceSessionSubscriber(private val dataCollectionEnabled: Boolean) :
@@ -29,6 +30,9 @@ class FirebasePerformanceSessionSubscriber(private val dataCollectionEnabled: Bo
2930
SessionManager.getInstance().updatePerfSession(newSession)
3031
perfSessionToAqs[newSession.sessionId()] = sessionDetails
3132
}
33+
34+
// Always log GaugeMetadata when a session changes.
35+
GaugeManager.getInstance().logGaugeMetadata(sessionDetails.sessionId)
3236
}
3337

3438
fun reportPerfSession(perfSessionId: String) {

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import java.util.Iterator;
3030
import java.util.Objects;
3131
import java.util.Set;
32-
import java.util.concurrent.ExecutorService;
33-
import java.util.concurrent.Executors;
3432
import java.util.concurrent.Future;
3533

3634
/** Session manager to generate sessionIDs and broadcast to the application. */
@@ -76,14 +74,7 @@ public SessionManager(
7674
* (currently that is before onResume finishes) to ensure gauge collection starts on time.
7775
*/
7876
public void setApplicationContext(final Context appContext) {
79-
// TODO(b/258263016): Migrate to go/firebase-android-executors
80-
@SuppressLint("ThreadPoolCreation")
81-
ExecutorService executorService = Executors.newSingleThreadExecutor();
82-
syncInitFuture =
83-
executorService.submit(
84-
() -> {
85-
gaugeManager.initializeGaugeMetadataManager(appContext);
86-
});
77+
gaugeManager.initializeGaugeMetadataManager(appContext, ApplicationProcessState.FOREGROUND);
8778
}
8879

8980
/**

firebase-perf/src/main/java/com/google/firebase/perf/session/gauges/GaugeManager.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ private GaugeManager() {
9595
}
9696

9797
/** Initializes GaugeMetadataManager which requires application context. */
98-
public void initializeGaugeMetadataManager(Context appContext) {
98+
public void initializeGaugeMetadataManager(
99+
Context appContext, ApplicationProcessState applicationProcessState) {
99100
this.gaugeMetadataManager = new GaugeMetadataManager(appContext);
101+
this.applicationProcessState = applicationProcessState;
100102
}
101103

102104
/** Returns the singleton instance of this class. */
@@ -255,28 +257,20 @@ private void syncFlush(String sessionId, ApplicationProcessState appState) {
255257
/**
256258
* Log the Gauge Metadata information to the transport.
257259
*
258-
* @param sessionId The {@link PerfSession#sessionId()} to which the collected Gauge Metrics
260+
* @param aqsSessionId The {@link FirebasePerformanceSessionSubscriber#getAqsMappedToPerfSession(String)} to which the collected Gauge Metrics
259261
* should be associated with.
260-
* @param appState The {@link ApplicationProcessState} for which these gauges are collected.
261262
* @return true if GaugeMetadata was logged, false otherwise.
262263
*/
263-
public boolean logGaugeMetadata(String sessionId, ApplicationProcessState appState) {
264-
// TODO(b/394127311): Re-introduce logging of metadata for AQS.
265-
String aqsSessionId =
266-
FirebasePerformanceSessionSubscriber.Companion.getInstance()
267-
.getAqsMappedToPerfSession(sessionId);
268-
AndroidLogger.getInstance()
269-
.debug("CFPR logGaugeMetadata: " + sessionId + " AQS: " + aqsSessionId);
270-
if (gaugeMetadataManager != null) {
271-
GaugeMetric gaugeMetric =
272-
GaugeMetric.newBuilder()
273-
.setSessionId(aqsSessionId)
274-
.setGaugeMetadata(getGaugeMetadata())
275-
.build();
276-
transportManager.log(gaugeMetric, appState);
277-
return true;
278-
}
279-
return false;
264+
public void logGaugeMetadata(String aqsSessionId) {
265+
// TODO(b/394127311): This can now throw an NPE. Explore if there's anything that should be
266+
// verified.
267+
AndroidLogger.getInstance().debug("CFPR logGaugeMetadata: " + aqsSessionId);
268+
GaugeMetric gaugeMetric =
269+
GaugeMetric.newBuilder()
270+
.setSessionId(aqsSessionId)
271+
.setGaugeMetadata(getGaugeMetadata())
272+
.build();
273+
transportManager.log(gaugeMetric, this.applicationProcessState);
280274
}
281275

282276
private GaugeMetadata getGaugeMetadata() {

firebase-perf/src/test/java/com/google/firebase/perf/session/SessionManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public void setApplicationContext_logGaugeMetadata_afterGaugeMetadataManagerIsIn
7575
testSessionManager.setApplicationContext(mockApplicationContext);
7676

7777
testSessionManager.getSyncInitFuture().get();
78-
inOrder.verify(mockGaugeManager).initializeGaugeMetadataManager(any());
79-
inOrder.verify(mockGaugeManager).logGaugeMetadata(any(), any());
78+
inOrder.verify(mockGaugeManager).initializeGaugeMetadataManager(any(), any());
79+
inOrder.verify(mockGaugeManager).logGaugeMetadata(any());
8080
}
8181

8282
@Test

firebase-perf/src/test/java/com/google/firebase/perf/session/gauges/GaugeManagerTest.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public void testLogGaugeMetadataSendDataToTransport() {
641641
when(fakeGaugeMetadataManager.getMaxAppJavaHeapMemoryKb()).thenReturn(1000);
642642
when(fakeGaugeMetadataManager.getMaxEncouragedAppJavaHeapMemoryKb()).thenReturn(800);
643643

644-
testGaugeManager.logGaugeMetadata("sessionId", ApplicationProcessState.FOREGROUND);
644+
testGaugeManager.logGaugeMetadata("sessionId");
645645

646646
GaugeMetric recordedGaugeMetric =
647647
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND, 1);
@@ -668,35 +668,6 @@ public void testLogGaugeMetadataDoesntLogWhenGaugeMetadataManagerNotAvailable()
668668
/* gaugeMetadataManager= */ null,
669669
new Lazy<>(() -> fakeCpuGaugeCollector),
670670
new Lazy<>(() -> fakeMemoryGaugeCollector));
671-
672-
assertThat(testGaugeManager.logGaugeMetadata("sessionId", ApplicationProcessState.FOREGROUND))
673-
.isFalse();
674-
}
675-
676-
@Test
677-
public void testLogGaugeMetadataLogsAfterApplicationContextIsSet() {
678-
679-
testGaugeManager =
680-
new GaugeManager(
681-
new Lazy<>(() -> fakeScheduledExecutorService),
682-
mockTransportManager,
683-
mockConfigResolver,
684-
/* gaugeMetadataManager= */ null,
685-
new Lazy<>(() -> fakeCpuGaugeCollector),
686-
new Lazy<>(() -> fakeMemoryGaugeCollector));
687-
688-
assertThat(testGaugeManager.logGaugeMetadata("sessionId", ApplicationProcessState.FOREGROUND))
689-
.isFalse();
690-
691-
testGaugeManager.initializeGaugeMetadataManager(ApplicationProvider.getApplicationContext());
692-
assertThat(testGaugeManager.logGaugeMetadata("sessionId", ApplicationProcessState.FOREGROUND))
693-
.isTrue();
694-
695-
GaugeMetric recordedGaugeMetric =
696-
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND, 1);
697-
GaugeMetadata recordedGaugeMetadata = recordedGaugeMetric.getGaugeMetadata();
698-
699-
assertThat(recordedGaugeMetric.getSessionId()).isEqualTo("sessionId");
700671
}
701672

702673
@Test

0 commit comments

Comments
 (0)