Skip to content

Commit

Permalink
Expose log path and ability to set value to lua API (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekimekim authored Feb 23, 2018
1 parent dcbeb7d commit e2f557d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
10 changes: 10 additions & 0 deletions resources/tests/scripts/field_from_path.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function process_metric(pyld)
end

function process_log(pyld)
local path = payload.log_path(pyld, 1)
payload.log_set_field(pyld, 1, "foo", path)
end

function tick(pyld)
end
9 changes: 9 additions & 0 deletions resources/tests/scripts/set_value.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function process_metric(pyld)
end

function process_log(pyld)
payload.log_set_value(pyld, 1, "foo")
end

function tick(pyld)
end
30 changes: 29 additions & 1 deletion src/filter/programmable_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ impl<'a> Payload<'a> {
1
}

#[allow(non_snake_case)]
unsafe extern "C" fn lua_log_path(L: *mut lua_State) -> c_int {
let mut state = State::from_ptr(L);
let pyld = state.to_userdata(1) as *mut Payload;
let idx = idx(state.to_integer(2), (*pyld).logs.len());
state.push_string(&(*pyld).logs[idx].path);
1
}

#[allow(non_snake_case)]
unsafe extern "C" fn lua_set_metric_name(L: *mut lua_State) -> c_int {
let mut state = State::from_ptr(L);
Expand Down Expand Up @@ -307,6 +316,23 @@ impl<'a> Payload<'a> {
1
}

#[allow(non_snake_case)]
unsafe extern "C" fn lua_log_set_value(L: *mut lua_State) -> c_int {
let mut state = State::from_ptr(L);
let pyld = state.to_userdata(1) as *mut Payload;
let idx = idx(state.to_integer(2), (*pyld).logs.len());
match state.to_str(3).map(|k| k.to_owned()) {
Some(key) => {
(*pyld).logs[idx].value = key;
},
None => {
error!("[log_set_value] no val provided");
}
}
state.push_nil();
1
}

#[allow(non_snake_case)]
unsafe extern "C" fn lua_metric_remove_tag(L: *mut lua_State) -> c_int {
let mut state = State::from_ptr(L);
Expand Down Expand Up @@ -369,16 +395,18 @@ impl<'a> Payload<'a> {
}
}

const PAYLOAD_LIB: [(&str, Function); 16] = [
const PAYLOAD_LIB: [(&str, Function); 18] = [
("set_metric_name", Some(Payload::lua_set_metric_name)),
("clear_logs", Some(Payload::lua_clear_logs)),
("clear_metrics", Some(Payload::lua_clear_metrics)),
("log_remove_tag", Some(Payload::lua_log_remove_tag)),
("log_set_tag", Some(Payload::lua_log_set_tag)),
("log_tag_value", Some(Payload::lua_log_tag_value)),
("log_set_field", Some(Payload::lua_log_set_field)),
("log_set_value", Some(Payload::lua_log_set_value)),
("log_field_value", Some(Payload::lua_log_field_value)),
("log_value", Some(Payload::lua_log_value)),
("log_path", Some(Payload::lua_log_path)),
("metric_query", Some(Payload::lua_metric_query)),
("metric_remove_tag", Some(Payload::lua_metric_remove_tag)),
("metric_set_tag", Some(Payload::lua_metric_set_tag)),
Expand Down
71 changes: 71 additions & 0 deletions tests/programmable_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,77 @@ mod integration {
assert_eq!(events[0], expected_event);
}

#[test]
fn test_get_log_path() {
let mut script = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
script.push("resources/tests/scripts");
let script_dir = script.clone();
script.push("field_from_path.lua");

let config = ProgrammableFilterConfig {
scripts_directory: Some(script_dir),
script: Some(script),
forwards: Vec::new(),
config_path: Some("filters.field_from_path".to_string()),
tags: Default::default(),
};
let mut cs = ProgrammableFilter::new(config);

let orig_log = metric::LogLine::new(
"identity",
"i am the very model of the modern major general",
);
let expected_log = metric::LogLine::new(
"identity",
"i am the very model of the modern major \
general",
).insert_field("foo", "identity");
let orig_event = metric::Event::new_log(orig_log);
let expected_event = metric::Event::new_log(expected_log);

let mut events = Vec::new();
let res = cs.process(orig_event.clone(), &mut events);
assert!(res.is_ok());
assert!(!events.is_empty());
assert_eq!(events.len(), 1);
assert_eq!(events[0], expected_event);
}

#[test]
fn test_set_log_value() {
let mut script = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
script.push("resources/tests/scripts");
let script_dir = script.clone();
script.push("set_value.lua");

let config = ProgrammableFilterConfig {
scripts_directory: Some(script_dir),
script: Some(script),
forwards: Vec::new(),
config_path: Some("filters.set_value".to_string()),
tags: Default::default(),
};
let mut cs = ProgrammableFilter::new(config);

let orig_log = metric::LogLine::new(
"identity",
"i am the very model of the modern major general",
);
let expected_log = metric::LogLine::new(
"identity",
"foo"
);
let orig_event = metric::Event::new_log(orig_log);
let expected_event = metric::Event::new_log(expected_log);

let mut events = Vec::new();
let res = cs.process(orig_event.clone(), &mut events);
assert!(res.is_ok());
assert!(!events.is_empty());
assert_eq!(events.len(), 1);
assert_eq!(events[0], expected_event);
}

#[test]
fn test_remove_metric_tag_kv() {
let mut script = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down

0 comments on commit e2f557d

Please sign in to comment.