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 000000000..ad1b39ad4 Binary files /dev/null and b/plugins/parsers/xpath/testcases/cbor_hex_encoding_explicit/data.bin differ 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" +