From 57bcdaf695bb2a5327be271d73030a80ad188da4 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Thu, 6 Jul 2023 16:16:35 +0200 Subject: [PATCH] fix(parsers.xpath): Handle explicitly defined fields correctly (#13550) --- plugins/parsers/xpath/parser.go | 7 +++++ .../cbor_hex_encoding_explicit/data.bin | Bin 0 -> 83 bytes .../cbor_hex_encoding_explicit/expected.out | 1 + .../cbor_hex_encoding_explicit/telegraf.conf | 29 ++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/data.bin create mode 100644 plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/expected.out create mode 100644 plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/telegraf.conf diff --git a/plugins/parsers/xpath/parser.go b/plugins/parsers/xpath/parser.go index 0ccf76653..0dd3f186d 100644 --- a/plugins/parsers/xpath/parser.go +++ b/plugins/parsers/xpath/parser.go @@ -397,6 +397,13 @@ func (p *Parser) parseQuery(starttime time.Time, doc, selected dataNode, config if err != nil { return nil, fmt.Errorf("failed to query field %q: %w", name, err) } + + if config.FieldsHexFilter != nil && config.FieldsHexFilter.Match(name) { + if b, ok := v.([]byte); ok { + v = hex.EncodeToString(b) + } + } + fields[name] = v } diff --git a/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/data.bin b/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/data.bin new file mode 100644 index 0000000000000000000000000000000000000000..ad1b39ad4be2083f98ea3dce761aff8662c0cada GIT binary patch literal 83 zcmZo%np#{^6rboA(P#`|hgMpEWnT3^&9V!5m T&n(Hz1!_plEs#o?HT4ky2+$tV literal 0 HcmV?d00001 diff --git a/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/expected.out b/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/expected.out new file mode 100644 index 000000000..fa6cc2f3e --- /dev/null +++ b/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/expected.out @@ -0,0 +1 @@ +data fieldc="this is a test",fielda="0001020304050607",fieldb="666f6f626172" 1687852514000000000 \ No newline at end of file diff --git a/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/telegraf.conf b/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/telegraf.conf new file mode 100644 index 000000000..e03241832 --- /dev/null +++ b/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/telegraf.conf @@ -0,0 +1,29 @@ +# Example data: +# [ +# { +# "str_a": bytearray("this is a test"), +# "str_b": bytearray("foobar"), +# "bytes_a": bytearray([0, 1, 2, 3, 4, 5, 6, 7]), +# "bytes_b": bytearray("foobar"), +# "timestamp": 1687852514 +# } +# ] + +[[inputs.file]] + files = ["./testcases/cbor_hex_encoding/data.bin"] + data_format = "xpath_cbor" + + xpath_native_types = true + + [[inputs.file.xpath]] + metric_name = "'data'" + metric_selection = "child::*" + timestamp = "timestamp" + timestamp_format = "unix" + fields_bytes_as_hex = ["fielda", "fieldb"] + + [inputs.file.xpath.fields] + fielda = "bytes_a" + fieldb = "bytes_b" + fieldc = "str_a" +