Skip to content

Commit 68fe2f2

Browse files
committed
fix(tag_cardinality_limit): reject cache_size_per_key in any non-probabilistic mode
1 parent 40b10c5 commit 68fe2f2

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

src/transforms/tag_cardinality_limit/config.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,39 +355,40 @@ impl GenerateConfig for Config {
355355
#[derive(Debug, Snafu)]
356356
pub enum BuildError {
357357
#[snafu(display(
358-
"cache_size_per_key set on per-tag entry `{tag_key}` but the inherited mode is \
359-
`exact`, where it has no effect. Remove the field or switch to `probabilistic` mode."
358+
"cache_size_per_key set on per-tag entry `{tag_key}` but the inherited mode is not \
359+
`probabilistic`, where it has no effect. Remove the field or switch to `probabilistic` mode."
360360
))]
361-
CacheSizeInExactMode { tag_key: String },
361+
CacheSizeRequiresProbabilistic { tag_key: String },
362362
}
363363

364364
impl Config {
365365
fn validate(&self) -> crate::Result<()> {
366-
// Global per_tag_limits: validate against the global mode.
367-
if self.global.mode == Mode::Exact {
366+
// Global per_tag_limits: cache_size_per_key only applies in probabilistic mode.
367+
if !matches!(self.global.mode, Mode::Probabilistic(_)) {
368368
for (tag_key, tag_cfg) in &self.per_tag_limits {
369369
if let PerTagMode::LimitOverride {
370370
cache_size_per_key: Some(_),
371371
..
372372
} = tag_cfg.mode
373373
{
374-
return Err(Box::new(BuildError::CacheSizeInExactMode {
374+
return Err(Box::new(BuildError::CacheSizeRequiresProbabilistic {
375375
tag_key: tag_key.clone(),
376376
}));
377377
}
378378
}
379379
}
380380

381-
// Per-metric per_tag_limits: validate against each per-metric mode.
381+
// Per-metric per_tag_limits: cache_size_per_key only applies when the per-metric
382+
// mode is probabilistic (not exact, and not excluded).
382383
for per_metric in self.per_metric_limits.values() {
383-
if per_metric.config.mode == OverrideMode::Exact {
384+
if !matches!(per_metric.config.mode, OverrideMode::Probabilistic(_)) {
384385
for (tag_key, tag_cfg) in &per_metric.per_tag_limits {
385386
if let PerTagMode::LimitOverride {
386387
cache_size_per_key: Some(_),
387388
..
388389
} = tag_cfg.mode
389390
{
390-
return Err(Box::new(BuildError::CacheSizeInExactMode {
391+
return Err(Box::new(BuildError::CacheSizeRequiresProbabilistic {
391392
tag_key: tag_key.clone(),
392393
}));
393394
}

src/transforms/tag_cardinality_limit/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,28 @@ async fn validation_rejects_cache_size_in_per_metric_exact_mode() {
17561756
assert!(config.build(&TransformContext::default()).await.is_err());
17571757
}
17581758

1759+
/// cache_size_per_key on a per-metric per-tag entry in excluded mode is a build error.
1760+
#[tokio::test]
1761+
async fn validation_rejects_cache_size_in_per_metric_excluded_mode() {
1762+
let config = make_transform_hashset_with_per_metric_limits(
1763+
500,
1764+
LimitExceededAction::DropTag,
1765+
HashMap::from([(
1766+
"metricA".to_string(),
1767+
make_per_metric_excluded(HashMap::from([(
1768+
"tag1".to_string(),
1769+
PerTagConfig {
1770+
mode: PerTagMode::LimitOverride {
1771+
value_limit: 5,
1772+
cache_size_per_key: Some(1024),
1773+
},
1774+
},
1775+
)])),
1776+
)]),
1777+
);
1778+
assert!(config.build(&TransformContext::default()).await.is_err());
1779+
}
1780+
17591781
/// cache_size_per_key in probabilistic mode is valid.
17601782
#[tokio::test]
17611783
async fn validation_allows_cache_size_in_probabilistic_mode() {

0 commit comments

Comments
 (0)