@@ -49,6 +49,8 @@ final class RateLimiter {
49
49
/** The app's bucket ID for sampling, a number in [0.0f, 1.0f). */
50
50
private final float samplingBucketId ;
51
51
52
+ private final float fragmentBucketId ;
53
+
52
54
private RateLimiterImpl traceLimiter = null ;
53
55
private RateLimiterImpl networkLimiter = null ;
54
56
@@ -63,7 +65,13 @@ final class RateLimiter {
63
65
* @param capacity token bucket capacity
64
66
*/
65
67
public RateLimiter (@ NonNull Context appContext , final Rate rate , final long capacity ) {
66
- this (rate , capacity , new Clock (), getSamplingBucketId (), ConfigResolver .getInstance ());
68
+ this (
69
+ rate ,
70
+ capacity ,
71
+ new Clock (),
72
+ getSamplingBucketId (),
73
+ getSamplingBucketId (),
74
+ ConfigResolver .getInstance ());
67
75
this .isLogcatEnabled = Utils .isDebugLoggingEnabled (appContext );
68
76
}
69
77
@@ -78,11 +86,16 @@ static float getSamplingBucketId() {
78
86
final long capacity ,
79
87
final Clock clock ,
80
88
float samplingBucketId ,
89
+ float fragmentBucketId ,
81
90
ConfigResolver configResolver ) {
82
91
Utils .checkArgument (
83
92
0.0f <= samplingBucketId && samplingBucketId < 1.0f ,
84
93
"Sampling bucket ID should be in range [0.0f, 1.0f)." );
94
+ Utils .checkArgument (
95
+ 0.0f <= fragmentBucketId && fragmentBucketId < 1.0f ,
96
+ "Fragment sampling bucket ID should be in range [0.0f, 1.0f)." );
85
97
this .samplingBucketId = samplingBucketId ;
98
+ this .fragmentBucketId = fragmentBucketId ;
86
99
this .configResolver = configResolver ;
87
100
88
101
traceLimiter =
@@ -104,6 +117,22 @@ private boolean isDeviceAllowedToSendNetworkEvents() {
104
117
return samplingBucketId < validNetworkSamplingBucketIdThreshold ;
105
118
}
106
119
120
+ /**
121
+ * Returns whether device is allowed to send Fragment screen trace events based on Fragment screen
122
+ * trace sampling rate.
123
+ */
124
+ private boolean isDeviceAllowedToSendFragmentScreenTraces () {
125
+ float validFragmentSamplingBucketIdThreshold = configResolver .getFragmentSamplingRate ();
126
+ return fragmentBucketId < validFragmentSamplingBucketIdThreshold ;
127
+ }
128
+
129
+ /** Identifies if the {@link PerfMetric} is a Fragment screen trace */
130
+ protected boolean isFragmentScreenTrace (PerfMetric metric ) {
131
+ return metric .hasTraceMetric ()
132
+ && metric .getTraceMetric ().getName ().startsWith (Constants .SCREEN_TRACE_PREFIX )
133
+ && metric .getTraceMetric ().containsCustomAttributes (Constants .ACTIVITY_ATTRIBUTE_KEY );
134
+ }
135
+
107
136
/**
108
137
* Check if the {@link PerfMetric} should be rate limited.
109
138
*
@@ -140,6 +169,12 @@ boolean isEventSampled(PerfMetric metric) {
140
169
return false ;
141
170
}
142
171
172
+ if (isFragmentScreenTrace (metric )
173
+ && !(isDeviceAllowedToSendFragmentScreenTraces ()
174
+ || hasVerboseSessions (metric .getTraceMetric ().getPerfSessionsList ()))) {
175
+ return false ;
176
+ }
177
+
143
178
if (metric .hasNetworkRequestMetric ()
144
179
&& !(isDeviceAllowedToSendNetworkEvents ()
145
180
|| hasVerboseSessions (metric .getNetworkRequestMetric ().getPerfSessionsList ()))) {
@@ -207,6 +242,11 @@ boolean getIsDeviceAllowedToSendNetworkEvents() {
207
242
return isDeviceAllowedToSendNetworkEvents ();
208
243
}
209
244
245
+ @ VisibleForTesting
246
+ boolean getIsDeviceAllowedToSendFragmentScreenTraces () {
247
+ return isDeviceAllowedToSendFragmentScreenTraces ();
248
+ }
249
+
210
250
/** The implementation of Token Bucket rate limiter. */
211
251
static class RateLimiterImpl {
212
252
0 commit comments