@@ -116,12 +116,12 @@ public static synchronized GaugeManager getInstance() {
116116 }
117117
118118 /**
119- * Starts the collection of available gauges for the given {@code sessionId} and {@code
120- * applicationProcessState}. The collected Gauge Metrics will be flushed at regular intervals.
119+ * Starts the collection of available gauges for the given {@link PerfSession}.
120+ * The collected Gauge Metrics will be flushed by {@link GaugeCounter}
121121 *
122122 * <p>GaugeManager can only collect gauges for one session at a time, and if this method is called
123123 * again with the same or new sessionId while it's already collecting gauges, all future gauges
124- * will then be associated with the same or new sessionId and applicationProcessState .
124+ * will then be associated with the same or new sessionId.
125125 *
126126 * @param session The {@link PerfSession} to which the collected gauges will be associated with.
127127 * @note: This method is NOT thread safe - {@link this.startCollectingGauges()} and {@link
@@ -132,8 +132,15 @@ public void startCollectingGauges(PerfSession session) {
132132 stopCollectingGauges ();
133133 }
134134
135+ ApplicationProcessState gaugeCollectionApplicationProcessState = applicationProcessState ;
136+ if (gaugeCollectionApplicationProcessState == ApplicationProcessState .APPLICATION_PROCESS_STATE_UNKNOWN ) {
137+ logger .warn ("Start collecting gauges with APPLICATION_PROCESS_STATE_UNKNOWN" );
138+ // Since the application process state is unknown, collect gauges at the foreground frequency.
139+ gaugeCollectionApplicationProcessState = ApplicationProcessState .FOREGROUND ;
140+ }
141+
135142 long collectionFrequency =
136- startCollectingGauges (this . applicationProcessState , session .getTimer ());
143+ startCollectingGauges (gaugeCollectionApplicationProcessState , session .getTimer ());
137144 if (collectionFrequency == INVALID_GAUGE_COLLECTION_FREQUENCY ) {
138145 logger .warn ("Invalid gauge collection frequency. Unable to start collecting Gauges." );
139146 return ;
@@ -185,10 +192,17 @@ public void stopCollectingGauges() {
185192 memoryGaugeCollector .get ().stopCollecting ();
186193
187194 logGaugeMetrics ();
195+
196+ // TODO(b/394127311): There might be a race condition where a final metric is collected, but
197+ // isn't uploaded.
188198 GaugeCounter .Companion .getInstance ().resetCounter ();
189199 this .session = null ;
190200 }
191201
202+ /**
203+ * Logs the existing GaugeMetrics to Firelog, associates it with the current {@link PerfSession}
204+ * and {@link ApplicationProcessState}.
205+ */
192206 protected boolean logGaugeMetrics () {
193207 if (session == null ) {
194208 logger .warn ("Attempted to log Gauge Metrics when session was null." );
0 commit comments