Skip to content

feat(metrics): Add metric user attributes from scope#1063

Open
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/metrics-defaults-scopedfrom
szokeasaurusrex/user-attributes-metrics
Open

feat(metrics): Add metric user attributes from scope#1063
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/metrics-defaults-scopedfrom
szokeasaurusrex/user-attributes-metrics

Conversation

@szokeasaurusrex
Copy link
Copy Markdown
Member

@szokeasaurusrex szokeasaurusrex commented Apr 16, 2026

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

@linear-code
Copy link
Copy Markdown

linear-code bot commented Apr 16, 2026

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 16, 2026

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 948a361

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)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/metrics-defaults-scoped branch from efcf784 to 8a5dd6f Compare April 16, 2026 19:48
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from 3ab9f68 to 948a361 Compare April 16, 2026 19:48
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review April 16, 2026 19:57
@szokeasaurusrex szokeasaurusrex requested a review from lcian as a code owner April 16, 2026 19:57
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)
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just personal preference 👍

Copy link
Copy Markdown
Member

@lcian lcian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants