Skip to content

Commit fbd31de

Browse files
committed
Changes in GaugeCounter
1 parent 8900e3f commit fbd31de

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import android.content.Context;
1919
import androidx.annotation.Keep;
2020
import androidx.annotation.VisibleForTesting;
21-
import com.google.firebase.perf.application.AppStateMonitor;
2221
import com.google.firebase.perf.logging.FirebaseSessionsEnforcementCheck;
2322
import com.google.firebase.perf.session.gauges.GaugeManager;
2423
import com.google.firebase.perf.v1.GaugeMetadata;
@@ -47,8 +46,7 @@ public static SessionManager getInstance() {
4746

4847
/** Returns the currently active PerfSession. */
4948
public final PerfSession perfSession() {
50-
FirebaseSessionsEnforcementCheck.checkSession(
51-
perfSession, "PerfSession.perfSession()");
49+
FirebaseSessionsEnforcementCheck.checkSession(perfSession, "PerfSession.perfSession()");
5250

5351
return perfSession;
5452
}
@@ -59,8 +57,7 @@ private SessionManager() {
5957
}
6058

6159
@VisibleForTesting
62-
public SessionManager(
63-
GaugeManager gaugeManager, PerfSession perfSession) {
60+
public SessionManager(GaugeManager gaugeManager, PerfSession perfSession) {
6461
this.gaugeManager = gaugeManager;
6562
this.perfSession = perfSession;
6663
}
@@ -80,8 +77,7 @@ public void setApplicationContext(final Context appContext) {
8077
*/
8178
public void stopGaugeCollectionIfSessionRunningTooLong() {
8279
FirebaseSessionsEnforcementCheck.checkSession(
83-
perfSession,
84-
"SessionManager.stopGaugeCollectionIfSessionRunningTooLong");
80+
perfSession, "SessionManager.stopGaugeCollectionIfSessionRunningTooLong");
8581

8682
if (perfSession.isSessionRunningTooLong()) {
8783
gaugeManager.stopCollectingGauges();
@@ -158,8 +154,7 @@ public void unregisterForSessionUpdates(WeakReference<SessionAwareObject> client
158154
}
159155

160156
private void startOrStopCollectingGauges() {
161-
FirebaseSessionsEnforcementCheck.checkSession(
162-
perfSession, "startOrStopCollectingGauges");
157+
FirebaseSessionsEnforcementCheck.checkSession(perfSession, "startOrStopCollectingGauges");
163158

164159
if (perfSession.isVerbose()) {
165160
gaugeManager.startCollectingGauges(perfSession);

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@ package com.google.firebase.perf.session.gauges
22

33
import java.util.concurrent.atomic.AtomicInteger
44

5-
5+
/**
6+
* [GaugeCounter] is a threadsafe counter for gauge metrics. If the metrics count exceeds
7+
* [MAX_METRIC_COUNT], it attempts to log the metrics to Firelog.
8+
*/
69
class GaugeCounter private constructor() {
7-
private val counter = AtomicInteger(0)
8-
private val gaugeManager: GaugeManager = GaugeManager.getInstance()
10+
private val counter = AtomicInteger(0)
11+
private val gaugeManager: GaugeManager = GaugeManager.getInstance()
912

10-
fun incrementCounter() {
11-
val metricsCount = counter.incrementAndGet()
13+
fun incrementCounter() {
14+
val metricsCount = counter.incrementAndGet()
1215

13-
if (metricsCount >= MAX_METRIC_COUNT && gaugeManager.logGaugeMetrics()) {
14-
counter.set(0)
15-
}
16+
if (metricsCount >= MAX_METRIC_COUNT) {
17+
gaugeManager.logGaugeMetrics()
1618
}
19+
}
1720

18-
fun resetCounter() {
19-
counter.set(0)
20-
}
21+
fun decrementCounter() {
22+
counter.decrementAndGet()
23+
}
2124

22-
companion object {
23-
val instance: GaugeCounter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { GaugeCounter() }
24-
const val MAX_METRIC_COUNT = 25
25-
}
25+
companion object {
26+
val instance: GaugeCounter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { GaugeCounter() }
27+
const val MAX_METRIC_COUNT = 25
28+
}
2629
}

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class GaugeManager extends AppStateUpdateHandler {
4949
// This is a guesstimate of the max amount of time to wait before any pending metrics' collection
5050
// might take.
5151
private static final long TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS = 20;
52-
private static final long APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC = 20;
5352
private static final long INVALID_GAUGE_COLLECTION_FREQUENCY = -1;
5453

5554
private final Lazy<ScheduledExecutorService> gaugeManagerExecutor;
@@ -59,6 +58,7 @@ public class GaugeManager extends AppStateUpdateHandler {
5958
private final TransportManager transportManager;
6059

6160
@Nullable private GaugeMetadataManager gaugeMetadataManager;
61+
@Nullable private ScheduledFuture<?> gaugeManagerDataCollectionJob = null;
6262
@Nullable private PerfSession session = null;
6363
private ApplicationProcessState applicationProcessState =
6464
ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN;
@@ -133,7 +133,8 @@ public void startCollectingGauges(PerfSession session) {
133133
}
134134

135135
ApplicationProcessState gaugeCollectionApplicationProcessState = applicationProcessState;
136-
if (gaugeCollectionApplicationProcessState == ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN) {
136+
if (gaugeCollectionApplicationProcessState
137+
== ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN) {
137138
logger.warn("Start collecting gauges with APPLICATION_PROCESS_STATE_UNKNOWN");
138139
// Since the application process state is unknown, collect gauges at the foreground frequency.
139140
gaugeCollectionApplicationProcessState = ApplicationProcessState.FOREGROUND;
@@ -192,39 +193,43 @@ public void stopCollectingGauges() {
192193
memoryGaugeCollector.get().stopCollecting();
193194

194195
logGaugeMetrics();
195-
196-
// TODO(b/394127311): There might be a race condition where a final metric is collected, but
197-
// isn't uploaded.
198-
GaugeCounter.Companion.getInstance().resetCounter();
199196
this.session = null;
200197
}
201198

202199
/**
203200
* Logs the existing GaugeMetrics to Firelog, associates it with the current {@link PerfSession}
204201
* and {@link ApplicationProcessState}.
202+
*
203+
* @return true if a new data collection job is started, false otherwise.
205204
*/
206205
protected boolean logGaugeMetrics() {
207206
if (session == null) {
208-
logger.warn("Attempted to log Gauge Metrics when session was null.");
207+
logger.debug("Attempted to log Gauge Metrics when session was null.");
208+
return false;
209+
}
210+
211+
// If there's an existing gaugeManagerDataCollectionJob, this is a no-op.
212+
if (gaugeManagerDataCollectionJob != null && !gaugeManagerDataCollectionJob.isDone()) {
213+
logger.debug("Attempted to start an additional gauge logging operation.");
209214
return false;
210215
}
211216

212217
logExistingGaugeMetrics(session.sessionId(), applicationProcessState);
213218
return true;
214219
}
215220

216-
private void logExistingGaugeMetrics(String sessionId, ApplicationProcessState applicationProcessState) {
221+
private void logExistingGaugeMetrics(
222+
String sessionId, ApplicationProcessState applicationProcessState) {
217223
// Flush any data that was collected and attach it to the given session and app state.
218-
@SuppressWarnings("FutureReturnValueIgnored")
219-
ScheduledFuture<?> unusedFuture =
220-
gaugeManagerExecutor
221-
.get()
222-
.schedule(
223-
() -> {
224-
syncFlush(sessionId, applicationProcessState);
225-
},
226-
TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS,
227-
TimeUnit.MILLISECONDS);
224+
gaugeManagerDataCollectionJob =
225+
gaugeManagerExecutor
226+
.get()
227+
.schedule(
228+
() -> {
229+
syncFlush(sessionId, applicationProcessState);
230+
},
231+
TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS,
232+
TimeUnit.MILLISECONDS);
228233
}
229234

230235
/**
@@ -236,16 +241,19 @@ private void logExistingGaugeMetrics(String sessionId, ApplicationProcessState a
236241
*/
237242
private void syncFlush(String sessionId, ApplicationProcessState appState) {
238243
GaugeMetric.Builder gaugeMetricBuilder = GaugeMetric.newBuilder();
244+
GaugeCounter gaugeCounter = GaugeCounter.Companion.getInstance();
239245

240246
// Adding CPU metric readings.
241247
while (!cpuGaugeCollector.get().cpuMetricReadings.isEmpty()) {
242248
gaugeMetricBuilder.addCpuMetricReadings(cpuGaugeCollector.get().cpuMetricReadings.poll());
249+
gaugeCounter.decrementCounter();
243250
}
244251

245252
// Adding Memory metric readings.
246253
while (!memoryGaugeCollector.get().memoryMetricReadings.isEmpty()) {
247254
gaugeMetricBuilder.addAndroidMemoryReadings(
248255
memoryGaugeCollector.get().memoryMetricReadings.poll());
256+
gaugeCounter.decrementCounter();
249257
}
250258

251259
// Adding Session ID info.

0 commit comments

Comments
 (0)