Skip to content

Commit bc01620

Browse files
committed
Attempt to simplify AQS session usage
1 parent 6855a97 commit bc01620

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
/** Details of a session including a unique Id and related information. */
3131
public class PerfSession implements Parcelable {
3232

33-
private final String internalSessionId;
33+
private static final String SESSION_ID_PREFIX = "fireperf-session";
34+
private final String sessionId;
3435
private final Timer creationTime;
3536

3637
private boolean isGaugeAndEventCollectionEnabled = false;
@@ -39,42 +40,39 @@ public class PerfSession implements Parcelable {
3940
* Creates a PerfSession object and decides what metrics to collect.
4041
*/
4142
public static PerfSession createNewSession() {
42-
String prunedSessionId = UUID.randomUUID().toString().replace("-", "");
43+
String prunedSessionId = SESSION_ID_PREFIX + UUID.randomUUID().toString().replace("-", "");
4344
PerfSession session = new PerfSession(prunedSessionId, new Clock());
4445
session.setGaugeAndEventCollectionEnabled(shouldCollectGaugesAndEvents());
45-
46-
// Every time a PerfSession is created, it sets the AQS to null. Once an AQS is received,
47-
// SessionManagerKt verifies if this is an active session, and sets the AQS session ID.
48-
// The assumption is that new PerfSessions *should* be limited to either App Start, or through
49-
// AQS.
50-
FirebasePerformanceSessionSubscriber.Companion.getInstance().reportPerfSession(prunedSessionId);
51-
5246
return session;
5347
}
5448

5549
/** Creates a PerfSession with the provided {@code sessionId} and {@code clock}. */
5650
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
57-
public PerfSession(String internalSessionId, Clock clock) {
58-
this.internalSessionId = internalSessionId;
51+
public PerfSession(String sessionId, Clock clock) {
52+
this.sessionId = sessionId;
5953
creationTime = clock.getTime();
54+
// Every time a PerfSession is created, it sets the AQS to null. Once an AQS is received,
55+
// SessionManagerKt verifies if this is an active session, and sets the AQS session ID.
56+
// The assumption is that new PerfSessions *should* be limited to either App Start, or through
57+
// AQS.
58+
FirebasePerformanceSessionSubscriber.Companion.getInstance().reportPerfSession(sessionId);
6059
}
6160

6261
private PerfSession(@NonNull Parcel in) {
6362
super();
64-
internalSessionId = in.readString();
63+
sessionId = in.readString();
6564
isGaugeAndEventCollectionEnabled = in.readByte() != 0;
6665
creationTime = in.readParcelable(Timer.class.getClassLoader());
6766
}
6867

6968
/** Returns the sessionId of the object. */
7069
public String sessionId() {
71-
return FirebasePerformanceSessionSubscriber.Companion.getInstance()
72-
.getAqsMappedToPerfSession(this.internalSessionId);
70+
return this.sessionId;
7371
}
7472

75-
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
76-
public String getInternalSessionId() {
77-
return internalSessionId;
73+
private String aqsSessionId() {
74+
return FirebasePerformanceSessionSubscriber.Companion.getInstance()
75+
.getAqsMappedToPerfSession(this.sessionId);
7876
}
7977

8078
/**
@@ -127,7 +125,7 @@ public boolean isSessionRunningTooLong() {
127125
/** Creates and returns the proto object for PerfSession object. */
128126
public com.google.firebase.perf.v1.PerfSession build() {
129127
com.google.firebase.perf.v1.PerfSession.Builder sessionMetric =
130-
com.google.firebase.perf.v1.PerfSession.newBuilder().setSessionId(sessionId());
128+
com.google.firebase.perf.v1.PerfSession.newBuilder().setSessionId(aqsSessionId());
131129

132130
// If gauge collection is enabled, enable gauge collection verbosity.
133131
if (isGaugeAndEventCollectionEnabled) {
@@ -202,7 +200,7 @@ public int describeContents() {
202200
* @param flags Additional flags about how the object should be written.
203201
*/
204202
public void writeToParcel(@NonNull Parcel out, int flags) {
205-
out.writeString(internalSessionId);
203+
out.writeString(sessionId);
206204
out.writeByte((byte) (isGaugeAndEventCollectionEnabled ? 1 : 0));
207205
out.writeParcelable(creationTime, 0);
208206
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public class SessionManager {
4040
@SuppressLint("StaticFieldLeak")
4141
private static final SessionManager instance = new SessionManager();
4242

43-
private static final String COLD_START_GAUGE_NAME = "coldstart";
44-
4543
private final GaugeManager gaugeManager;
4644
private final AppStateMonitor appStateMonitor;
4745
private final Set<WeakReference<SessionAwareObject>> clients = new HashSet<>();
@@ -115,8 +113,7 @@ public void stopGaugeCollectionIfSessionRunningTooLong() {
115113
*/
116114
public void updatePerfSession(PerfSession perfSession) {
117115
// Do not update the perf session if it is the exact same sessionId.
118-
if (Objects.equals(
119-
perfSession.getInternalSessionId(), this.perfSession.getInternalSessionId())) {
116+
if (Objects.equals(perfSession.sessionId(), this.perfSession.sessionId())) {
120117
return;
121118
}
122119

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.firebase.components.Lazy;
2323
import com.google.firebase.perf.config.ConfigResolver;
2424
import com.google.firebase.perf.logging.AndroidLogger;
25+
import com.google.firebase.perf.session.FirebasePerformanceSessionSubscriber;
2526
import com.google.firebase.perf.session.PerfSession;
2627
import com.google.firebase.perf.transport.TransportManager;
2728
import com.google.firebase.perf.util.Timer;
@@ -72,16 +73,16 @@ private GaugeManager() {
7273
TransportManager.getInstance(),
7374
ConfigResolver.getInstance(),
7475
null,
75-
new Lazy<>(CpuGaugeCollector::new),
76-
new Lazy<>(MemoryGaugeCollector::new));
76+
new Lazy<>(() -> new CpuGaugeCollector()),
77+
new Lazy<>(() -> new MemoryGaugeCollector()));
7778
}
7879

7980
@VisibleForTesting
8081
GaugeManager(
8182
Lazy<ScheduledExecutorService> gaugeManagerExecutor,
8283
TransportManager transportManager,
8384
ConfigResolver configResolver,
84-
@Nullable GaugeMetadataManager gaugeMetadataManager,
85+
GaugeMetadataManager gaugeMetadataManager,
8586
Lazy<CpuGaugeCollector> cpuGaugeCollector,
8687
Lazy<MemoryGaugeCollector> memoryGaugeCollector) {
8788

@@ -136,12 +137,11 @@ public void startCollectingGauges(
136137
final String sessionIdForScheduledTask = sessionId;
137138
final ApplicationProcessState applicationProcessStateForScheduledTask = applicationProcessState;
138139

139-
// TODO(b/394127311): Switch to using AQS.
140140
try {
141141
gaugeManagerDataCollectionJob =
142142
gaugeManagerExecutor
143143
.get()
144-
.scheduleWithFixedDelay(
144+
.scheduleAtFixedRate(
145145
() -> {
146146
syncFlush(sessionIdForScheduledTask, applicationProcessStateForScheduledTask);
147147
},
@@ -205,7 +205,6 @@ public void stopCollectingGauges() {
205205
gaugeManagerDataCollectionJob.cancel(false);
206206
}
207207

208-
// TODO(b/394127311): Switch to using AQS.
209208
// Flush any data that was collected for this session one last time.
210209
@SuppressWarnings("FutureReturnValueIgnored")
211210
ScheduledFuture unusedFuture =
@@ -244,21 +243,26 @@ private void syncFlush(String sessionId, ApplicationProcessState appState) {
244243
}
245244

246245
// Adding Session ID info.
247-
// TODO(b/394127311): Switch to using AQS.
248-
gaugeMetricBuilder.setSessionId(sessionId);
246+
String aqsSessionId =
247+
FirebasePerformanceSessionSubscriber.Companion.getInstance()
248+
.getAqsMappedToPerfSession(sessionId);
249+
gaugeMetricBuilder.setSessionId(aqsSessionId);
249250

250251
transportManager.log(gaugeMetricBuilder.build(), appState);
251252
}
252253

253254
/**
254255
* Log the Gauge Metadata information to the transport.
255256
*
256-
* @param aqsSessionId The {@link PerfSession#aqsSessionId()} ()} to which the collected Gauge Metrics
257+
* @param sessionId The {@link PerfSession#sessionId()} to which the collected Gauge Metrics
257258
* should be associated with.
258259
* @param appState The {@link ApplicationProcessState} for which these gauges are collected.
259260
* @return true if GaugeMetadata was logged, false otherwise.
260261
*/
261-
public boolean logGaugeMetadata(String aqsSessionId, ApplicationProcessState appState) {
262+
public boolean logGaugeMetadata(String sessionId, ApplicationProcessState appState) {
263+
String aqsSessionId =
264+
FirebasePerformanceSessionSubscriber.Companion.getInstance()
265+
.getAqsMappedToPerfSession(sessionId);
262266
if (gaugeMetadataManager != null) {
263267
GaugeMetric gaugeMetric =
264268
GaugeMetric.newBuilder()

0 commit comments

Comments
 (0)