23
23
import com .google .firebase .perf .util .Clock ;
24
24
import com .google .firebase .perf .util .Timer ;
25
25
import com .google .firebase .perf .v1 .SessionVerbosity ;
26
- import com .google .firebase .sessions .api .SessionSubscriber ;
27
26
import java .util .List ;
27
+ import java .util .UUID ;
28
28
import java .util .concurrent .TimeUnit ;
29
29
30
30
/** Details of a session including a unique Id and related information. */
31
31
public class PerfSession implements Parcelable {
32
-
33
- private final String sessionId ;
34
32
private final Timer creationTime ;
35
- @ Nullable private String aqsSessionId ;
36
-
33
+ private final String sessionId ;
37
34
private boolean isGaugeAndEventCollectionEnabled = false ;
35
+ public final boolean isAqsReady ;
38
36
39
37
/*
40
38
* Creates a PerfSession object and decides what metrics to collect.
41
39
*/
42
- public static PerfSession createWithId (@ NonNull String sessionId ) {
43
- String prunedSessionId = sessionId .replace ("-" , "" );
44
- PerfSession session = new PerfSession (prunedSessionId , new Clock ());
45
- session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents ());
46
-
40
+ public static PerfSession createWithId (@ Nullable String aqsSessionId ) {
41
+ String sessionId = UUID .randomUUID ().toString ().replace ("-" , "" );
42
+ Boolean isAqsReady = false ;
43
+ if (aqsSessionId != null ) {
44
+ sessionId = aqsSessionId ;
45
+ isAqsReady = true ;
46
+ }
47
+ PerfSession session = new PerfSession (sessionId , new Clock (), isAqsReady );
48
+ session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents (sessionId ));
47
49
return session ;
48
50
}
49
51
50
52
/** Creates a PerfSession with the provided {@code sessionId} and {@code clock}. */
51
53
@ VisibleForTesting (otherwise = VisibleForTesting .PACKAGE_PRIVATE )
52
- public PerfSession (String sessionId , Clock clock ) {
54
+ public PerfSession (String sessionId , Clock clock , boolean isAqsReady ) {
53
55
this .sessionId = sessionId ;
56
+ this .isAqsReady = isAqsReady ;
54
57
creationTime = clock .getTime ();
55
58
}
56
59
57
60
private PerfSession (@ NonNull Parcel in ) {
58
61
super ();
59
62
sessionId = in .readString ();
60
63
isGaugeAndEventCollectionEnabled = in .readByte () != 0 ;
64
+ isAqsReady = in .readByte () != 0 ;
61
65
creationTime = in .readParcelable (Timer .class .getClassLoader ());
62
66
}
63
67
64
- /** Returns the sessionId of the session. */
68
+ /** Returns the sessionId for the given session. */
65
69
public String sessionId () {
66
70
return sessionId ;
67
71
}
68
72
69
- /** Returns the AQS sessionId for the given session. */
70
- @ Nullable
71
- public String aqsSessionId () {
72
- return aqsSessionId ;
73
- }
74
-
75
- /** Sets the AQS sessionId for the given session. */
76
- public void setAQSId (SessionSubscriber .SessionDetails aqs ) {
77
- if (aqsSessionId == null ) {
78
- aqsSessionId = aqs .getSessionId ();
79
- }
80
- }
81
-
82
73
/**
83
74
* Returns a timer object that has been seeded with the system time at which the session began.
84
75
*/
@@ -105,18 +96,6 @@ public boolean isVerbose() {
105
96
return isGaugeAndEventCollectionEnabled ;
106
97
}
107
98
108
- /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */
109
- @ VisibleForTesting
110
- static boolean isVerbose (@ NonNull com .google .firebase .perf .v1 .PerfSession perfSession ) {
111
- for (SessionVerbosity sessionVerbosity : perfSession .getSessionVerbosityList ()) {
112
- if (sessionVerbosity == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
113
- return true ;
114
- }
115
- }
116
-
117
- return false ;
118
- }
119
-
120
99
/**
121
100
* Checks if it has been more than {@link ConfigResolver#getSessionsMaxDurationMinutes()} time
122
101
* since the creation time of the current session.
@@ -128,7 +107,6 @@ public boolean isSessionRunningTooLong() {
128
107
129
108
/** Creates and returns the proto object for PerfSession object. */
130
109
public com .google .firebase .perf .v1 .PerfSession build () {
131
- // TODO(b/394127311): Switch to using AQS.
132
110
com .google .firebase .perf .v1 .PerfSession .Builder sessionMetric =
133
111
com .google .firebase .perf .v1 .PerfSession .newBuilder ().setSessionId (sessionId );
134
112
@@ -179,11 +157,10 @@ public static com.google.firebase.perf.v1.PerfSession[] buildAndSort(
179
157
}
180
158
181
159
/** If true, Session Gauge collection is enabled. */
182
- public static boolean shouldCollectGaugesAndEvents () {
160
+ public static boolean shouldCollectGaugesAndEvents (String sessionId ) {
183
161
ConfigResolver configResolver = ConfigResolver .getInstance ();
184
-
185
162
return configResolver .isPerformanceMonitoringEnabled ()
186
- && Math .random () < configResolver .getSessionsSamplingRate ();
163
+ && ( Math .abs ( sessionId . hashCode () % 100 ) < configResolver .getSessionsSamplingRate () * 100 );
187
164
}
188
165
189
166
/**
@@ -207,6 +184,7 @@ public int describeContents() {
207
184
public void writeToParcel (@ NonNull Parcel out , int flags ) {
208
185
out .writeString (sessionId );
209
186
out .writeByte ((byte ) (isGaugeAndEventCollectionEnabled ? 1 : 0 ));
187
+ out .writeByte ((byte ) (isAqsReady ? 1 : 0 ));
210
188
out .writeParcelable (creationTime , 0 );
211
189
}
212
190
@@ -224,4 +202,16 @@ public PerfSession[] newArray(int size) {
224
202
return new PerfSession [size ];
225
203
}
226
204
};
205
+
206
+ /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */
207
+ @ VisibleForTesting
208
+ static boolean isVerbose (@ NonNull com .google .firebase .perf .v1 .PerfSession perfSession ) {
209
+ for (SessionVerbosity sessionVerbosity : perfSession .getSessionVerbosityList ()) {
210
+ if (sessionVerbosity == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
211
+ return true ;
212
+ }
213
+ }
214
+
215
+ return false ;
216
+ }
227
217
}
0 commit comments