[FR] Allow custom attributes on built-in traces via global attribute API#8031
[FR] Allow custom attributes on built-in traces via global attribute API#8031jrodiz wants to merge 4 commits into
Conversation
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
Using Gemini Code AssistThe 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
Customization To customize 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 Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. 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. |
… full attribute scope Global attributes apply to all traces (built-in and custom) via TraceMetricBuilder, not only the built-in traces listed in the original changelog entry. Updated the entry to accurately describe the full scope and mention that the attribute methods and constants were made public.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request makes global attribute methods and constants public in FirebasePerformance and ensures these attributes are merged into all traces (built-in and custom) and network requests. Feedback suggests enforcing the MAX_TRACE_CUSTOM_ATTRIBUTES limit during the merge process to prevent events from being dropped by the backend and optimizing map allocations by utilizing the defensive copy returned by getAttributes().
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.
| @@ -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] | |||
There was a problem hiding this comment.
"Global attributes set via these methods are attached to all traces — automatic (for eg. _app_start, screen traces and network traces) and manual."
|
Closing fork PR in favor of: #8162 |
[FR] Allow custom attributes on built-in traces (#6664)
Summary
Resolves #6664
FirebasePerformancealready hadputAttribute/removeAttribute/getAttribute/getAttributesbacked by aConcurrentHashMap, andTransportManageralready attached those global attributes to every logged event — but the four methods were@hide, so developers could not actually set them.This PR removes the
@hideannotations, makes the related constants public, and adds the methods to the public API surface. No behavioral change is needed in the trace/network serialization path —TransportManager.setApplicationInfoAndBuild(firebase-perf/src/main/java/com/google/firebase/perf/transport/TransportManager.java:507-512) already callsputAllCustomAttributes(getGlobalCustomAttributes())onApplicationInfofor every trace metric and network request metric.Where the attributes land
Global attributes attach at the
ApplicationInfo.custom_attributeslevel, not atTraceMetric.custom_attributes/NetworkRequestMetric.custom_attributes. That distinction matters for any consumer reading the proto directly — those are separate proto fields. Trace- and request-level attributes set viaTrace.putAttribute/HttpMetric.putAttributecontinue to land in their respective per-eventcustom_attributesmap and are independent of the global ones.Usage
These attributes will appear on
_app_start,_app_in_foreground,_app_in_background, screen traces (_st_*), network requests, and all custom traces automatically — via the existingTransportManagerlogic.Notes
MAX_TRACE_CUSTOM_ATTRIBUTES(5) byFirebasePerformance.putAttribute's existingcheckAttribute()call.mCustomAttributesis aConcurrentHashMap;getAttributes()returns a defensive copy._app_start: global attributes must be set before the trace is built (e.g., inApplication.onCreate()).Verification
All tests pass locally.