Skip to content

feat(metrics): Add before_send_metric callback#1064

Open
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/user-attributes-metricsfrom
szokeasaurusrex/before-send-metric
Open

feat(metrics): Add before_send_metric callback#1064
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/user-attributes-metricsfrom
szokeasaurusrex/before-send-metric

Conversation

@szokeasaurusrex
Copy link
Copy Markdown
Member

@szokeasaurusrex szokeasaurusrex commented Apr 16, 2026

Run metrics through ClientOptions::before_send_metric after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Stacked on #1063.

Co-authored-by: Joris Bayer joris.bayer@sentry.io

Closes #1025
Closes RUST-170

@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 7eb91c1

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from a9398ed to cf1dd39 Compare April 17, 2026 08:54
@szokeasaurusrex szokeasaurusrex changed the title feat!(metrics): Add before_send_metric callback feat(metrics): Add before_send_metric callback Apr 17, 2026
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review April 17, 2026 08:55
@szokeasaurusrex szokeasaurusrex requested a review from lcian as a code owner April 17, 2026 08:55
szokeasaurusrex added a commit that referenced this pull request Apr 17, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from cf1dd39 to cdad82d Compare April 17, 2026 08:56
szokeasaurusrex added a commit that referenced this pull request Apr 17, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from cdad82d to 286f469 Compare April 17, 2026 08:56
szokeasaurusrex added a commit that referenced this pull request Apr 17, 2026

assert_eq!(
metric.attributes.get("added_by_callback"),
Some(&LogAttribute(Value::from("yes"))),
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.

Did we intend to call this just Attribute (could either be a new type or just a type alias)? I don't remember what was the outcome of that discussion (if any).

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.

Yeah, I think it makes sense to make that change. Likely we should do it as a type alias (rename LogAttribute to Attribute, and make LogAttribute an alias to Attribute). However, I believe we can also do this later, should be a backwards and forwards-compatible change as far as I am aware

szokeasaurusrex added a commit that referenced this pull request Apr 21, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from 286f469 to 816fa87 Compare April 21, 2026 12:40
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from 948a361 to aa6f863 Compare April 21, 2026 12:41
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from 816fa87 to 225d1dd Compare April 21, 2026 12:46
szokeasaurusrex added a commit that referenced this pull request Apr 21, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
szokeasaurusrex added a commit that referenced this pull request Apr 21, 2026
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from aa6f863 to eece55d Compare April 21, 2026 12:47
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from 225d1dd to bae243c Compare April 21, 2026 15:20
szokeasaurusrex added a commit that referenced this pull request Apr 21, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
szokeasaurusrex added a commit that referenced this pull request Apr 21, 2026
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from eece55d to 787d60a Compare April 21, 2026 15:20
szokeasaurusrex added a commit that referenced this pull request Apr 22, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
szokeasaurusrex added a commit that referenced this pull request Apr 22, 2026
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from bae243c to d429da8 Compare April 22, 2026 17:49
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from 787d60a to 807de4c Compare April 22, 2026 17:49
Comment thread sentry-core/src/client.rs
Comment on lines +585 to +587
if let Some(ref func) = self.options.before_send_metric {
metric = func(metric)?;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The before_send_metric callback is invoked even when options.enable_metrics is false, because the check happens after the callback has already executed.
Severity: MEDIUM

Suggested Fix

Add an early return guard at the beginning of the capture_metric function to check if self.options.enable_metrics is false. This will prevent prepare_metric and the before_send_metric callback from being called when metrics are disabled, aligning its behavior with the capture_log implementation.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry-core/src/client.rs#L585-L587

Potential issue: The `capture_metric` function calls `prepare_metric` without first
checking if metrics are enabled via `options.enable_metrics`. The `prepare_metric`
function then invokes the `before_send_metric` callback. The metric is only discarded
afterward because the `metrics_batcher` is `None`. This leads to unexpected side effects
from the callback executing even when the feature is disabled. This behavior is
inconsistent with `capture_log`, which correctly uses an early guard to prevent
processing when logs are disabled.

Did we get this right? 👍 / 👎 to inform future reviews.

szokeasaurusrex added a commit that referenced this pull request Apr 23, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
szokeasaurusrex added a commit that referenced this pull request Apr 23, 2026
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from d429da8 to 4ffac14 Compare April 23, 2026 20:54
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from 807de4c to 28bf132 Compare April 23, 2026 20:54
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from 4ffac14 to fb0e4b4 Compare April 23, 2026 21:21
szokeasaurusrex added a commit that referenced this pull request Apr 23, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from 28bf132 to aab78c9 Compare April 23, 2026 21:21
szokeasaurusrex added a commit that referenced this pull request Apr 23, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from fb0e4b4 to 4cba491 Compare April 23, 2026 21:22
szokeasaurusrex added a commit that referenced this pull request Apr 23, 2026
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from 4cba491 to 9d92d41 Compare April 23, 2026 21:25
szokeasaurusrex added a commit that referenced this pull request Apr 23, 2026
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/user-attributes-metrics branch from aab78c9 to aeedb59 Compare April 23, 2026 21:25
Run metrics through `ClientOptions::before_send_metric` after scope/default attribute enrichment and before enqueue so applications can filter or mutate metrics before batching.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>

 Closes #1025
Closes [RUST-170](https://linear.app/getsentry/issue/RUST-170/add-before-send-metric-callback-processing-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/before-send-metric branch from 9d92d41 to 7eb91c1 Compare April 23, 2026 21:25
szokeasaurusrex added a commit that referenced this pull request Apr 23, 2026
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