Skip to content

Commit

Permalink
+address review about being conditionnaly fallible or infallible
Browse files Browse the repository at this point in the history
Signed-off-by: prognant <pierre.rognant@datadoghq.com>
  • Loading branch information
prognant committed Jun 18, 2021
1 parent e8604e7 commit 5628523
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
11 changes: 8 additions & 3 deletions docs/reference/remap/functions/encode_key_value.cue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ remap: functions: encode_key_value: {
Encodes the `value` to in key/value format with customizable delimiters. Default delimiters match
the [logfmt](\(urls.logfmt)) format.
"""#
notices: [
"""
If `fields_ordering` is specified then the function is fallible else it is infallible.
""",
]

arguments: [
{
Expand Down Expand Up @@ -44,7 +49,7 @@ remap: functions: encode_key_value: {
{
title: "Encode with default delimiters (no ordering)"
source: """
encode_key_value!({"ts": "2021-06-05T17:20:00Z", "msg": "This is a message", "lvl": "info"})
encode_key_value({"ts": "2021-06-05T17:20:00Z", "msg": "This is a message", "lvl": "info"})
"""
return: #"lvl=info msg="This is a message" ts=2021-06-05T17:20:00Z"#
},
Expand All @@ -58,7 +63,7 @@ remap: functions: encode_key_value: {
{
title: "Encode with default delimiters (nested fields)"
source: """
encode_key_value!({"agent": {"name": "vector"}, "log": {"file": {"path": "my.log"}}, "event": "log"})
encode_key_value({"agent": {"name": "vector"}, "log": {"file": {"path": "my.log"}}, "event": "log"})
"""
return: #"agent.name=vector event=log log.file.path=my.log"#
},
Expand All @@ -72,7 +77,7 @@ remap: functions: encode_key_value: {
{
title: "Encode with custom delimiters (no ordering)"
source: """
encode_key_value!(
encode_key_value(
{"ts": "2021-06-05T17:20:00Z", "msg": "This is a message", "lvl": "info"},
field_delimiter: ",",
key_value_delimiter: ":"
Expand Down
9 changes: 7 additions & 2 deletions docs/reference/remap/functions/encode_logfmt.cue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ remap: functions: encode_logfmt: {
description: #"""
Encodes the `value` to [logfmt](\#(urls.logfmt)).
"""#
notices: [
"""
""",
]

arguments: [
{
Expand All @@ -29,7 +34,7 @@ remap: functions: encode_logfmt: {
{
title: "Encode to logfmt (no ordering)"
source: """
encode_logfmt!({"ts": "2021-06-05T17:20:00Z", "msg": "This is a message", "lvl": "info"})
encode_logfmt({"ts": "2021-06-05T17:20:00Z", "msg": "This is a message", "lvl": "info"})
"""
return: #"lvl=info msg="This is a message" ts=2021-06-05T17:20:00Z"#
},
Expand All @@ -43,7 +48,7 @@ remap: functions: encode_logfmt: {
{
title: "Encode to logfmt (nested fields)"
source: """
encode_logfmt!({"agent": {"name": "vector"}, "log": {"file": {"path": "my.log"}}, "event": "log"})
encode_logfmt({"agent": {"name": "vector"}, "log": {"file": {"path": "my.log"}}, "event": "log"})
"""
return: #"agent.name=vector event=log log.file.path=my.log"#
},
Expand Down
6 changes: 1 addition & 5 deletions docs/reference/remap/functions/parse_logfmt.cue
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ remap: functions: parse_logfmt: {
* Keys and values can be wrapped with `"`.
* `"` characters can be escaped by `\`.
"""#
notices: [
"""
All values are returned as strings, it is recommended to manually coerce values as you see fit.
""",
]
notices: functions.encode_key_value.notices

arguments: [
{
Expand Down
19 changes: 11 additions & 8 deletions lib/vrl/stdlib/src/encode_key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Function for EncodeKeyValue {
&[
Example {
title: "encode object",
source: r#"encode_key_value!({"lvl": "info", "msg": "This is a message", "log_id": 12345})"#,
source: r#"encode_key_value({"lvl": "info", "msg": "This is a message", "log_id": 12345})"#,
result: Ok(r#"s'log_id=12345 lvl=info msg="This is a message"'"#),
},
Example {
Expand All @@ -71,7 +71,7 @@ impl Function for EncodeKeyValue {
},
Example {
title: "custom delimiters",
source: r#"encode_key_value!({"start": "ool", "end": "kul", "stop1": "yyc", "stop2" : "gdx"}, key_value_delimiter: ":", field_delimiter: ",")"#,
source: r#"encode_key_value({"start": "ool", "end": "kul", "stop1": "yyc", "stop2" : "gdx"}, key_value_delimiter: ":", field_delimiter: ",")"#,
result: Ok(r#"s'end:kul,start:ool,stop1:yyc,stop2:gdx'"#),
},
]
Expand Down Expand Up @@ -121,7 +121,10 @@ impl Expression for EncodeKeyValueFn {
}

fn type_def(&self, _state: &state::Compiler) -> TypeDef {
TypeDef::new().bytes().fallible()
match &self.fields {
None => TypeDef::new().bytes().infallible(),
Some(_) => TypeDef::new().bytes().fallible(),
}
}
}

Expand Down Expand Up @@ -237,7 +240,7 @@ mod tests {
}
],
want: Ok("lvl=info"),
tdef: TypeDef::new().bytes().fallible(),
tdef: TypeDef::new().bytes().infallible(),
}

multiple_elements {
Expand All @@ -248,7 +251,7 @@ mod tests {
}
],
want: Ok("log_id=12345 lvl=info"),
tdef: TypeDef::new().bytes().fallible(),
tdef: TypeDef::new().bytes().infallible(),
}

string_with_spaces {
Expand All @@ -258,7 +261,7 @@ mod tests {
"msg" => "This is a log message"
}],
want: Ok(r#"lvl=info msg="This is a log message""#),
tdef: TypeDef::new().bytes().fallible(),
tdef: TypeDef::new().bytes().infallible(),
}

string_with_characters_to_escape {
Expand All @@ -270,7 +273,7 @@ mod tests {
"space key" => "foo"
}],
want: Ok(r#"another_field="some\\nfield\\and things" lvl=info msg="payload: {\"code\": 200}\\n" "space key"=foo"#),
tdef: TypeDef::new().bytes().fallible(),
tdef: TypeDef::new().bytes().infallible(),
}

nested_fields {
Expand All @@ -292,7 +295,7 @@ mod tests {
"event" => "log"
}],
want: Ok("agent.id=1234 agent.name=vector event=log log.file.path=encode_key_value.rs network.ip.0=127 network.ip.1=0 network.ip.2=0 network.ip.3=1 network.proto=tcp"),
tdef: TypeDef::new().bytes().fallible(),
tdef: TypeDef::new().bytes().infallible(),
}

fields_ordering {
Expand Down
2 changes: 1 addition & 1 deletion lib/vrl/stdlib/src/encode_logfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Function for EncodeLogfmt {
&[
Example {
title: "encode object",
source: r#"encode_logfmt!({"lvl": "info", "msg": "This is a message", "log_id": 12345})"#,
source: r#"encode_logfmt({"lvl": "info", "msg": "This is a message", "log_id": 12345})"#,
result: Ok(r#"s'log_id=12345 lvl=info msg="This is a message"'"#),
},
Example {
Expand Down

0 comments on commit 5628523

Please sign in to comment.