Skip to content

feat(metrics): Associate trace data with metrics#1061

Open
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/core-metrics-capturefrom
issue-1058-trace-metric
Open

feat(metrics): Associate trace data with metrics#1061
szokeasaurusrex wants to merge 1 commit intoszokeasaurusrex/core-metrics-capturefrom
issue-1058-trace-metric

Conversation

@szokeasaurusrex
Copy link
Copy Markdown
Member

@szokeasaurusrex szokeasaurusrex commented Apr 15, 2026

Set trace and span id on metrics.

Stacked on #1026

Co-authored-by: Joris Bayer joris.bayer@sentry.io
Closes #1058
Closes RUST-186

@linear-code
Copy link
Copy Markdown

linear-code bot commented Apr 15, 2026

@szokeasaurusrex szokeasaurusrex changed the title feat(metrics): Associate trace data with metrics (RUST-186) feat(metrics): Associate trace data with metrics Apr 15, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 15, 2026

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

Generated by 🚫 dangerJS against 3d71e21

@szokeasaurusrex szokeasaurusrex force-pushed the issue-1058-trace-metric branch from d425a32 to 8e8205e Compare April 15, 2026 16:02
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review April 15, 2026 16:03
@szokeasaurusrex szokeasaurusrex requested a review from lcian as a code owner April 15, 2026 16:03
@szokeasaurusrex szokeasaurusrex force-pushed the issue-1058-trace-metric branch 2 times, most recently from 8077154 to e2abb12 Compare April 15, 2026 16:04
.map(|span| span.get_trace_context().trace_id)
.unwrap_or(self.propagation_context.trace_id);

metric.span_id = self.get_span().map(|span| span.span_id());
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.

We should fall back on the propagation context to get the span ID, as it seems the Python implementation does it that way: https://github.com/getsentry/sentry-python/blob/5d26c2f60e289c89ab260ee25607a3bea5e174c9/sentry_sdk/scope.py#L1828.

I saw that logs do the same and only consider self.span, so we should probably fix that too.

Comment thread sentry-core/src/client.rs

/// Prepares a metric to be sent, setting trace association data from the scope.
#[cfg(feature = "metrics")]
fn prepare_metric(&self, mut metric: Metric, scope: &Scope) -> Option<Metric> {
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.

This could also return just Metric. If we need to have before_send_metric (not sure if that exists) then I guess Option<Metric> makes sense though. It's an internal API anyway so it doesn't really matter.

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 other than the comment about span_id.

@szokeasaurusrex szokeasaurusrex force-pushed the issue-1058-trace-metric branch from e2abb12 to 9f8e156 Compare April 16, 2026 18:48
.unwrap_or(self.propagation_context.trace_id);

metric.span_id = self.get_span().map(|span| span.span_id());
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Redundant double get_span() call in apply_to_metric

Low Severity

apply_to_metric calls self.get_span() twice, each time cloning the Option<TransactionOrSpan>. For the Transaction variant, this also results in get_trace_context() being called twice (once explicitly for trace_id, once internally via span_id()), each acquiring a mutex lock and cloning the full TraceContext. A single get_span() call, stored in a local variable, would avoid the redundant cloning and lock acquisitions. The sibling method apply_to_log already follows this pattern by checking self.span.as_ref() once.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9f8e156. Configure here.


/// Applies the contained scoped data to a trace metric, setting the `trace_id` and `span_id`.
#[cfg(feature = "metrics")]
pub(crate) fn apply_to_metric(&self, metric: &mut 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.

Inconsistent visibility between noop and real apply_to_metric

Low Severity

apply_to_metric is pub(crate) in real.rs but pub in noop.rs. Every other scope method (apply_to_log, apply_to_event, set_span, get_span, etc.) uses pub consistently in both implementations. This breaks the established pattern and means the method's external accessibility changes depending on whether the client feature flag is enabled.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9f8e156. Configure here.

Set trace and span id on metrics.

Co-authored-by: Joris Bayer <joris.bayer@sentry.io>
Closes #1058
Closes [RUST-186](https://linear.app/getsentry/issue/RUST-186/add-trace-metric-tracing-association-in-sentry-core)
@szokeasaurusrex szokeasaurusrex force-pushed the issue-1058-trace-metric branch from 9f8e156 to 3d71e21 Compare April 16, 2026 19:47
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 3 total unresolved issues (including 2 from previous reviews).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3d71e21. Configure here.

.unwrap_or(self.propagation_context.trace_id);

metric.span_id = self.get_span().map(|span| span.span_id());
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Metric span_id doesn't fall back to propagation context

Medium Severity

In apply_to_metric, trace_id falls back to self.propagation_context.trace_id when no active span exists, but span_id does not similarly fall back to self.propagation_context.span_id — it just becomes None. This is inconsistent within the method itself (trace_id has a fallback but span_id doesn't), inconsistent with apply_to_event (which sets both trace_id and span_id from the propagation context when no span is active), and inconsistent with the Python SDK reference implementation. The test assertions for is_none() appear to encode the wrong expected behavior.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 3d71e21. Configure here.

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