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

enhancement(remap transform): add get_hostname function #6141

Merged
merged 12 commits into from
Jan 26, 2021
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
/docs/reference/remap/compact.cue @FungusHumungus
/docs/reference/remap/exists.cue @FungusHumungus
/docs/reference/remap/flatten.cue @FungusHumungus
/docs/reference/remap/get_hostname.cue @fanatid
/docs/reference/remap/ip_cidr_contains.cue @FungusHumungus
/docs/reference/remap/ip_subnet.cue @FungusHumungus
/docs/reference/remap/ip_to_ipv6.cue @FungusHumungus
Expand Down Expand Up @@ -103,6 +104,7 @@
/lib/remap-functions/src/compact.rs @FungusHumungus
/lib/remap-functions/src/exists.rs @FungusHumungus
/lib/remap-functions/src/flatten.rs @FungusHumungus
/lib/remap-functions/src/get_hostname.rs @fanatid
/lib/remap-functions/src/ip_cidr_contains.rs @FungusHumungus
/lib/remap-functions/src/ip_subnet.rs @FungusHumungus
/lib/remap-functions/src/ip_to_ipv6.rs @FungusHumungus
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions docs/reference/remap/functions/get_hostname.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package metadata

remap: functions: get_hostname: {
arguments: []
internal_failure_reasons: []
return: types: ["string"]
category: "System"
description: #"""
Get system's hostname.
"""#
examples: [
{
title: "Get hostname"
input: log: {}
source: #"""
.hostname = get_hostname!()
"""#
output: log: hostname: "localhost.localdomain"
},
]
}
3 changes: 3 additions & 0 deletions lib/remap-functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ chrono = { version = "0.4", optional = true }
cidr-utils = { version = "0.5", optional = true }
grok = { version = "1", optional = true }
hex = { version = "0.4", optional = true }
hostname = { version = "0.3", optional = true }
lazy_static = { version = "1", optional = true }
md-5 = { version = "0.9", optional = true }
nom = { version = "6.0.1", optional = true }
Expand Down Expand Up @@ -54,6 +55,7 @@ default = [
"format_number",
"format_timestamp",
"get_env_var",
"get_hostname",
"includes",
"ip_cidr_contains",
"ip_subnet",
Expand Down Expand Up @@ -122,6 +124,7 @@ floor = []
format_number = ["rust_decimal"]
format_timestamp = ["chrono"]
get_env_var = []
get_hostname = ["hostname"]
includes = []
ip_cidr_contains = ["cidr-utils"]
ip_subnet = ["lazy_static", "regex"]
Expand Down
58 changes: 58 additions & 0 deletions lib/remap-functions/src/get_hostname.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use remap::prelude::*;

#[derive(Clone, Copy, Debug)]
pub struct GetHostname;

impl Function for GetHostname {
fn identifier(&self) -> &'static str {
"get_hostname"
}

fn compile(&self, _: ArgumentList) -> Result<Box<dyn Expression>> {
Ok(Box::new(GetHostnameFn))
}
}

#[derive(Debug, Clone)]
struct GetHostnameFn;

impl Expression for GetHostnameFn {
fn execute(&self, _: &mut state::Program, _: &mut dyn Object) -> Result<Value> {
Ok(hostname::get()
.map_err(|error| format!("failed to get hostname: {}", error))?
.to_string_lossy()
.into())
}

fn type_def(&self, _: &state::Compiler) -> TypeDef {
TypeDef {
fallible: true,
kind: value::Kind::Bytes,
..Default::default()
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::map;

remap::test_type_def![static_def {
expr: |_| GetHostnameFn,
def: TypeDef {
fallible: true,
kind: value::Kind::Bytes,
..Default::default()
},
}];

#[test]
fn get_hostname() {
let mut state = state::Program::default();
let mut object: Value = map![].into();
let value = GetHostnameFn.execute(&mut state, &mut object).unwrap();

assert!(matches!(&value, Value::Bytes(_)));
}
}
6 changes: 6 additions & 0 deletions lib/remap-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ mod format_number;
mod format_timestamp;
#[cfg(feature = "get_env_var")]
mod get_env_var;
#[cfg(feature = "get_hostname")]
mod get_hostname;
#[cfg(feature = "includes")]
mod includes;
#[cfg(feature = "ip_cidr_contains")]
Expand Down Expand Up @@ -173,6 +175,8 @@ pub use format_number::FormatNumber;
pub use format_timestamp::FormatTimestamp;
#[cfg(feature = "get_env_var")]
pub use get_env_var::GetEnvVar;
#[cfg(feature = "get_hostname")]
pub use get_hostname::GetHostname;
#[cfg(feature = "includes")]
pub use includes::Includes;
#[cfg(feature = "ip_cidr_contains")]
Expand Down Expand Up @@ -308,6 +312,8 @@ pub fn all() -> Vec<Box<dyn remap::Function>> {
Box::new(FormatTimestamp),
#[cfg(feature = "get_env_var")]
Box::new(GetEnvVar),
#[cfg(feature = "get_hostname")]
Box::new(GetHostname),
#[cfg(feature = "includes")]
Box::new(Includes),
#[cfg(feature = "ip_cidr_contains")]
Expand Down
20 changes: 20 additions & 0 deletions tests/behavior/transforms/remap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1918,3 +1918,23 @@
[[tests.outputs.conditions]]
type = "remap"
source = '[.a, .b, .c] == [3, 2, 3]'

[transforms.remap_function_get_hostname]
inputs = []
type = "remap"
source = """
.a = get_hostname!()
"""
[[tests]]
name = "remap_function_get_hostname"
[tests.input]
insert_at = "remap_function_get_hostname"
type = "log"
[tests.input.log_fields]
[[tests.outputs]]
extract_from = "remap_function_get_hostname"
[[tests.outputs.conditions]]
type = "remap"
source = '''
.a != ""
'''