Skip to content

Commit 7472e2e

Browse files
Fireperf fragments: sampling (#3588)
* ssample fragment after trace already sampled * gjf * tests and qol for immutable bundle * test names and revert getFloat * fragment-sampling * fix test * separate bucketId checkArguments
1 parent 84bb05b commit 7472e2e

File tree

2 files changed

+249
-19
lines changed

2 files changed

+249
-19
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/transport/RateLimiter.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ final class RateLimiter {
4949
/** The app's bucket ID for sampling, a number in [0.0f, 1.0f). */
5050
private final float samplingBucketId;
5151

52+
private final float fragmentBucketId;
53+
5254
private RateLimiterImpl traceLimiter = null;
5355
private RateLimiterImpl networkLimiter = null;
5456

@@ -63,7 +65,13 @@ final class RateLimiter {
6365
* @param capacity token bucket capacity
6466
*/
6567
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());
6775
this.isLogcatEnabled = Utils.isDebugLoggingEnabled(appContext);
6876
}
6977

@@ -78,11 +86,16 @@ static float getSamplingBucketId() {
7886
final long capacity,
7987
final Clock clock,
8088
float samplingBucketId,
89+
float fragmentBucketId,
8190
ConfigResolver configResolver) {
8291
Utils.checkArgument(
8392
0.0f <= samplingBucketId && samplingBucketId < 1.0f,
8493
"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).");
8597
this.samplingBucketId = samplingBucketId;
98+
this.fragmentBucketId = fragmentBucketId;
8699
this.configResolver = configResolver;
87100

88101
traceLimiter =
@@ -104,6 +117,22 @@ private boolean isDeviceAllowedToSendNetworkEvents() {
104117
return samplingBucketId < validNetworkSamplingBucketIdThreshold;
105118
}
106119

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+
107136
/**
108137
* Check if the {@link PerfMetric} should be rate limited.
109138
*
@@ -140,6 +169,12 @@ boolean isEventSampled(PerfMetric metric) {
140169
return false;
141170
}
142171

172+
if (isFragmentScreenTrace(metric)
173+
&& !(isDeviceAllowedToSendFragmentScreenTraces()
174+
|| hasVerboseSessions(metric.getTraceMetric().getPerfSessionsList()))) {
175+
return false;
176+
}
177+
143178
if (metric.hasNetworkRequestMetric()
144179
&& !(isDeviceAllowedToSendNetworkEvents()
145180
|| hasVerboseSessions(metric.getNetworkRequestMetric().getPerfSessionsList()))) {
@@ -207,6 +242,11 @@ boolean getIsDeviceAllowedToSendNetworkEvents() {
207242
return isDeviceAllowedToSendNetworkEvents();
208243
}
209244

245+
@VisibleForTesting
246+
boolean getIsDeviceAllowedToSendFragmentScreenTraces() {
247+
return isDeviceAllowedToSendFragmentScreenTraces();
248+
}
249+
210250
/** The implementation of Token Bucket rate limiter. */
211251
static class RateLimiterImpl {
212252

0 commit comments

Comments
 (0)