Skip to content

Allow overriding sampling rate for events #1127

Closed as not planned
Closed as not planned

Description

@darrell-roberts

Problem Statement

Most event capture will use the sampling rate for the client, however there are some events we consider high priority we want to always send. This would require a user defined function that allows use to set something on the event that we could check for by-passing the sampling rate.

Solution Brainstorm

Add a user defined function to ClientOptions that receives an event and checks if it qualifies for ignoring the configured sampling rate.
Have prepare_event check for user defined sampling override and check if sampling rate should be ignored.

Activity

  1. added
    Feature
    Rust
    on May 17, 2026
  2. linear-code commented on May 17, 2026

    @linear-code
  3. darrell-roberts commented on May 17, 2026

    @darrell-roberts
    Author

    Created draft PR: #1128

  4. szokeasaurusrex commented on May 18, 2026

    @szokeasaurusrex
    Member

    Hi @darrell-roberts, thanks for the issue and the draft PR!

    Rather than adding a function that returns a boolean to fully ignore the sample rate, I would suggest we instead add support for an error_sampler function, like we have in the Python SDK, which returns a floating-point value instead of a boolean. That version allows sampling different events at different sample rates, in addition to supporting your use case.

    However, as adding fields to the ClientOptions is a breaking change, we will need to carefully consider if/when we should make this change.

  5. darrell-roberts commented on May 18, 2026

    @darrell-roberts
    Author

    Hi @darrell-roberts, thanks for the issue and the draft PR!

    Rather than adding a function that returns a boolean to fully ignore the sample rate, I would suggest we instead add support for an error_sampler function, like we have in the Python SDK, which returns a floating-point value instead of a boolean. That version allows sampling different events at different sample rates, in addition to supporting your use case.

    Hi @szokeasaurusrex

    Thanks. The approach in the python SDK is a better approach. I've updated the draft PR with the suggestion.

    However, as adding fields to the ClientOptions is a breaking change, we will need to carefully consider if/when we should make this change.

    This may be ok, in the sense if no user defined sampling rate override function is provided it falls back to the default behavior.

  6. szokeasaurusrex commented on May 18, 2026

    @szokeasaurusrex
    Member

    This may be ok, in the sense if no user defined sampling rate override function is provided it falls back to the default behavior.

    @darrell-roberts the problem is that the ClientOptions struct is a pub struct with all pub fields. Even though most users will likely construct ClientOptions by setting a few fields manually then having ..Default::default fill in the rest, it is still possible to exhaustively construct and/or to exhaustively match against the ClientOptions struct. As adding a field will break code which exhaustively constructs or matches the struct, it is a breaking change.

    At some point, I would like to refractor ClientOptions so that we can add functionality in a non-breaking manner, but this itself will be a disruptive breaking change, as it would require fundamentally changing the API. So that will only happen after careful consideration.

  7. darrell-roberts commented on May 19, 2026

    @darrell-roberts
    Author

    This may be ok, in the sense if no user defined sampling rate override function is provided it falls back to the default behavior.

    @darrell-roberts the problem is that the ClientOptions struct is a pub struct with all pub fields. Even though most users will likely construct ClientOptions by setting a few fields manually then having ..Default::default fill in the rest, it is still possible to exhaustively construct and/or to exhaustively match against the ClientOptions struct. As adding a field will break code which exhaustively constructs or matches the struct, it is a breaking change.

    At some point, I would like to refractor ClientOptions so that we can add functionality in a non-breaking manner, but this itself will be a disruptive breaking change, as it would require fundamentally changing the API. So that will only happen after careful consideration.

    What do you suggest I do with this issue / PR?

  8. szokeasaurusrex commented on May 19, 2026

    @szokeasaurusrex
    Member

    @darrell-roberts the issue can remain and on the backlog. I have applied a label to indicate that this would require a breaking change. Unless we see huge demand for this feature, we likely will not make a major release only to add this feature, but we can include it with the next major release, whenever that occurs. I do not have a specific timeline for the next major release, but likely within a few months.

    I would ask that we close the PR for now, just to keep things tidy. We can revive the branch whenever we are ready to make the change.

  9. szokeasaurusrex commented on May 19, 2026

    @szokeasaurusrex
    Member

    Also, as a workaround until we release this change, you might be able to implement custom sampling logic in before_send.

    Doing this would require you to set a sample_rate of 1.0 (the default), and then to implement your custom sampling logic in before_send. This setup could look something like the following pseudocode:

    use rand::Rng;
    use sentry::{protocol::Event, ClientOptions};
    
    fn sample_rate(_event: &Event<'static>) -> f64 {
        todo!("implement custom sampling logic");
    }
    
    let _guard = sentry::init(
        ClientOptions::new()
            .dsn("<dsn>")
            .sample_rate(1.0)
            .before_send(|event| {
                (rand::rng().random::<f64>() < sample_rate(&event)).then_some(event)
            }),
    );
  10. szokeasaurusrex commented on Jul 29, 2026

    @szokeasaurusrex
    Member

    Hi @darrell-roberts, following the release of SDK version 0.49.0, we can now add new client configuration options without necessitating a breaking change, since the relevant struct was made #[non_exhaustive].

    If you are interested, you can update your PR (some APIs have changed), and then reopen it.

  11. darrell-roberts commented on Aug 11, 2026

    @darrell-roberts
    Author

    Hi @darrell-roberts, following the release of SDK version 0.49.0, we can now add new client configuration options without necessitating a breaking change, since the relevant struct was made #[non_exhaustive].

    If you are interested, you can update your PR (some APIs have changed), and then reopen it.

    Hi @szokeasaurusrex, Thanks. I've put up PR #1284.

  12. szokeasaurusrex commented on Aug 11, 2026

    @szokeasaurusrex
    Member

    Hi @darrell-roberts, sorry for the back and forth on this issue.

    I realize that I previously suggested reopening your original PR once ClientOptions became #[non_exhaustive]. However, after discussing this feature proposal internally with the broader SDK team, we decided not to add dynamic per-event error sampling to the Rust SDK at this time.

    The use case is valid, but the other SDKs we checked do not support this capability; the Python SDK is currently the only exception. A dynamic error sampler is also not defined in the Sentry SDK specification. Since this use case can already be implemented with a custom before_send callback, we do not want to introduce and maintain a Rust-specific API for it at this time.

    As I mentioned previously, the supported workaround is to set sample_rate to 1.0 (the default) and implement the custom sampling logic in before_send:

    use rand::Rng;
    use sentry::{protocol::Event, ClientOptions};
    
    fn sample_rate(_event: &Event<'static>) -> f64 {
        todo!("implement custom sampling logic");
    }
    
    let _guard = sentry::init(
        ClientOptions::new()
            .dsn("<dsn>")
            .sample_rate(1.0)
            .before_send(|event| {
                (rand::rng().random::<f64>() < sample_rate(&event)).then_some(event)
            }),
    );

    This is a product and maintenance decision rather than a judgment on the use case or the implementation. We appreciate you bringing the use case to our attention and taking the time to implement a solution. We would reconsider this decision if our cross-SDK direction changes or if there is significant community demand for the feature.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRust

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions