Skip to content

Commit 325e54c

Browse files
committed
Add different defaults for when RC fetch failed
1 parent 174e7d8 commit 325e54c

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-7
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/config/ConfigResolver.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ public double getTraceSamplingRate() {
290290
// Order of precedence is:
291291
// 1. If the value exists through Firebase Remote Config, cache and return this value.
292292
// 2. If the value exists in device cache, return this value.
293-
// 3. Otherwise, return default value.
293+
// 3. If the Firebase Remote Config fetch failed, return the default "rc failure" value.
294+
// 4. Otherwise, return default value.
294295
TraceSamplingRate config = TraceSamplingRate.getInstance();
295296

296297
// 1. Reads value from Firebase Remote Config, saves this value in cache layer if valid.
@@ -306,7 +307,12 @@ public double getTraceSamplingRate() {
306307
return deviceCacheValue.get();
307308
}
308309

309-
// 3. Returns default value if there is no valid value from above approaches.
310+
// 3. Check if RC fetch failed.
311+
if (remoteConfigManager.isLastFetchFailed()) {
312+
return config.getDefaultOnRcFetchFail();
313+
}
314+
315+
// 4. Returns default value if there is no valid value from above approaches.
310316
return config.getDefault();
311317
}
312318

@@ -315,7 +321,8 @@ public double getNetworkRequestSamplingRate() {
315321
// Order of precedence is:
316322
// 1. If the value exists through Firebase Remote Config, cache and return this value.
317323
// 2. If the value exists in device cache, return this value.
318-
// 3. Otherwise, return default value.
324+
// 3. If the Firebase Remote Config fetch failed, return the default "rc failure" value.
325+
// 4. Otherwise, return default value.
319326
NetworkRequestSamplingRate config = NetworkRequestSamplingRate.getInstance();
320327

321328
// 1. Reads value from Firebase Remote Config, saves this value in cache layer if valid.
@@ -331,7 +338,12 @@ public double getNetworkRequestSamplingRate() {
331338
return deviceCacheValue.get();
332339
}
333340

334-
// 3. Returns default value if there is no valid value from above approaches.
341+
// 3. Check if RC fetch failed.
342+
if (remoteConfigManager.isLastFetchFailed()) {
343+
return config.getDefaultOnRcFetchFail();
344+
}
345+
346+
// 4. Returns default value if there is no valid value from above approaches.
335347
return config.getDefault();
336348
}
337349

@@ -342,7 +354,8 @@ public double getSessionsSamplingRate() {
342354
// and return this value.
343355
// 2. If the value exists through Firebase Remote Config, cache and return this value.
344356
// 3. If the value exists in device cache, return this value.
345-
// 4. Otherwise, return default value.
357+
// 4. If the Firebase Remote Config fetch failed, return the default "rc failure" value.
358+
// 5. Otherwise, return default value.
346359
SessionsSamplingRate config = SessionsSamplingRate.getInstance();
347360

348361
// 1. Reads value in Android Manifest (it is set by developers during build time).
@@ -368,7 +381,12 @@ public double getSessionsSamplingRate() {
368381
return deviceCacheValue.get();
369382
}
370383

371-
// 4. Returns default value if there is no valid value from above approaches.
384+
// 4. Check if RC fetch failed.
385+
if (remoteConfigManager.isLastFetchFailed()) {
386+
return config.getDefaultOnRcFetchFail();
387+
}
388+
389+
// 5. Returns default value if there is no valid value from above approaches.
372390
return config.getDefault();
373391
}
374392

firebase-perf/src/main/java/com/google/firebase/perf/config/ConfigurationConstants.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ protected Double getDefault() {
152152
return 1.0;
153153
}
154154

