-
Notifications
You must be signed in to change notification settings - Fork 687
[FR] Allow custom attributes on built-in traces via global attribute API #8031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import androidx.annotation.Nullable; | ||||||||||||||||||||||||||||||||||||
| import androidx.annotation.VisibleForTesting; | ||||||||||||||||||||||||||||||||||||
| import com.google.firebase.perf.FirebasePerformance; | ||||||||||||||||||||||||||||||||||||
| import com.google.firebase.perf.application.AppStateMonitor; | ||||||||||||||||||||||||||||||||||||
| import com.google.firebase.perf.application.AppStateUpdateHandler; | ||||||||||||||||||||||||||||||||||||
| import com.google.firebase.perf.logging.AndroidLogger; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -37,6 +38,7 @@ | |||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||
| import java.util.Arrays; | ||||||||||||||||||||||||||||||||||||
| import java.util.Collections; | ||||||||||||||||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -213,7 +215,14 @@ public NetworkRequestMetricBuilder setRequestPayloadBytes(long bytes) { | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** Sets the customAttributes for the current {@link NetworkRequestMetric}. */ | ||||||||||||||||||||||||||||||||||||
| public NetworkRequestMetricBuilder setCustomAttributes(Map<String, String> attributes) { | ||||||||||||||||||||||||||||||||||||
| builder.clearCustomAttributes().putAllCustomAttributes(attributes); | ||||||||||||||||||||||||||||||||||||
| Map<String, String> merged = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| merged.putAll(FirebasePerformance.getInstance().getAttributes()); | ||||||||||||||||||||||||||||||||||||
| } catch (IllegalStateException e) { | ||||||||||||||||||||||||||||||||||||
| // FirebaseApp not initialized yet, skip global attributes | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| merged.putAll(attributes); | ||||||||||||||||||||||||||||||||||||
| builder.clearCustomAttributes().putAllCustomAttributes(merged); | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+218
to
+225
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation creates a new
Suggested change
|
||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,9 +15,11 @@ | |||||||||||||||||||||||||||||||||||
| package com.google.firebase.perf.metrics; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import androidx.annotation.NonNull; | ||||||||||||||||||||||||||||||||||||
| import com.google.firebase.perf.FirebasePerformance; | ||||||||||||||||||||||||||||||||||||
| import com.google.firebase.perf.session.PerfSession; | ||||||||||||||||||||||||||||||||||||
| import com.google.firebase.perf.v1.TraceMetric; | ||||||||||||||||||||||||||||||||||||
| import java.util.Arrays; | ||||||||||||||||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -53,7 +55,14 @@ TraceMetric build() { | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| traceMetric.putAllCustomAttributes(trace.getAttributes()); | ||||||||||||||||||||||||||||||||||||
| Map<String, String> mergedAttributes = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| mergedAttributes.putAll(FirebasePerformance.getInstance().getAttributes()); | ||||||||||||||||||||||||||||||||||||
| } catch (IllegalStateException e) { | ||||||||||||||||||||||||||||||||||||
| // FirebaseApp not initialized yet, skip global attributes | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| mergedAttributes.putAll(trace.getAttributes()); | ||||||||||||||||||||||||||||||||||||
| traceMetric.putAllCustomAttributes(mergedAttributes); | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The merging logic can result in a trace having more than
Comment on lines
+58
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation creates a new
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| com.google.firebase.perf.v1.PerfSession[] perfSessions = | ||||||||||||||||||||||||||||||||||||
| PerfSession.buildAndSort(trace.getSessions()); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The merging logic can result in a network request having more than
MAX_TRACE_CUSTOM_ATTRIBUTES(5) attributes if both global and local attributes are set. Since the backend typically enforces a limit of 5 custom attributes per event, this could lead to events being dropped or truncated. Consider enforcing the limit during the merge, giving priority to local attributes.