Skip to content

Commit a0ee2b2

Browse files
committed
More changes
1 parent 73a04e7 commit a0ee2b2

File tree

6 files changed

+47
-29
lines changed

6 files changed

+47
-29
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.google.firebase.perf.session
2+
3+
import com.google.firebase.perf.util.Constants
4+
import java.util.UUID
5+
6+
/**
7+
* Identifies whether the [PerfSession] is an AQS or not.
8+
*/
9+
fun PerfSession.isAQS(): Boolean {
10+
return !this.sessionId().startsWith(Constants.UNDEFINED_AQS_ID_PREFIX)
11+
}
12+
13+
@JvmOverloads fun createSessionId(aqsId: String = Constants.UNDEFINED_AQS_ID_PREFIX): String {
14+
if (aqsId == Constants.UNDEFINED_AQS_ID_PREFIX) {
15+
val uuid = UUID.randomUUID().toString().replace("-", "");
16+
return uuid.replaceRange(0, aqsId.length, aqsId)
17+
}
18+
19+
return aqsId;
20+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class FirebasePerformanceSessionSubscriber(override val isDataCollectionEnabled:
3030
val currentPerfSession = SessionManager.getInstance().perfSession()
3131

3232
// A [PerfSession] was created before a session was started.
33-
if (!currentPerfSession.isAqsReady) {
33+
if (!currentPerfSession.isAQS()) {
3434
GaugeManager.getInstance()
3535
.logGaugeMetadata(currentPerfSession.sessionId(), ApplicationProcessState.FOREGROUND)
3636
return

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,33 @@ public class PerfSession implements Parcelable {
3232
private final Timer creationTime;
3333
private final String sessionId;
3434
private boolean isGaugeAndEventCollectionEnabled = false;
35-
public final boolean isAqsReady;
3635

3736
/*
3837
* Creates a PerfSession object and decides what metrics to collect.
3938
*/
4039
public static PerfSession createWithId(@Nullable String aqsSessionId) {
4140
String sessionId;
42-
Boolean isAqsReady;
43-
if (aqsSessionId != null) {
44-
sessionId = aqsSessionId;
45-
isAqsReady = true;
41+
if (aqsSessionId == null) {
42+
sessionId = AqsUtilsKt.createSessionId();
4643
} else {
47-
sessionId = UUID.randomUUID().toString().replace("-", "");
48-
isAqsReady = false;
44+
sessionId = AqsUtilsKt.createSessionId(aqsSessionId);
4945
}
50-
PerfSession session = new PerfSession(sessionId, new Clock(), isAqsReady);
51-
session.setGaugeAndEventCollectionEnabled(shouldCollectGaugesAndEvents(sessionId));
46+
PerfSession session = new PerfSession(sessionId, new Clock());
47+
session.setGaugeAndEventCollectionEnabled(session.shouldCollectGaugesAndEvents());
5248
return session;
5349
}
5450

5551
/** Creates a PerfSession with the provided {@code sessionId} and {@code clock}. */
5652
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
57-
public PerfSession(String sessionId, Clock clock, boolean isAqsReady) {
53+
public PerfSession(String sessionId, Clock clock) {
5854
this.sessionId = sessionId;
59-
this.isAqsReady = isAqsReady;
6055
creationTime = clock.getTime();
6156
}
6257

6358
private PerfSession(@NonNull Parcel in) {
6459
super();
6560
sessionId = in.readString();
6661
isGaugeAndEventCollectionEnabled = in.readByte() != 0;
67-
isAqsReady = in.readByte() != 0;
6862
creationTime = in.readParcelable(Timer.class.getClassLoader());
6963
}
7064

@@ -160,10 +154,10 @@ public static com.google.firebase.perf.v1.PerfSession[] buildAndSort(
160154
}
161155

162156
/** If true, Session Gauge collection is enabled. */
163-
public static boolean shouldCollectGaugesAndEvents(String sessionId) {
157+
public boolean shouldCollectGaugesAndEvents() {
164158
ConfigResolver configResolver = ConfigResolver.getInstance();
165159
return configResolver.isPerformanceMonitoringEnabled()
166-
&& (Math.abs(sessionId.hashCode() % 100) < configResolver.getSessionsSamplingRate() * 100);
160+
&& (Math.abs(this.sessionId.hashCode() % 100) < configResolver.getSessionsSamplingRate() * 100);
167161
}
168162

169163
/**
@@ -187,7 +181,6 @@ public int describeContents() {
187181
public void writeToParcel(@NonNull Parcel out, int flags) {
188182
out.writeString(sessionId);
189183
out.writeByte((byte) (isGaugeAndEventCollectionEnabled ? 1 : 0));
190-
out.writeByte((byte) (isAqsReady ? 1 : 0));
191184
out.writeParcelable(creationTime, 0);
192185
}
193186

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void updateGaugeCollection(ApplicationProcessState applicationProcessStat
113113

114114
if (session == null
115115
|| !session.isGaugeAndEventCollectionEnabled()
116-
|| session.aqsSessionId().equals(Constants.UNDEFINED_AQS_ID)) {
116+
|| session.sessionId().equals(Constants.UNDEFINED_AQS_ID_PREFIX)) {
117117
logger.warn("Not starting gauge collection.");
118118
stopCollectingGauges();
119119
return;
@@ -230,7 +230,7 @@ public void stopCollectingGauges() {
230230
gaugeManagerDataCollectionJob.cancel(false);
231231
}
232232

233-
final String sessionIdForScheduledTask = session.aqsSessionId();
233+
final String sessionIdForScheduledTask = session.sessionId();
234234
this.session = null;
235235

236236
// Flush any data that was collected for this session one last time.
@@ -254,7 +254,7 @@ public void stopCollectingGauges() {
254254
* @param appState The app state for which these gauges are collected.
255255
*/
256256
private void syncFlush(String sessionId, ApplicationProcessState appState) {
257-
if (sessionId.equals(Constants.UNDEFINED_AQS_ID)) {
257+
if (sessionId.equals(Constants.UNDEFINED_AQS_ID_PREFIX)) {
258258
// TODO(b/394127311): Use DebugEnforcementCheck.
259259
logger.debug("Flushing gauge metrics to an undefined session ID.");
260260
}

firebase-perf/src/main/java/com/google/firebase/perf/util/Constants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public class Constants {
2222
public static final String PREFS_NAME = "FirebasePerfSharedPrefs";
2323
public static final String ENABLE_DISABLE = "isEnabled";
2424

25-
public static final String UNDEFINED_AQS_ID = "_uaqsid";
25+
// Contains non-hex characters and so guarantees it isn't an AQS.
26+
// https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.uuid/-uuid/
27+
public static final String UNDEFINED_AQS_ID_PREFIX = "noaqsid";
2628

2729
public static final double MIN_SAMPLING_RATE = 0.0;
2830
public static final double MAX_SAMPLING_RATE = 1.0;

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,20 @@ public void shouldCollectGaugesAndEvents_perfMonDisabledAtRuntime_sessionNotVerb
7676
Bundle bundle = new Bundle();
7777
bundle.putFloat("sessions_sampling_percentage", 100);
7878
configResolver.setMetadataBundle(new ImmutableBundle(bundle));
79+
PerfSession testSession = PerfSession.createWithId("aqsSessionId");
7980

8081
// By default, session is verbose if developer has set 100% of session verbosity.
81-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isTrue();
82+
assertThat(testSession.shouldCollectGaugesAndEvents()).isTrue();
8283

8384
// Case #1: developer has disabled Performance Monitoring during runtime.
8485
configResolver.setIsPerformanceCollectionEnabled(false);
8586

86-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isFalse();
87+
assertThat(testSession.shouldCollectGaugesAndEvents()).isFalse();
8788

8889
// Case #2: developer has enabled Performance Monitoring during runtime.
8990
configResolver.setIsPerformanceCollectionEnabled(true);
9091

91-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isTrue();
92+
assertThat(testSession.shouldCollectGaugesAndEvents()).isTrue();
9293
}
9394

9495
@Test
@@ -99,20 +100,21 @@ public void shouldCollectGaugesAndEvents_perfMonDisabledAtBuildtime_verbosityDep
99100
bundle.putFloat("sessions_sampling_percentage", 100);
100101
bundle.putBoolean("firebase_performance_collection_enabled", false);
101102
configResolver.setMetadataBundle(new ImmutableBundle(bundle));
103+
PerfSession testSession = PerfSession.createWithId("aqsSessionId");
102104

103105
// By default, session is not verbose if developer disabled performance monitoring at build
104106
// time.
105-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isFalse();
107+
assertThat(testSession.shouldCollectGaugesAndEvents()).isFalse();
106108

107109
// Case #1: developer has enabled Performance Monitoring during runtime.
108110
configResolver.setIsPerformanceCollectionEnabled(true);
109111

110-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isTrue();
112+
assertThat(testSession.shouldCollectGaugesAndEvents()).isTrue();
111113

112114
// Case #2: developer has disabled Performance Monitoring during runtime.
113115
configResolver.setIsPerformanceCollectionEnabled(false);
114116

115-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isFalse();
117+
assertThat(testSession.shouldCollectGaugesAndEvents()).isFalse();
116118
}
117119

118120
@Test
@@ -122,19 +124,20 @@ public void shouldCollectGaugesAndEvents_perfMonDeactivated_sessionNotVerbose()
122124
bundle.putFloat("sessions_sampling_percentage", 100);
123125
bundle.putBoolean("firebase_performance_collection_deactivated", true);
124126
configResolver.setMetadataBundle(new ImmutableBundle(bundle));
127+
PerfSession testSession = PerfSession.createWithId("aqsSessionId");
125128

126129
// Session will never be verbose if developer deactivated performance monitoring at build time.
127-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isFalse();
130+
assertThat(testSession.shouldCollectGaugesAndEvents()).isFalse();
128131

129132
// Case #1: developer has enabled Performance Monitoring during runtime.
130133
configResolver.setIsPerformanceCollectionEnabled(true);
131134

132-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isFalse();
135+
assertThat(testSession.shouldCollectGaugesAndEvents()).isFalse();
133136

134137
// Case #2: developer has disabled Performance Monitoring during runtime.
135138
configResolver.setIsPerformanceCollectionEnabled(false);
136139

137-
assertThat(PerfSession.shouldCollectGaugesAndEvents("sessionId")).isFalse();
140+
assertThat(testSession.shouldCollectGaugesAndEvents()).isFalse();
138141
}
139142

140143
@Test

0 commit comments

Comments
 (0)