Skip to content

sentry: Reduce trace sample rate for download endpoint #5423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/sentry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod middleware;

use crate::env_optional;
pub use middleware::CustomSentryMiddleware as SentryMiddleware;
use sentry::{ClientInitGuard, ClientOptions, IntoDsn};
use sentry::{ClientInitGuard, ClientOptions, IntoDsn, TransactionContext};
use std::sync::Arc;

/// Initializes the Sentry SDK from the environment variables.
///
Expand All @@ -28,13 +29,26 @@ pub fn init() -> ClientInitGuard {

let traces_sample_rate = env_optional("SENTRY_TRACES_SAMPLE_RATE").unwrap_or(0.0);

let traces_sampler = move |ctx: &TransactionContext| -> f32 {
let is_download_endpoint =
ctx.name().starts_with("/api/v1/crates/") && ctx.name().ends_with("/download");

if is_download_endpoint {
// Reduce the sample rate for the download endpoint, since we have significantly
// more traffic on that endpoint compared to the rest
traces_sample_rate / 10.
} else {
traces_sample_rate
}
};

let opts = ClientOptions {
auto_session_tracking: true,
dsn,
environment,
release,
session_mode: sentry::SessionMode::Request,
traces_sample_rate,
traces_sampler: Some(Arc::new(traces_sampler)),
..Default::default()
};

Expand Down