2121import androidx .annotation .VisibleForTesting ;
2222import com .google .firebase .perf .config .ConfigResolver ;
2323import com .google .firebase .perf .util .Clock ;
24- import com .google .firebase .perf .util .Constants ;
2524import com .google .firebase .perf .util .Timer ;
2625import com .google .firebase .perf .v1 .SessionVerbosity ;
27- import com .google .firebase .sessions .api .SessionSubscriber ;
2826import java .util .List ;
27+ import java .util .UUID ;
2928import java .util .concurrent .TimeUnit ;
3029
3130/** Details of a session including a unique Id and related information. */
3231public class PerfSession implements Parcelable {
33-
34- private final String sessionId ;
3532 private final Timer creationTime ;
36- // TODO(b/394127311): Remove this once this isn't needed.
37- private String aqsSessionId = Constants .UNDEFINED_AQS_ID ;
38-
33+ private final String sessionId ;
3934 private boolean isGaugeAndEventCollectionEnabled = false ;
35+ public final boolean isAqsReady ;
4036
4137 /*
4238 * Creates a PerfSession object and decides what metrics to collect.
4339 */
44- public static PerfSession createWithId (@ NonNull String sessionId ) {
45- String prunedSessionId = sessionId .replace ("-" , "" );
46- PerfSession session = new PerfSession (prunedSessionId , new Clock ());
47- session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents ());
48-
40+ public static PerfSession createWithId (@ Nullable String aqsSessionId ) {
41+ String sessionId ;
42+ Boolean isAqsReady ;
43+ if (aqsSessionId != null ) {
44+ sessionId = aqsSessionId ;
45+ isAqsReady = true ;
46+ } else {
47+ sessionId = UUID .randomUUID ().toString ().replace ("-" , "" );
48+ isAqsReady = false ;
49+ }
50+ PerfSession session = new PerfSession (sessionId , new Clock (), isAqsReady );
51+ session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents (sessionId ));
4952 return session ;
5053 }
5154
5255 /** Creates a PerfSession with the provided {@code sessionId} and {@code clock}. */
5356 @ VisibleForTesting (otherwise = VisibleForTesting .PACKAGE_PRIVATE )
54- public PerfSession (String sessionId , Clock clock ) {
57+ public PerfSession (String sessionId , Clock clock , boolean isAqsReady ) {
5558 this .sessionId = sessionId ;
59+ this .isAqsReady = isAqsReady ;
5660 creationTime = clock .getTime ();
5761 }
5862
5963 private PerfSession (@ NonNull Parcel in ) {
6064 super ();
6165 sessionId = in .readString ();
6266 isGaugeAndEventCollectionEnabled = in .readByte () != 0 ;
67+ isAqsReady = in .readByte () != 0 ;
6368 creationTime = in .readParcelable (Timer .class .getClassLoader ());
6469 }
6570
66- /** Returns the sessionId of the session. */
71+ /** Returns the sessionId for the given session. */
6772 public String sessionId () {
6873 return sessionId ;
6974 }
7075
71- /** Returns the AQS sessionId for the given session. */
72- public String aqsSessionId () {
73- // This is a fallback for if/when the AQS ID is undefined.
74- if (aqsSessionId .equals (Constants .UNDEFINED_AQS_ID )) {
75- // TODO(b/394127311): Explore returning a valid - but different ID.
76- return Constants .UNDEFINED_AQS_ID ;
77- }
78- return aqsSessionId ;
79- }
80-
81- /** Returns the AQS sessionId for the given session. */
82- public void setAQSId (SessionSubscriber .SessionDetails aqs ) {
83- if (aqsSessionId .equals (Constants .UNDEFINED_AQS_ID )) {
84- aqsSessionId = aqs .getSessionId ();
85- }
86- }
87-
8876 /**
8977 * Returns a timer object that has been seeded with the system time at which the session began.
9078 */
@@ -111,18 +99,6 @@ public boolean isVerbose() {
11199 return isGaugeAndEventCollectionEnabled ;
112100 }
113101
114- /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */
115- @ VisibleForTesting
116- static boolean isVerbose (@ NonNull com .google .firebase .perf .v1 .PerfSession perfSession ) {
117- for (SessionVerbosity sessionVerbosity : perfSession .getSessionVerbosityList ()) {
118- if (sessionVerbosity == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
119- return true ;
120- }
121- }
122-
123- return false ;
124- }
125-
126102 /**
127103 * Checks if it has been more than {@link ConfigResolver#getSessionsMaxDurationMinutes()} time
128104 * since the creation time of the current session.
@@ -134,9 +110,8 @@ public boolean isSessionRunningTooLong() {
134110
135111 /** Creates and returns the proto object for PerfSession object. */
136112 public com .google .firebase .perf .v1 .PerfSession build () {
137- // TODO(b/394127311): Switch to using AQS.
138113 com .google .firebase .perf .v1 .PerfSession .Builder sessionMetric =
139- com .google .firebase .perf .v1 .PerfSession .newBuilder ().setSessionId (aqsSessionId );
114+ com .google .firebase .perf .v1 .PerfSession .newBuilder ().setSessionId (sessionId );
140115
141116 // If gauge collection is enabled, enable gauge collection verbosity.
142117 if (isGaugeAndEventCollectionEnabled ) {
@@ -185,11 +160,10 @@ public static com.google.firebase.perf.v1.PerfSession[] buildAndSort(
185160 }
186161
187162 /** If true, Session Gauge collection is enabled. */
188- public static boolean shouldCollectGaugesAndEvents () {
163+ public static boolean shouldCollectGaugesAndEvents (String sessionId ) {
189164 ConfigResolver configResolver = ConfigResolver .getInstance ();
190-
191165 return configResolver .isPerformanceMonitoringEnabled ()
192- && Math .random () < configResolver .getSessionsSamplingRate ();
166+ && ( Math .abs ( sessionId . hashCode () % 100 ) < configResolver .getSessionsSamplingRate () * 100 );
193167 }
194168
195169 /**
@@ -213,6 +187,7 @@ public int describeContents() {
213187 public void writeToParcel (@ NonNull Parcel out , int flags ) {
214188 out .writeString (sessionId );
215189 out .writeByte ((byte ) (isGaugeAndEventCollectionEnabled ? 1 : 0 ));
190+ out .writeByte ((byte ) (isAqsReady ? 1 : 0 ));
216191 out .writeParcelable (creationTime , 0 );
217192 }
218193
@@ -230,4 +205,16 @@ public PerfSession[] newArray(int size) {
230205 return new PerfSession [size ];
231206 }
232207 };
208+
209+ /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */
210+ @ VisibleForTesting
211+ static boolean isVerbose (@ NonNull com .google .firebase .perf .v1 .PerfSession perfSession ) {
212+ for (SessionVerbosity sessionVerbosity : perfSession .getSessionVerbosityList ()) {
213+ if (sessionVerbosity == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
214+ return true ;
215+ }
216+ }
217+
218+ return false ;
219+ }
233220}
0 commit comments