Skip to content

[FR] Allow custom attributes on built-in traces (#6664)#8228

Open
jrodiz wants to merge 6 commits into
firebase:mainfrom
jrodiz:feature/jrc--6664.3.Support.custom.attributes.in.fireperf
Open

[FR] Allow custom attributes on built-in traces (#6664)#8228
jrodiz wants to merge 6 commits into
firebase:mainfrom
jrodiz:feature/jrc--6664.3.Support.custom.attributes.in.fireperf

Conversation

@jrodiz
Copy link
Copy Markdown
Contributor

@jrodiz jrodiz commented May 28, 2026

[FR] Allow custom attributes on built-in traces (#6664)

#6664.

FirebasePerformance already exposes putAttribute / removeAttribute / getAttribute / getAttributes as public Java methods (currently @hide in the Javadocs), backed by a ConcurrentHashMap. TransportManager.setApplicationInfoAndBuild (firebase-perf/src/main/java/com/google/firebase/perf/transport/TransportManager.java:507-512) calls putAllCustomAttributes(getGlobalCustomAttributes()) on ApplicationInfo for every TraceMetric and NetworkRequestMetric, so any global attributes set through that API already ride along on built-in traces and network requests.

This PR adds a unit test that pins down that behavior so the hidden API can be recommended to customers (per discussion with ACore) while the formal public-API review is scheduled separately.

Changes

  • TransportManagerTest.java — New unit test logBuiltInTraces_globalCustomAttributesAreAdded covering three paths:
    • App-start trace (_as) → asserts a global attribute appears on ApplicationInfo.custom_attributes.
    • Screen trace (_st_MainActivity) → asserts a second global attribute appears on ApplicationInfo.custom_attributes.
    • Network request → asserts the global attribute appears on ApplicationInfo.custom_attributes.

Usage (today, via the hidden API)

// In Application.onCreate() — set once, applies to all built-in and custom traces
// and to all network requests
FirebasePerformance.getInstance().putAttribute("build_type", "debug");
FirebasePerformance.getInstance().putAttribute("user_tier", "premium");

These attributes appear on _app_start, _app_in_foreground, _app_in_background, screen traces (_st_*), network requests, and all custom traces — at the ApplicationInfo.custom_attributes level — via the existing TransportManager logic.

The methods above are still @hide and the surface is subject to change without notice until the formal API review lands.

Notes

  • Attribute limit: the global pool is validated at MAX_TRACE_CUSTOM_ATTRIBUTES (5) by FirebasePerformance.putAttribute's existing checkAttribute() call.
  • Thread safety: mCustomAttributes is a ConcurrentHashMap; getAttributes() returns a defensive copy.
  • Timing for _app_start: global attributes must be set before the trace is built (e.g., in Application.onCreate()).
  • Follow-up (out of scope for this PR): formal API review must document/verify behavior with internal _-prefixed keys and update https://firebase.google.com/docs/perf-mon/attributes?platform=android#collecting-user-data before the four methods can be exposed publicly.

Verification

./gradlew :firebase-perf:test

All tests pass locally.

jrodiz and others added 6 commits May 14, 2026 13:33
Resolves firebase#6664.

FirebasePerformance already had putAttribute/getAttribute/removeAttribute/
getAttributes backed by a ConcurrentHashMap, but they were @hide and not
wired into built-in trace serialization. This change makes them public and
ensures global attributes are included on all traces (built-in and custom).

Changes:
- Make global attribute methods (putAttribute, removeAttribute, getAttribute,
  getAttributes) public on FirebasePerformance
- Expose MAX_TRACE_CUSTOM_ATTRIBUTES, MAX_ATTRIBUTE_KEY_LENGTH,
  MAX_ATTRIBUTE_VALUE_LENGTH as public constants on FirebasePerformance
- Update api.txt with the expanded public API surface
- Merge global attributes in TraceMetricBuilder (screen + custom Trace objects)
- Add global attributes to AppStartTrace (_app_start)
- Add global attributes to AppStateMonitor.sendSessionLog() (_fs, _bs)
- Merge global attributes in NetworkRequestMetricBuilder (auto + manual HTTP)
- Trace/request-level attributes take precedence over global on key conflicts

Test coverage (TraceMetric.custom_attributes):
- AppStartTraceTest: global attrs on _app_start
- AppStateMonitorTest: global attrs on _app_in_foreground, _app_in_background,
  and screen traces (_st_*)
- TraceMetricBuilderTest: global attrs merged, trace-level overrides global
- NetworkRequestMetricBuilderTest: global attrs on network requests,
  per-request overrides global
- TransportManagerTest: global attrs on built-in traces at ApplicationInfo level
TransportManager.setApplicationInfoAndBuild already adds global custom
attributes to all traces and network requests at the ApplicationInfo level
(TransportManager.java:507-512). The merging logic added in this PR to
TraceMetricBuilder, NetworkRequestMetricBuilder, AppStartTrace, and
AppStateMonitor duplicated those attributes at the TraceMetric /
NetworkRequestMetric level, which is redundant.

Visibility changes on FirebasePerformance, api.txt, and CHANGELOG are
preserved — exposing the previously @hide global-attribute APIs is all
that this PR needs to do.
…eview

Restore @hide + private MAX_* on FirebasePerformance; drop api.txt and
CHANGELOG entries; revert to version=22.0.6. Keep new TransportManager test.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@jrodiz jrodiz requested a review from tejasd May 28, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants