diff --git a/firebase-perf/CHANGELOG.md b/firebase-perf/CHANGELOG.md index 966a91944e3..f7877ed2f1b 100644 --- a/firebase-perf/CHANGELOG.md +++ b/firebase-perf/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased +- [feature] Made the global attribute methods (`putAttribute`, `removeAttribute`, `getAttribute`, `getAttributes`) and related constants (`MAX_TRACE_CUSTOM_ATTRIBUTES`, `MAX_ATTRIBUTE_KEY_LENGTH`, `MAX_ATTRIBUTE_VALUE_LENGTH`) public on `FirebasePerformance`. Global attributes set via these methods are attached to all traces — built-in (`_app_start`, `_app_in_foreground`, `_app_in_background`, screen traces) and custom — as well as all network requests. [#6664] - [changed] Bumped internal dependencies. # 22.0.4 diff --git a/firebase-perf/api.txt b/firebase-perf/api.txt index 13dfb7dcbea..d22c80e12c6 100644 --- a/firebase-perf/api.txt +++ b/firebase-perf/api.txt @@ -2,11 +2,15 @@ package com.google.firebase.perf { @javax.inject.Singleton public class FirebasePerformance { + method public String? getAttribute(String); + method public java.util.Map getAttributes(); method public static com.google.firebase.perf.FirebasePerformance getInstance(); method public boolean isPerformanceCollectionEnabled(); method public com.google.firebase.perf.metrics.HttpMetric newHttpMetric(String, @com.google.firebase.perf.FirebasePerformance.HttpMethod String); method public com.google.firebase.perf.metrics.HttpMetric newHttpMetric(java.net.URL, @com.google.firebase.perf.FirebasePerformance.HttpMethod String); method public com.google.firebase.perf.metrics.Trace newTrace(String); + method public void putAttribute(String, String); + method public void removeAttribute(String); method public void setPerformanceCollectionEnabled(boolean); method public static com.google.firebase.perf.metrics.Trace startTrace(String); field public static final int MAX_ATTRIBUTE_KEY_LENGTH = 40; // 0x28 diff --git a/firebase-perf/src/main/java/com/google/firebase/perf/FirebasePerformance.java b/firebase-perf/src/main/java/com/google/firebase/perf/FirebasePerformance.java index 40468566225..4dcbdb922ac 100644 --- a/firebase-perf/src/main/java/com/google/firebase/perf/FirebasePerformance.java +++ b/firebase-perf/src/main/java/com/google/firebase/perf/FirebasePerformance.java @@ -71,15 +71,15 @@ public class FirebasePerformance implements FirebasePerformanceAttributable { /** Maximum allowed number of attributes allowed in a trace. */ @SuppressWarnings("unused") // Used in Javadoc. - private static final int MAX_TRACE_CUSTOM_ATTRIBUTES = Constants.MAX_TRACE_CUSTOM_ATTRIBUTES; + public static final int MAX_TRACE_CUSTOM_ATTRIBUTES = Constants.MAX_TRACE_CUSTOM_ATTRIBUTES; /** Maximum allowed length of the Key of the {@link Trace} attribute */ @SuppressWarnings("unused") // Used in Javadoc. - private static final int MAX_ATTRIBUTE_KEY_LENGTH = Constants.MAX_ATTRIBUTE_KEY_LENGTH; + public static final int MAX_ATTRIBUTE_KEY_LENGTH = Constants.MAX_ATTRIBUTE_KEY_LENGTH; /** Maximum allowed length of the Value of the {@link Trace} attribute */ @SuppressWarnings("unused") // Used in Javadoc. - private static final int MAX_ATTRIBUTE_VALUE_LENGTH = Constants.MAX_ATTRIBUTE_VALUE_LENGTH; + public static final int MAX_ATTRIBUTE_VALUE_LENGTH = Constants.MAX_ATTRIBUTE_VALUE_LENGTH; /** Maximum allowed length of the name of the {@link Trace} */ @SuppressWarnings("unused") // Used in Javadoc. @@ -332,7 +332,6 @@ public boolean isPerformanceCollectionEnabled() { * length is limited to {@link #MAX_ATTRIBUTE_KEY_LENGTH} * @param value value of the attribute. The max length is limited to {@link * #MAX_ATTRIBUTE_VALUE_LENGTH} - * @hide */ @Override public void putAttribute(@NonNull String attribute, @NonNull String value) { @@ -371,7 +370,6 @@ private void checkAttribute(@Nullable String key, @Nullable String value) { * Removes the attribute from the global list of attributes. * * @param attribute name of the attribute to be removed from the global pool. - * @hide */ @Override public void removeAttribute(@NonNull String attribute) { @@ -383,7 +381,6 @@ public void removeAttribute(@NonNull String attribute) { * * @param attribute name of the attribute to fetch the value for * @return the value of the attribute if it exists or null otherwise. - * @hide */ @Override @Nullable @@ -395,7 +392,6 @@ public String getAttribute(@NonNull String attribute) { * Returns the map of all the attributes currently added in the global pool. * * @return map of attributes and its values currently added to the running Traces - * @hide */ @Override @NonNull diff --git a/firebase-perf/src/test/java/com/google/firebase/perf/transport/TransportManagerTest.java b/firebase-perf/src/test/java/com/google/firebase/perf/transport/TransportManagerTest.java index 5376265aa0e..318f14bdcfc 100644 --- a/firebase-perf/src/test/java/com/google/firebase/perf/transport/TransportManagerTest.java +++ b/firebase-perf/src/test/java/com/google/firebase/perf/transport/TransportManagerTest.java @@ -1102,6 +1102,48 @@ public void syncLogForGaugeMetric_performanceDisabled_noInteractionWithFirebaseI // region Global Custom Attributes Behaviour + @Test + public void logBuiltInTraces_globalCustomAttributesAreAdded() { + FirebasePerformance.getInstance().removeAttribute("experiment_id"); + FirebasePerformance.getInstance().removeAttribute("user_tier"); + FirebasePerformance.getInstance().putAttribute("experiment_id", "exp_123"); + FirebasePerformance.getInstance().putAttribute("user_tier", "gold"); + + // 1. App Start Trace (_as) + TraceMetric appStartTrace = createValidTraceMetric().toBuilder().setName("_as").build(); + testTransportManager.log(appStartTrace); + fakeExecutorService.runAll(); + PerfMetric loggedAppStart = getLastLoggedEvent(times(1)); + assertThat(loggedAppStart.getApplicationInfo().getCustomAttributesMap()) + .containsEntry("experiment_id", "exp_123"); + clearLastLoggedEvents(); + + // 2. Screen Trace (_st_MainActivity) + TraceMetric screenTrace = + createValidTraceMetric().toBuilder() + .setName("_st_MainActivity") + .putCounters(Constants.CounterNames.FRAMES_TOTAL.toString(), 100L) + .build(); + testTransportManager.log(screenTrace); + fakeExecutorService.runAll(); + PerfMetric loggedScreenTrace = getLastLoggedEvent(times(1)); + assertThat(loggedScreenTrace.getApplicationInfo().getCustomAttributesMap()) + .containsEntry("user_tier", "gold"); + clearLastLoggedEvents(); + + // 3. Network Request + NetworkRequestMetric networkMetric = createValidNetworkRequestMetric(); + testTransportManager.log(networkMetric); + fakeExecutorService.runAll(); + PerfMetric loggedNetworkMetric = getLastLoggedEvent(times(1)); + assertThat(loggedNetworkMetric.getApplicationInfo().getCustomAttributesMap()) + .containsEntry("experiment_id", "exp_123"); + + // Cleanup + FirebasePerformance.getInstance().removeAttribute("experiment_id"); + FirebasePerformance.getInstance().removeAttribute("user_tier"); + } + @Test public void logTraceMetric_globalCustomAttributesAreAdded() { FirebasePerformance.getInstance().putAttribute("test_key1", "test_value1");