Skip to content

Commit

Permalink
wip: vrl
Browse files Browse the repository at this point in the history
  • Loading branch information
davidB committed Aug 20, 2024
1 parent a50ba3f commit 487703b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions cdviz-collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ time = "0.3"
tokio = { version = "1.37", features = ["full"] }
tracing = "0.1"
tracing-opentelemetry-instrumentation-sdk = { version = "0.19" }
vrl = "0.17.0"

[dev-dependencies]
assert2 = "0.3"
Expand Down
42 changes: 42 additions & 0 deletions cdviz-collector/examples/vrl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::collections::BTreeMap;
use vrl::{
compiler::{state::RuntimeState, Context, TargetValue, TimeZone},
value,
value::{Secrets, Value},
};

fn main() {
// The VRL source code to run. This just retrieves the value of field "x"
//let src = ".x";
let src = ".x == 1";

// Use all of the std library functions
let fns = vrl::stdlib::all();

// Compile the program (and panic if it's invalid)
let result = vrl::compiler::compile(src, &fns).unwrap();

// This is the target that can be accessed / modified in the VRL program.
// You can use any custom type that implements `Target`, but `TargetValue` is also provided for convenience.
let mut target = TargetValue {
// the value starts as just an object with a single field "x" set to 1
value: value!({x: 1}),
// the metadata is empty
metadata: Value::Object(BTreeMap::new()),
// and there are no secrets associated with the target
secrets: Secrets::default(),
};

// The current state of the runtime (i.e. local variables)
let mut state = RuntimeState::default();

let timezone = TimeZone::default();

// A context bundles all the info necessary for the runtime to resolve a value.
let mut ctx = Context::new(&mut target, &mut state, &timezone);

// This executes the VRL program, making any modifications to the target, and returning a result.
let value = result.program.resolve(&mut ctx).unwrap();

assert_eq!(value, value!(true));
}

0 comments on commit 487703b

Please sign in to comment.