-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(tag_cardinality_limit transform): enable per metric limits for tag_cardinality_limit
#22077
Open
esensar
wants to merge
7
commits into
vectordotdev:master
Choose a base branch
from
esensar:feature/tag-cardinality-by-metric
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2fcd7a
feat(tag_cardinality_limit transform): enable per metric limits for `…
esensar 880a435
Add changelog entry
esensar 803043a
Fix typo in changelog entry
esensar 8adbcf0
Generate docs using `make generate-component-docs`
esensar 1a0babe
Use HashMap instead of Vec for `per_metric_limits`
esensar 7d12a37
Remove debugging logs from `tag_cardinality_limit`
esensar a83908a
Change `&Option` into `Option<&..>` in `tag_cardinality_limit`
esensar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
changelog.d/22077_per_metric_limits_for_tag_cardinality_limit_transform.feature.md
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,3 @@ | ||
The `tag_cardinality_limit` transform now supports customizing limits for specific metrics, matched by metric name and optionally its namespace. | ||
|
||
authors: esensar |
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 |
---|---|---|
|
@@ -19,13 +19,15 @@ mod tag_value_set; | |
mod tests; | ||
|
||
use crate::event::metric::TagValueSet; | ||
pub use config::TagCardinalityLimitConfig; | ||
pub use config::{TagCardinalityLimitConfig, TagCardinalityLimitInnerConfig}; | ||
use tag_value_set::AcceptedTagValueSet; | ||
|
||
type MetricId = (Option<String>, String); | ||
|
||
#[derive(Debug)] | ||
pub struct TagCardinalityLimit { | ||
config: TagCardinalityLimitConfig, | ||
accepted_tags: HashMap<String, AcceptedTagValueSet>, | ||
accepted_tags: HashMap<Option<MetricId>, HashMap<String, AcceptedTagValueSet>>, | ||
} | ||
|
||
impl TagCardinalityLimit { | ||
|
@@ -36,6 +38,22 @@ impl TagCardinalityLimit { | |
} | ||
} | ||
|
||
fn get_config_for_metric( | ||
&self, | ||
metric_key: &Option<MetricId>, | ||
) -> &TagCardinalityLimitInnerConfig { | ||
match metric_key { | ||
Some(id) => self | ||
.config | ||
.per_metric_limits | ||
.iter() | ||
.find(|c| c.name == id.1 && (c.namespace.is_none() || c.namespace == id.0)) | ||
.map(|c| &c.config) | ||
.unwrap_or(&self.config.global), | ||
None => &self.config.global, | ||
} | ||
} | ||
|
||
/// Takes in key and a value corresponding to a tag on an incoming Metric | ||
/// Event. If that value is already part of set of accepted values for that | ||
/// key, then simply returns true. If that value is not yet part of the | ||
|
@@ -44,22 +62,30 @@ impl TagCardinalityLimit { | |
/// for the key and returns true, otherwise returns false. A false return | ||
/// value indicates to the caller that the value is not accepted for this | ||
/// key, and the configured limit_exceeded_action should be taken. | ||
fn try_accept_tag(&mut self, key: &str, value: &TagValueSet) -> bool { | ||
let tag_value_set = self.accepted_tags.entry_ref(key).or_insert_with(|| { | ||
AcceptedTagValueSet::new(self.config.value_limit, &self.config.mode) | ||
}); | ||
fn try_accept_tag( | ||
&mut self, | ||
metric_key: &Option<MetricId>, | ||
key: &str, | ||
value: &TagValueSet, | ||
) -> bool { | ||
let config = self.get_config_for_metric(metric_key).clone(); | ||
info!("try_accept_tag using config: {:?}", config); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
let metric_accepted_tags = self.accepted_tags.entry(metric_key.clone()).or_default(); | ||
let tag_value_set = metric_accepted_tags | ||
.entry_ref(key) | ||
.or_insert_with(|| AcceptedTagValueSet::new(config.value_limit, &config.mode)); | ||
|
||
if tag_value_set.contains(value) { | ||
// Tag value has already been accepted, nothing more to do. | ||
return true; | ||
} | ||
|
||
// Tag value not yet part of the accepted set. | ||
if tag_value_set.len() < self.config.value_limit { | ||
if tag_value_set.len() < config.value_limit { | ||
// accept the new value | ||
tag_value_set.insert(value.clone()); | ||
|
||
if tag_value_set.len() == self.config.value_limit { | ||
if tag_value_set.len() == config.value_limit { | ||
emit!(TagCardinalityValueLimitReached { key }); | ||
} | ||
|
||
|
@@ -72,34 +98,58 @@ impl TagCardinalityLimit { | |
|
||
/// Checks if recording a key and value corresponding to a tag on an incoming Metric would | ||
/// exceed the cardinality limit. | ||
fn tag_limit_exceeded(&self, key: &str, value: &TagValueSet) -> bool { | ||
fn tag_limit_exceeded( | ||
&self, | ||
metric_key: &Option<MetricId>, | ||
esensar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
key: &str, | ||
value: &TagValueSet, | ||
) -> bool { | ||
self.accepted_tags | ||
.get(key) | ||
.map(|value_set| { | ||
!value_set.contains(value) && value_set.len() >= self.config.value_limit | ||
.get(metric_key) | ||
.and_then(|metric_accepted_tags| { | ||
metric_accepted_tags.get(key).map(|value_set| { | ||
!value_set.contains(value) | ||
&& value_set.len() >= self.get_config_for_metric(metric_key).value_limit | ||
}) | ||
}) | ||
.unwrap_or(false) | ||
} | ||
|
||
/// Record a key and value corresponding to a tag on an incoming Metric. | ||
fn record_tag_value(&mut self, key: &str, value: &TagValueSet) { | ||
self.accepted_tags | ||
fn record_tag_value(&mut self, metric_key: &Option<MetricId>, key: &str, value: &TagValueSet) { | ||
let config = self.get_config_for_metric(metric_key).clone(); | ||
let metric_accepted_tags = self.accepted_tags.entry(metric_key.clone()).or_default(); | ||
metric_accepted_tags | ||
.entry_ref(key) | ||
.or_insert_with(|| AcceptedTagValueSet::new(self.config.value_limit, &self.config.mode)) | ||
.or_insert_with(|| AcceptedTagValueSet::new(config.value_limit, &config.mode)) | ||
.insert(value.clone()); | ||
} | ||
|
||
fn transform_one(&mut self, mut event: Event) -> Option<Event> { | ||
let metric = event.as_mut_metric(); | ||
let metric_name = metric.name().to_string(); | ||
let metric_namespace = metric.namespace().map(|n| n.to_string()); | ||
info!("The config: {:?}", self.config); | ||
let has_per_metric_config = self.config.per_metric_limits.iter().any(|c| { | ||
c.name == metric_name && (c.namespace.is_none() || c.namespace == metric_namespace) | ||
}); | ||
let metric_key = if has_per_metric_config { | ||
info!("Metric specific config has been found!"); | ||
Some((metric_namespace, metric_name.clone())) | ||
} else { | ||
None | ||
}; | ||
if let Some(tags_map) = metric.tags_mut() { | ||
match self.config.limit_exceeded_action { | ||
match self | ||
.get_config_for_metric(&metric_key) | ||
.limit_exceeded_action | ||
{ | ||
LimitExceededAction::DropEvent => { | ||
// This needs to check all the tags, to ensure that the ordering of tag names | ||
// doesn't change the behavior of the check. | ||
|
||
for (key, value) in tags_map.iter_sets() { | ||
if self.tag_limit_exceeded(key, value) { | ||
if self.tag_limit_exceeded(&metric_key, key, value) { | ||
emit!(TagCardinalityLimitRejectingEvent { | ||
metric_name: &metric_name, | ||
tag_key: key, | ||
|
@@ -109,12 +159,12 @@ impl TagCardinalityLimit { | |
} | ||
} | ||
for (key, value) in tags_map.iter_sets() { | ||
self.record_tag_value(key, value); | ||
self.record_tag_value(&metric_key, key, value); | ||
} | ||
} | ||
LimitExceededAction::DropTag => { | ||
tags_map.retain(|key, value| { | ||
if self.try_accept_tag(key, value) { | ||
if self.try_accept_tag(&metric_key, key, value) { | ||
true | ||
} else { | ||
emit!(TagCardinalityLimitRejectingTag { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It's better to use a map type here from
name
toPerMetricConfig
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, not sure why I picked
Vec
, map makes much more sense.