feat(metrics): Add before_send_metric callback#1064
feat(metrics): Add before_send_metric callback#1064szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/user-attributes-metricsfrom
Conversation
|
a9398ed to
cf1dd39
Compare
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)
cf1dd39 to
cdad82d
Compare
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)
cdad82d to
286f469
Compare
Stacked on #1064 Closes #1011 Closes [RUST-161](https://linear.app/getsentry/issue/RUST-161/add-trace-metric-rate-limiting-behavior-for-transport) Co-Authored-By: Joris Bayer <joris.bayer@sentry.io>
|
|
||
| assert_eq!( | ||
| metric.attributes.get("added_by_callback"), | ||
| Some(&LogAttribute(Value::from("yes"))), |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
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)
286f469 to
816fa87
Compare
948a361 to
aa6f863
Compare
816fa87 to
225d1dd
Compare
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)
Stacked on #1064 Closes #1011 Closes [RUST-161](https://linear.app/getsentry/issue/RUST-161/add-trace-metric-rate-limiting-behavior-for-transport) Co-Authored-By: Joris Bayer <joris.bayer@sentry.io>
aa6f863 to
eece55d
Compare
225d1dd to
bae243c
Compare
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)
Stacked on #1064 Closes #1011 Closes [RUST-161](https://linear.app/getsentry/issue/RUST-161/add-trace-metric-rate-limiting-behavior-for-transport) Co-Authored-By: Joris Bayer <joris.bayer@sentry.io>
eece55d to
787d60a
Compare
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)
Stacked on #1064 Closes #1011 Closes [RUST-161](https://linear.app/getsentry/issue/RUST-161/add-trace-metric-rate-limiting-behavior-for-transport) Co-Authored-By: Joris Bayer <joris.bayer@sentry.io>
bae243c to
d429da8
Compare
787d60a to
807de4c
Compare
| if let Some(ref func) = self.options.before_send_metric { | ||
| metric = func(metric)?; | ||
| } |
There was a problem hiding this comment.
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.
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)
Stacked on #1064 Closes #1011 Closes [RUST-161](https://linear.app/getsentry/issue/RUST-161/add-trace-metric-rate-limiting-behavior-for-transport) Co-Authored-By: Joris Bayer <joris.bayer@sentry.io>
d429da8 to
4ffac14
Compare
807de4c to
28bf132
Compare
4ffac14 to
fb0e4b4
Compare
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)
28bf132 to
aab78c9
Compare
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)
fb0e4b4 to
4cba491
Compare
Stacked on #1064 Closes #1011 Closes [RUST-161](https://linear.app/getsentry/issue/RUST-161/add-trace-metric-rate-limiting-behavior-for-transport) Co-Authored-By: Joris Bayer <joris.bayer@sentry.io>
4cba491 to
9d92d41
Compare
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)
aab78c9 to
aeedb59
Compare
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)
9d92d41 to
7eb91c1
Compare
Stacked on #1064 Closes #1011 Closes [RUST-161](https://linear.app/getsentry/issue/RUST-161/add-trace-metric-rate-limiting-behavior-for-transport) Co-Authored-By: Joris Bayer <joris.bayer@sentry.io>
Run metrics through
ClientOptions::before_send_metricafter 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