155+
@Override
156+
protected Double getDefaultOnRcFetchFail() {
157+
// Reduce the typical default by 2 orders of magnitude.
158+
return getDefault() / 100;
159+
}
160+
155161
@Override
156162
protected String getRemoteConfigFlag() {
157163
return "fpr_vc_trace_sampling_rate";
@@ -185,6 +191,12 @@ protected Double getDefault() {
185191
return 1.0;
186192
}
187193

194+
@Override
195+
protected Double getDefaultOnRcFetchFail() {
196+
// Reduce the typical default by 2 orders of magnitude.
197+
return getDefault() / 100;
198+
}
199+
188200
@Override
189201
protected String getRemoteConfigFlag() {
190202
return "fpr_vc_network_request_sampling_rate";
@@ -553,6 +565,12 @@ protected Double getDefault() {
553565
return 0.01;
554566
}
555567

568+
@Override
569+
protected Double getDefaultOnRcFetchFail() {
570+
// Reduce the typical default by 2 orders of magnitude.
571+
return getDefault() / 100;
572+
}
573+
556574
@Override
557575
protected String getDeviceCacheFlag() {
558576
return "com.google.firebase.perf.SessionSamplingRate";

firebase-perf/src/main/java/com/google/firebase/perf/config/ConfigurationFlag.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ abstract class ConfigurationFlag<T> {
2727
/* Default value for this flag. */
2828
protected abstract T getDefault();
2929

30+
/* Default value for this flag in the case that RC fetch explicitly failed. */
31+
protected T getDefaultOnRcFetchFail() {
32+
// Same as typical default unless overridden.
33+
return getDefault();
34+
}
35+
3036
/* Configuration flag ID for Firebase Remote Config. */
3137
@Nullable
3238
String getRemoteConfigFlag() {

firebase-perf/src/main/java/com/google/firebase/perf/config/RemoteConfigManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ private void triggerFirebaseRemoteConfigFetchAndActivateOnSuccessfulFetch() {
344344
.addOnSuccessListener(executor, result -> syncConfigValues(firebaseRemoteConfig.getAll()))
345345
.addOnFailureListener(
346346
executor,
347-
task -> {
347+
ex -> {
348+
logger.debug("Remote config fetch failed: %s", ex);
348349
firebaseRemoteConfigLastFetchTimestampMs = FETCH_NEVER_HAPPENED_TIMESTAMP_MS;
349350
});
350351
}

firebase-perf/src/test/java/com/google/firebase/perf/config/ConfigResolverTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,17 @@ public void getTraceSamplingRate_noRemoteConfig_returnsDefault() {
16531653
assertThat(testConfigResolver.getTraceSamplingRate()).isEqualTo(1.00);
16541654
}
16551655

1656+
@Test
1657+
public void getTraceSamplingRate_remoteConfigFetchFailed_returnsRCFailureDefault() {
1658+
when(mockRemoteConfigManager.getDouble(TRACE_SAMPLING_RATE_FRC_KEY))
1659+
.thenReturn(Optional.absent());
1660+
when(mockDeviceCacheManager.getDouble(TRACE_SAMPLING_RATE_CACHE_KEY))
1661+
.thenReturn(Optional.absent());
1662+
when(mockRemoteConfigManager.isLastFetchFailed()).thenReturn(true);
1663+
1664+
assertThat(testConfigResolver.getTraceSamplingRate()).isEqualTo(1.00 / 100);
1665+
}
1666+
16561667
@Test
16571668
public void getTraceSamplingRate_noRemoteConfigHasCache_returnsCache() {
16581669
when(mockRemoteConfigManager.getDouble(TRACE_SAMPLING_RATE_FRC_KEY))
@@ -1714,6 +1725,17 @@ public void getNetworkRequestSamplingRate_noRemoteConfig_returnsDefault() {
17141725
assertThat(testConfigResolver.getNetworkRequestSamplingRate()).isEqualTo(1.00);
17151726
}
17161727

1728+
@Test
1729+
public void getNetworkRequestSamplingRate_remoteConfigFetchFailed_returnsRCFailureDefault() {
1730+
when(mockRemoteConfigManager.getDouble(NETWORK_REQUEST_SAMPLING_RATE_FRC_KEY))
1731+
.thenReturn(Optional.absent());
1732+
when(mockDeviceCacheManager.getDouble(NETWORK_REQUEST_SAMPLING_RATE_CACHE_KEY))
1733+
.thenReturn(Optional.absent());
1734+
when(mockRemoteConfigManager.isLastFetchFailed()).thenReturn(true);
1735+
1736+
assertThat(testConfigResolver.getNetworkRequestSamplingRate()).isEqualTo(1.00 / 100);
1737+
}
1738+
17171739
@Test
17181740
public void getNetworkRequestSamplingRate_noRemoteConfigHasCache_returnsCache() {
17191741
when(mockRemoteConfigManager.getDouble(NETWORK_REQUEST_SAMPLING_RATE_FRC_KEY))
@@ -1935,6 +1957,18 @@ public void getSessionsSamplingRate_invalidRemoteConfig_returnCacheValue() {
19351957
verify(mockDeviceCacheManager, never()).setValue(any(), any());
19361958
}
19371959

1960+
1961+
@Test
1962+
public void getSessionsSamplingRate_remoteConfigFetchFailed_returnsRCFailureDefault() {
1963+
when(mockDeviceCacheManager.getDouble(SESSION_SAMPLING_RATE_CACHE_KEY))
1964+
.thenReturn(Optional.absent());
1965+
when(mockRemoteConfigManager.getDouble(SESSION_SAMPLING_RATE_FRC_KEY))
1966+
.thenReturn(Optional.absent());
1967+
when(mockRemoteConfigManager.isLastFetchFailed()).thenReturn(true);
1968+
1969+
assertThat(testConfigResolver.getSessionsSamplingRate()).isEqualTo(0.01 / 100);
1970+
}
1971+
19381972
@Test
19391973
public void getSessionsSamplingRate_validCache_returnCacheValue() {
19401974
when(mockDeviceCacheManager.getDouble(SESSION_SAMPLING_RATE_CACHE_KEY))

0 commit comments

Comments
 (0)