feat(metrics): Add metric user attributes from scope#1063
Open
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/metrics-defaults-scopedfrom
Open
feat(metrics): Add metric user attributes from scope#1063szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/metrics-defaults-scopedfrom
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/metrics-defaults-scopedfrom
Conversation
|
Add the metric user attribute portion of the earlier enrichment work. Pass send_default_pii into metric scope application and attach user.id, user.name, and user.email from the scope when that option is enabled. Preserve explicitly set user.* metric attributes instead of overwriting them. Co-authored-by: Joris Bayer <joris.bayer@sentry.io> Closes #1060 Closes [RUST-188](https://linear.app/getsentry/issue/RUST-188/add-trace-metric-user-attribute-enrichment-gated-by-send-default-pii)
efcf784 to
8a5dd6f
Compare
3ab9f68 to
948a361
Compare
lcian
reviewed
Apr 17, 2026
Comment on lines
+473
to
+516
| #[cfg(feature = "metrics")] | ||
| trait MetricExt { | ||
| fn insert_attribute<K, V>(&mut self, key: K, value: V) | ||
| where | ||
| K: Into<Cow<'static, str>>, | ||
| V: Into<Value>; | ||
|
|
||
| fn attribute(&self, key: &str) -> Option<&LogAttribute>; | ||
|
|
||
| /// Applies user attributes from provided [`User`]. | ||
| fn apply_user_attributes(&mut self, user: &User) { | ||
| [ | ||
| ("user.id", user.id.as_deref()), | ||
| ("user.name", user.username.as_deref()), | ||
| ("user.email", user.email.as_deref()), | ||
| ] | ||
| .into_iter() | ||
| .flat_map(|(attribute, value)| value.map(|v| (attribute, v))) | ||
| .for_each(|(attribute, value)| self.insert_attribute(attribute, value)); | ||
| } | ||
|
|
||
| /// Checks if any user attributes are on this metric | ||
| fn has_any_user_attributes(&self) -> bool { | ||
| ["user.id", "user.name", "user.email"] | ||
| .into_iter() | ||
| .any(|key| self.attribute(key).is_some()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "metrics")] | ||
| impl MetricExt for Metric { | ||
| fn insert_attribute<K, V>(&mut self, key: K, value: V) | ||
| where | ||
| K: Into<Cow<'static, str>>, | ||
| V: Into<Value>, | ||
| { | ||
| self.attributes | ||
| .insert(key.into(), LogAttribute(value.into())); | ||
| } | ||
|
|
||
| fn attribute(&self, key: &str) -> Option<&LogAttribute> { | ||
| self.attributes.get(key) | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
You could just inline this code instead of putting it in a separate extension trait. But I guess it's a matter of preference, so feel free to keep it if you like it more this way.
Member
Author
There was a problem hiding this comment.
I find it quite a bit more ergonomic tbh, especially because this extension trait also handles type conversions for me.
Why do you think it would be better to inline? Just personal preference, or is there some technical tradeoff that I am failing to consider?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add the metric user attribute portion of the earlier enrichment work.
Pass send_default_pii into metric scope application and attach user.id, user.name, and user.email from the scope when that option is enabled. Preserve explicitly set
user.*metric attributes instead of overwriting them.Co-authored-by: Joris Bayer joris.bayer@sentry.io
Closes #1060
Closes RUST-188