Skip to content

Commit 74f059a

Browse files
committed
fix(tag_cardinality_limit): reject cache_size_per_key in any non-probabilistic mode
1 parent 65b1222 commit 74f059a

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
@@ -344,39 +344,40 @@ impl GenerateConfig for Config {
344344
#[derive(Debug, Snafu)]
345345
pub enum BuildError {
346346
#[snafu(display(
347-
"cache_size_per_key set on per-tag entry `{tag_key}` but the inherited mode is \
348-
`exact`, where it has no effect. Remove the field or switch to `probabilistic` mode."
347+
"cache_size_per_key set on per-tag entry `{tag_key}` but the inherited mode is not \
348+
`probabilistic`, where it has no effect. Remove the field or switch to `probabilistic` mode."
349349
))]
350-
CacheSizeInExactMode { tag_key: String },
350+
CacheSizeRequiresProbabilistic { tag_key: String },
351351
}
352352

353353
impl Config {
354354
fn validate(&self) -> crate::Result<()> {
355-
// Global per_tag_limits: validate against the global mode.
356-
if self.global.mode == Mode::Exact {
355+
// Global per_tag_limits: cache_size_per_key only applies in probabilistic mode.
356+
if !matches!(self.global.mode, Mode::Probabilistic(_)) {
357357
for (tag_key, tag_cfg) in &self.per_tag_limits {
358358
if let PerTagMode::LimitOverride {
359359
cache_size_per_key: Some(_),
360360
..
361361
} = tag_cfg.mode
362362
{
363-
return Err(Box::new(BuildError::CacheSizeInExactMode {
363+
return Err(Box::new(BuildError::CacheSizeRequiresProbabilistic {
364364
tag_key: tag_key.clone(),
365365
}));
366366
}
367367
}
368368
}
369369

370-
// Per-metric per_tag_limits: validate against each per-metric mode.
370+
// Per-metric per_tag_limits: cache_size_per_key only applies when the per-metric
371+
// mode is probabilistic (not exact, and not excluded).
371372
for per_metric in self.per_metric_limits.values() {
372-
if per_metric.config.mode == OverrideMode::Exact {
373+
if !matches!(per_metric.config.mode, OverrideMode::Probabilistic(_)) {
373374
for (tag_key, tag_cfg) in &per_metric.per_tag_limits {
374375
if let PerTagMode::LimitOverride {
375376
cache_size_per_key: Some(_),
376377
..
377378
} = tag_cfg.mode
378379
{
379-
return Err(Box::new(BuildError::CacheSizeInExactMode {
380+
return Err(Box::new(BuildError::CacheSizeRequiresProbabilistic {
380381
tag_key: tag_key.clone(),
381382
}));
382383
}

src/transforms/tag_cardinality_limit/tests.rs

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

1649+
/// cache_size_per_key on a per-metric per-tag entry in excluded mode is a build error.
1650+
#[tokio::test]
1651+
async fn validation_rejects_cache_size_in_per_metric_excluded_mode() {
1652+
let config = make_transform_hashset_with_per_metric_limits(
1653+
500,
1654+
LimitExceededAction::DropTag,
1655+
HashMap::from([(
1656+
"metricA".to_string(),
1657+
make_per_metric_excluded(HashMap::from([(
1658+
"tag1".to_string(),
1659+
PerTagConfig {
1660+
mode: PerTagMode::LimitOverride {
1661+
value_limit: 5,
1662+
cache_size_per_key: Some(1024),
1663+
},
1664+
},
1665+
)])),
1666+
)]),
1667+
);
1668+
assert!(config.build(&TransformContext::default()).await.is_err());
1669+
}
1670+
16491671
/// cache_size_per_key in probabilistic mode is valid.
16501672
#[tokio::test]
16511673
async fn validation_allows_cache_size_in_probabilistic_mode() {

0 commit comments

Comments
 (0)