Skip to content

feat(statsd): Add sampling parameter to distribution metrics#5576

Merged
jjbayer merged 13 commits intomasterfrom
feat/distribution-sampling-rate
Jan 23, 2026
Merged

feat(statsd): Add sampling parameter to distribution metrics#5576
jjbayer merged 13 commits intomasterfrom
feat/distribution-sampling-rate

Conversation

@jjbayer
Copy link
Copy Markdown
Member

@jjbayer jjbayer commented Jan 23, 2026

The metric!(distribution(...)) and metric!(timer(...)) macros now take an optional sample parameter to explicitly set the sampling rate the metric. This allows per-metric control over sampling rates via cadence's with_sampling_rate.

Example usage:

metric!(distribution(MyMetric, sample = 0.01) = value);

ref: INGEST-659

jjbayer and others added 2 commits January 23, 2026 11:42
…er metrics

The `metric!(distribution(...))` and `metric!(timer(...))` macros now support
an optional `sampling` parameter to explicitly set a per-metric sampling rate
that overrides the global sample rate.

Example usage:
```rust
// Uses global sample rate (default behavior, unchanged)
metric!(distribution(MyMetric) = value);
metric!(timer(MyTimer) = duration);

// Override with explicit sample rate
metric!(distribution(MyMetric, sampling = 0.01) = value);
metric!(timer(MyTimer, sampling = 0.01) = duration);
metric!(timer(MyTimer, sampling = 0.01), { block });
```

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jjbayer jjbayer force-pushed the feat/distribution-sampling-rate branch from 8581a4f to 24862d6 Compare January 23, 2026 11:42
@jjbayer jjbayer changed the title feat(statsd): Add required sampling parameter to distribution metrics feat(statsd): Add sampling parameter to distribution metrics Jan 23, 2026
pub fn send_metric_with_sample_rate<'a, T>(
&'a self,
mut metric: MetricBuilder<'a, '_, T>,
sample_rate: Option<f64>,
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.

Unfortunately we cannot inspect MetricBuilder to check whether a sample rate was set, so we have to pass it explicitly.

@linear
Copy link
Copy Markdown

linear bot commented Jan 23, 2026

Self::InMemory(buffer) => buffer.push(envelope).await,
}?;
relay_statsd::metric!(
distribution(RelayDistributions::BufferEnvelopeBodySize, sample = 0.01) =
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 reverted the temporary change of #5571 and set a sample rate instead.

@jjbayer jjbayer marked this pull request as ready for review January 23, 2026 12:00
@jjbayer jjbayer requested a review from a team as a code owner January 23, 2026 12:01
pub fn send_metric_with_sample_rate<'a, T>(
&'a self,
mut metric: MetricBuilder<'a, '_, T>,
sample_rate: Option<f64>,
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.

Any specific reason you went with f64 over f32 which is used for the config?

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.

f64 is what cadence expects, that's all (self.sample_rate gets converted to f64 down below).

cursor[bot]

This comment was marked as outdated.

@jjbayer jjbayer enabled auto-merge January 23, 2026 12:19
cursor[bot]

This comment was marked as outdated.

@jjbayer jjbayer added this pull request to the merge queue Jan 23, 2026
@jjbayer jjbayer removed this pull request from the merge queue due to a manual request Jan 23, 2026
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 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@jjbayer jjbayer enabled auto-merge January 23, 2026 13:35
@jjbayer jjbayer added this pull request to the merge queue Jan 23, 2026
Merged via the queue into master with commit 8f13694 Jan 23, 2026
29 checks passed
@jjbayer jjbayer deleted the feat/distribution-sampling-rate branch January 23, 2026 13:58
@jjbayer jjbayer mentioned this pull request Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants