Skip to content
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

fix(sample transform)!: Accept remap conditions #7864

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/conditions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod remap;

pub use check_fields::CheckFieldsConfig;

use self::remap::RemapConfig;
pub use self::remap::RemapConfig;

pub trait Condition: Send + Sync + dyn_clone::DynClone {
fn check(&self, e: &Event) -> bool;
Expand Down
71 changes: 42 additions & 29 deletions src/transforms/sample.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
conditions::{CheckFieldsConfig, Condition, ConditionConfig},
conditions::{AnyCondition, Condition},
config::{DataType, GenerateConfig, GlobalOptions, TransformConfig, TransformDescription},
event::Event,
internal_events::SampleEventDiscarded,
Expand All @@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
pub struct SampleConfig {
pub rate: u64,
pub key_field: Option<String>,
pub exclude: Option<CheckFieldsConfig>,
pub exclude: Option<AnyCondition>,
}

inventory::submit! {
Expand All @@ -28,7 +28,7 @@ impl GenerateConfig for SampleConfig {
toml::Value::try_from(Self {
rate: 10,
key_field: None,
exclude: None,
exclude: None::<AnyCondition>,
})
.unwrap()
}
Expand Down Expand Up @@ -142,24 +142,20 @@ impl FunctionTransform for Sample {
mod tests {
use super::*;
use crate::{
conditions::check_fields::CheckFieldsPredicateArg, config::log_schema, event::Event,
test_util::random_lines, transforms::test::transform_one,
conditions::{ConditionConfig, RemapConfig},
config::log_schema,
event::Event,
test_util::random_lines,
transforms::test::transform_one,
};
use approx::assert_relative_eq;
use indexmap::IndexMap;

fn condition_contains(pre: &str) -> Box<dyn Condition> {
condition(log_schema().message_key(), "contains", pre)
}

fn condition(field: &str, condition: &str, value: &str) -> Box<dyn Condition> {
let mut preds: IndexMap<String, CheckFieldsPredicateArg> = IndexMap::new();
preds.insert(
format!("{}.{}", field, condition),
CheckFieldsPredicateArg::String(value.into()),
);

CheckFieldsConfig::new(preds).build().unwrap()
fn condition_contains(key: &str, needle: &str) -> Box<dyn Condition> {
RemapConfig {
source: format!(r#"contains!(."{}", "{}")"#, key, needle),
}
.build()
.unwrap()
}

#[test]
Expand All @@ -175,7 +171,7 @@ mod tests {
let mut sampler = Sample::new(
2,
Some(log_schema().message_key().into()),
Some(condition_contains("na")),
Some(condition_contains(log_schema().message_key(), "na")),
);
let total_passed = events
.into_iter()
Expand All @@ -193,7 +189,7 @@ mod tests {
let mut sampler = Sample::new(
25,
Some(log_schema().message_key().into()),
Some(condition_contains("na")),
Some(condition_contains(log_schema().message_key(), "na")),
);
let total_passed = events
.into_iter()
Expand All @@ -214,7 +210,7 @@ mod tests {
let mut sampler = Sample::new(
2,
Some(log_schema().message_key().into()),
Some(condition_contains("na")),
Some(condition_contains(log_schema().message_key(), "na")),
);

let first_run = events
Expand Down Expand Up @@ -242,8 +238,11 @@ mod tests {
fn always_passes_events_matching_pass_list() {
for key_field in &[None, Some(log_schema().message_key().into())] {
let event = Event::from("i am important");
let mut sampler =
Sample::new(0, key_field.clone(), Some(condition_contains("important")));
let mut sampler = Sample::new(
0,
key_field.clone(),
Some(condition_contains(log_schema().message_key(), "important")),
);
let iterations = 0..1000;
let total_passed = iterations
.filter_map(|_| {
Expand All @@ -257,12 +256,14 @@ mod tests {

#[test]
fn handles_key_field() {
for key_field in &[None, Some(log_schema().timestamp_key().into())] {
let event = Event::from("nananana");
for key_field in &[None, Some("other_field".into())] {
let mut event = Event::from("nananana");
let log = event.as_mut_log();
log.insert("other_field", "foo");
let mut sampler = Sample::new(
0,
key_field.clone(),
Some(condition(log_schema().timestamp_key(), "contains", ":")),
Some(condition_contains("other_field", "foo")),
);
let iterations = 0..1000;
let total_passed = iterations
Expand All @@ -279,7 +280,11 @@ mod tests {
fn sampler_adds_sampling_rate_to_event() {
for key_field in &[None, Some(log_schema().message_key().into())] {
let events = random_events(10000);
let mut sampler = Sample::new(10, key_field.clone(), Some(condition_contains("na")));
let mut sampler = Sample::new(
10,
key_field.clone(),
Some(condition_contains(log_schema().message_key(), "na")),
);
let passing = events
.into_iter()
.filter(|s| {
Expand All @@ -292,7 +297,11 @@ mod tests {
assert_eq!(passing.as_log()["sample_rate"], "10".into());

let events = random_events(10000);
let mut sampler = Sample::new(25, key_field.clone(), Some(condition_contains("na")));
let mut sampler = Sample::new(
25,
key_field.clone(),
Some(condition_contains(log_schema().message_key(), "na")),
);
let passing = events
.into_iter()
.filter(|s| {
Expand All @@ -305,7 +314,11 @@ mod tests {
assert_eq!(passing.as_log()["sample_rate"], "25".into());

// If the event passed the regex check, don't include the sampling rate
let mut sampler = Sample::new(25, key_field.clone(), Some(condition_contains("na")));
let mut sampler = Sample::new(
25,
key_field.clone(),
Some(condition_contains(log_schema().message_key(), "na")),
);
let event = Event::from("nananana");
let passing = transform_one(&mut sampler, event).unwrap();
assert!(passing.as_log().get("sample_rate").is_none());
Expand Down
19 changes: 14 additions & 5 deletions tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ async fn happy_path() {
inputs = ["in"]
rate = 10
key_field = "message"
exclude."message.contains" = "error"
exclude = """
contains!(.message, "error")
"""

[sinks.out]
type = "socket"
Expand All @@ -60,7 +62,7 @@ async fn happy_path() {
in = {type = "socket", mode = "tcp", address = "127.0.0.1:1235"}

[transforms]
sample = {type = "sample", inputs = ["in"], rate = 10, key_field = "message", exclude."message.contains" = "error"}
sample = {type = "sample", inputs = ["in"], rate = 10, key_field = "message", exclude = """ contains!(.message, "error") """ }

[sinks]
out = {type = "socket", mode = "tcp", inputs = ["sample"], encoding = "text", address = "127.0.0.1:9999"}
Expand Down Expand Up @@ -189,7 +191,9 @@ async fn nonexistant_input() {
inputs = ["qwerty"]
rate = 10
key_field = "message"
exclude."message.contains" = "error"
exclude = """
contains!(.message, "error")
"""

[sinks.out]
type = "socket"
Expand Down Expand Up @@ -273,6 +277,7 @@ async fn bad_regex() {
inputs = ["in"]
rate = 10
key_field = "message"
exclude.type = "check_fields"
exclude."message.regex" = "(["

[sinks.out]
Expand Down Expand Up @@ -479,14 +484,18 @@ async fn warnings() {
inputs = ["in1"]
rate = 10
key_field = "message"
exclude."message.contains" = "error"
exclude = """
contains!(.message, "error")
"""

[transforms.sample2]
type = "sample"
inputs = ["in1"]
rate = 10
key_field = "message"
exclude."message.contains" = "error"
exclude = """
contains!(.message, "error")
"""

[sinks.out]
type = "socket"
Expand Down