Skip to content

Commit 8cf5118

Browse files
committed
Refactor GaugeMetadata to be logged only when the session is verbose
1 parent bc42d60 commit 8cf5118

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package com.google.firebase.perf.session
1818

1919
import com.google.firebase.perf.logging.FirebaseSessionsEnforcementCheck
20-
import com.google.firebase.perf.session.gauges.GaugeManager
21-
import com.google.firebase.perf.v1.ApplicationProcessState
2220
import com.google.firebase.sessions.api.SessionSubscriber
2321

2422
class FirebasePerformanceSessionSubscriber(override val isDataCollectionEnabled: Boolean) :
@@ -33,7 +31,5 @@ class FirebasePerformanceSessionSubscriber(override val isDataCollectionEnabled:
3331

3432
val updatedSession = PerfSession.createWithId(sessionDetails.sessionId)
3533
SessionManager.getInstance().updatePerfSession(updatedSession)
36-
GaugeManager.getInstance()
37-
.logGaugeMetadata(updatedSession.sessionId(), ApplicationProcessState.FOREGROUND)
3834
}
3935
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public void updatePerfSession(PerfSession perfSession) {
115115
}
116116
}
117117

118+
// Log gauge metadata.
119+
logGaugeMetadataIfCollectionEnabled();
120+
118121
// Start of stop the gauge data collection.
119122
startOrStopCollectingGauges();
120123
}
@@ -153,6 +156,12 @@ public void unregisterForSessionUpdates(WeakReference<SessionAwareObject> client
153156
}
154157
}
155158

159+
private void logGaugeMetadataIfCollectionEnabled() {
160+
if (perfSession.isVerbose()) {
161+
gaugeManager.logGaugeMetadata(perfSession.sessionId());
162+
}
163+
}
164+
156165
private void startOrStopCollectingGauges() {
157166
FirebaseSessionsEnforcementCheck.checkSession(perfSession, "startOrStopCollectingGauges");
158167

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,18 +274,17 @@ private void syncFlush(String sessionId, ApplicationProcessState appState) {
274274
* Log the Gauge Metadata information to the transport.
275275
*
276276
* @param sessionId The {@link PerfSession#sessionId()} ()} to which the collected Gauge Metrics
277-
* should be associated with.
278-
* @param appState The {@link ApplicationProcessState} for which these gauges are collected.
277+
* should be associated with.
279278
* @return true if GaugeMetadata was logged, false otherwise.
280279
*/
281-
public boolean logGaugeMetadata(String sessionId, ApplicationProcessState appState) {
280+
public boolean logGaugeMetadata(String sessionId) {
282281
if (gaugeMetadataManager != null) {
283282
GaugeMetric gaugeMetric =
284283
GaugeMetric.newBuilder()
285284
.setSessionId(sessionId)
286285
.setGaugeMetadata(getGaugeMetadata())
287286
.build();
288-
transportManager.log(gaugeMetric, appState);
287+
transportManager.log(gaugeMetric, ApplicationProcessState.FOREGROUND);
289288
return true;
290289
}
291290
return false;

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ public void testGaugeCounterIsDecrementedWhenLogged() {
385385
// There's no job to log the gauges.
386386
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
387387

388-
// Generate metrics that don't exceed the GaugeCounter.MAX_COUNT.
389-
generateMetricsAndIncrementCounter(20);
388+
generateMetricsAndIncrementCounter(MAX_GAUGE_COUNTER_BEFORE_LOGGING - 10);
390389

391390
// There's still no job to log the gauges.
392391
assertThat(fakeScheduledExecutorService.isEmpty()).isTrue();
@@ -397,7 +396,8 @@ public void testGaugeCounterIsDecrementedWhenLogged() {
397396
assertThat(fakeScheduledExecutorService.getDelayToNextTask(TimeUnit.MILLISECONDS))
398397
.isEqualTo(TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS);
399398

400-
assertThat(GaugeCounter.count()).isEqualTo(priorGaugeCounter + 30);
399+
assertThat(GaugeCounter.count())
400+
.isEqualTo(priorGaugeCounter + MAX_GAUGE_COUNTER_BEFORE_LOGGING);
401401
fakeScheduledExecutorService.simulateSleepExecutingAtMostOneTask();
402402

403403
assertThat(GaugeCounter.count()).isEqualTo(priorGaugeCounter);
@@ -561,7 +561,7 @@ public void testLogGaugeMetadataSendDataToTransport() {
561561
when(fakeGaugeMetadataManager.getMaxAppJavaHeapMemoryKb()).thenReturn(1000);
562562
when(fakeGaugeMetadataManager.getMaxEncouragedAppJavaHeapMemoryKb()).thenReturn(800);
563563

564-
testGaugeManager.logGaugeMetadata(testSessionId(1), ApplicationProcessState.FOREGROUND);
564+
testGaugeManager.logGaugeMetadata(testSessionId(1));
565565

566566
GaugeMetric recordedGaugeMetric =
567567
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND);
@@ -588,9 +588,7 @@ public void testLogGaugeMetadataDoesNotLogWhenGaugeMetadataManagerNotAvailable()
588588
new Lazy<>(() -> fakeCpuGaugeCollector),
589589
new Lazy<>(() -> fakeMemoryGaugeCollector));
590590

591-
assertThat(
592-
testGaugeManager.logGaugeMetadata(testSessionId(1), ApplicationProcessState.FOREGROUND))
593-
.isFalse();
591+
assertThat(testGaugeManager.logGaugeMetadata(testSessionId(1))).isFalse();
594592
}
595593

596594
@Test
@@ -605,14 +603,10 @@ public void testLogGaugeMetadataLogsAfterApplicationContextIsSet() {
605603
new Lazy<>(() -> fakeCpuGaugeCollector),
606604
new Lazy<>(() -> fakeMemoryGaugeCollector));
607605

608-
assertThat(
609-
testGaugeManager.logGaugeMetadata(testSessionId(1), ApplicationProcessState.FOREGROUND))
610-
.isFalse();
606+
assertThat(testGaugeManager.logGaugeMetadata(testSessionId(1))).isFalse();
611607

612608
testGaugeManager.initializeGaugeMetadataManager(ApplicationProvider.getApplicationContext());
613-
assertThat(
614-
testGaugeManager.logGaugeMetadata(testSessionId(1), ApplicationProcessState.FOREGROUND))
615-
.isTrue();
609+
assertThat(testGaugeManager.logGaugeMetadata(testSessionId(1))).isTrue();
616610

617611
GaugeMetric recordedGaugeMetric =
618612
getLastRecordedGaugeMetric(ApplicationProcessState.FOREGROUND);

0 commit comments

Comments
 (0)