Skip to content

Commit

Permalink
Update sampling config to use the JSON format (#31247)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 1898424d22a71be0ace1a24f0e7f24d227a049e7
  • Loading branch information
sshader authored and Convex, Inc. committed Nov 7, 2024
1 parent 51f3774 commit e6d9941
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
39 changes: 21 additions & 18 deletions crates/common/src/knobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,35 +1100,38 @@ pub static MAX_PUSH_BYTES: LazyLock<usize> =
LazyLock::new(|| env_config("MAX_PUSH_BYTES", 100_000_000));

/// Percentage of request traces that should sampled.
/// Can set a global value, as well as per-route values
///
/// Note that the regexes can't contain commas.
/// Sampling config is a JSON object with the following format:
/// {
/// instanceOverrides: {
/// instanceName1: [{routeRegexp1: fraction1}, {routeRegexp2:
/// fraction2}, ...], instanceName2: [{routeRegexp1: fraction1},
/// {routeRegexp2: fraction2}, ...], ...
/// },
/// routeOverrides: [{routeRegexp: fraction}, ...],
/// defaultFraction: fraction
/// }
///
/// Enable sampling for 10% of /api/push_config and 0.001% of all requests
/// Use knobs to enable to higher limits for individual instances.
/// (see also the doc comment on SamplingConfigJson).
///
/// You can also enable sampling for individual instance_names if applicable,
/// e.g. in Usher.
/// These apply in order -- the instance overrides take precedence over route
/// overrides, which take precedence over the default fraction.
///
/// When in doubt, write out a test case to verify the behavior in
/// minitrace_helpers.rs.
///
/// Examples:
/// REQUEST_TRACE_SAMPLE_CONFIG=0.01
/// REQUEST_TRACE_SAMPLE_CONFIG=/route1=0.50,0.01
/// REQUEST_TRACE_SAMPLE_CONFIG=/route1=0.50,route2=0.50,0.01
/// REQUEST_TRACE_SAMPLE_CONFIG=/http/.*=0.50
/// It's often easiest to craft the JSON in a repl (e.g. node, browser console),
/// and then stringify it at the end.
///
/// REQUEST_TRACE_SAMPLE_CONFIG=carnitas:/route1=0.5,alpastor:1.0,0.01
/// This configures:
/// - sampling for "/route1" for instance name "carnitas" to 0.5
/// - sampling for all methods for instance name "alpastor" to 1.0
/// - sampling for anything else to 0.01
/// See `minitrace_helpers.rs` for more examples
pub static REQUEST_TRACE_SAMPLE_CONFIG: LazyLock<SamplingConfig> = LazyLock::new(|| {
env_config(
"REQUEST_TRACE_SAMPLE_CONFIG",
prod_override(
SamplingConfig::default(),
"/api/push_config=0.1,0.00001".parse().unwrap(),
r#"{"defaultFraction":0.00001,"routeOverrides":[{"routeRegexp":"/api/push_config","fraction":0.1}]}"#
.parse()
.unwrap(),
),
)
});
Expand Down
10 changes: 5 additions & 5 deletions crates/common/src/minitrace_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ mod tests {
"defaultFraction": 0.01
}"#
.parse()?;
assert_eq!(config.by_regex.len(), 3);
assert_eq!(config.sample_ratio("carnitas", "a"), 0.5);
assert_eq!(config.sample_ratio("carnitas", "b"), 0.15);
assert_eq!(config.sample_ratio("carnitas", "c"), 0.01);
Expand All @@ -351,15 +350,17 @@ mod tests {
"defaultFraction": 0.0
}"#
.parse()?;
assert_eq!(config.by_regex.len(), 2);
assert_eq!(config.sample_ratio("carnitas", "/f/a"), 0.5);
assert_eq!(config.sample_ratio("carnitas", "/f/b"), 0.5);
assert_eq!(config.sample_ratio("carnitas", "c"), 0.0);

// Instance overrides.
let config: SamplingConfig = r#"{
"instanceOverrides": {
"alpastor": [ { "routeRegexp": "a", "fraction": 0.5 } ],
"alpastor": [
{ "routeRegexp": "a", "fraction": 0.5 },
{ "routeRegexp": "c", "fraction": 0.5 }
],
"carnitas": [ { "routeRegexp": ".*", "fraction": 0.01 } ]
},
"routeOverrides": [
Expand All @@ -368,13 +369,12 @@ mod tests {
"defaultFraction": 1.0
}"#
.parse()?;
assert_eq!(config.by_regex.len(), 4);
assert_eq!(config.sample_ratio("carnitas", "a"), 0.01);
assert_eq!(config.sample_ratio("carnitas", "b"), 0.01);
assert_eq!(config.sample_ratio("carnitas", "c"), 0.01);
assert_eq!(config.sample_ratio("alpastor", "a"), 0.5);
assert_eq!(config.sample_ratio("alpastor", "b"), 0.15);
assert_eq!(config.sample_ratio("alpastor", "c"), 1.0);
assert_eq!(config.sample_ratio("alpastor", "c"), 0.5);
assert_eq!(config.sample_ratio("chorizo", "a"), 1.0);
assert_eq!(config.sample_ratio("chorizo", "b"), 0.15);
assert_eq!(config.sample_ratio("chorizo", "c"), 1.0);
Expand Down

0 comments on commit e6d9941

Please sign in to comment.