-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(capture): add ability to configure event dropping by token (#26756)
- Loading branch information
1 parent
cfa779f
commit 8955ed1
Showing
9 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod overflow; | ||
pub mod redis; | ||
pub mod token_dropper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::collections::HashMap; | ||
|
||
use tracing::warn; | ||
|
||
#[derive(Default)] | ||
pub struct TokenDropper { | ||
to_drop: HashMap<String, Vec<String>>, | ||
} | ||
|
||
impl TokenDropper { | ||
// Takes "<token>:<distinct_id or *>,<distinct_id or *>;<token>..." | ||
pub fn new(config: &str) -> Self { | ||
let mut to_drop = HashMap::new(); | ||
for pair in config.split(';') { | ||
let mut parts = pair.split(':'); | ||
let Some(token) = parts.next() else { | ||
warn!("No distinct id's configured for pair {}", pair); | ||
continue; | ||
}; | ||
let Some(ids) = parts.next() else { | ||
warn!("No distinct id's configured for token {}", token); | ||
continue; | ||
}; | ||
let ids = ids.split(',').map(|s| s.to_string()).collect(); | ||
to_drop.insert(token.to_string(), ids); | ||
} | ||
Self { to_drop } | ||
} | ||
|
||
pub fn should_drop(&self, token: &str, distinct_id: &str) -> bool { | ||
self.to_drop | ||
.get(token) | ||
.map(|ids| ids.iter().any(|id| id == distinct_id || id == "*")) | ||
.unwrap_or(false) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_empty_config() { | ||
let dropper = TokenDropper::new(""); | ||
assert!(!dropper.should_drop("token", "id")); | ||
} | ||
|
||
#[test] | ||
fn test_single_token_id() { | ||
let dropper = TokenDropper::new("token:id"); | ||
assert!(dropper.should_drop("token", "id")); | ||
assert!(!dropper.should_drop("token", "other")); | ||
} | ||
|
||
#[test] | ||
fn test_multiple_ids() { | ||
let dropper = TokenDropper::new("token:id1,id2"); | ||
assert!(dropper.should_drop("token", "id1")); | ||
assert!(dropper.should_drop("token", "id2")); | ||
assert!(!dropper.should_drop("token", "id3")); | ||
} | ||
|
||
#[test] | ||
fn test_wildcard() { | ||
let dropper = TokenDropper::new("token:*"); | ||
assert!(dropper.should_drop("token", "anything")); | ||
} | ||
|
||
#[test] | ||
fn test_multiple_tokens() { | ||
let dropper = TokenDropper::new("token1:id1;token2:id2"); | ||
assert!(dropper.should_drop("token1", "id1")); | ||
assert!(dropper.should_drop("token2", "id2")); | ||
assert!(!dropper.should_drop("token1", "id2")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters