diff --git a/plugins/parsers/value/README.md b/plugins/parsers/value/README.md index f8fb93608..43ee93062 100644 --- a/plugins/parsers/value/README.md +++ b/plugins/parsers/value/README.md @@ -41,6 +41,7 @@ You **must** tell Telegraf what type of metric to collect by using the will treat integers as floating-point values and produces an error on data that cannot be converted (e.g. strings). - `string`: outputs the data as a string. +- `base64`: outputs the data as a base64 encoded string. - `boolean`: converts the received data to a boolean value. This setting will produce an error on any data except for `true` and `false` strings. - `auto_integer`: converts the received data to an integer value if possible and diff --git a/plugins/parsers/value/parser.go b/plugins/parsers/value/parser.go index 2648e778c..2ac053c08 100644 --- a/plugins/parsers/value/parser.go +++ b/plugins/parsers/value/parser.go @@ -2,6 +2,7 @@ package value import ( "bytes" + "encoding/base64" "fmt" "strconv" "strings" @@ -27,6 +28,8 @@ func (v *Parser) Init() error { v.DataType = "float" case "str", "string": v.DataType = "string" + case "base64": + v.DataType = "base64" case "bool", "boolean": v.DataType = "bool" case "auto_integer", "auto_float": @@ -64,6 +67,8 @@ func (v *Parser) Parse(buf []byte) ([]telegraf.Metric, error) { value, err = strconv.ParseFloat(vStr, 64) case "string": value = vStr + case "base64": + value = base64.StdEncoding.EncodeToString(buf) case "bool": value, err = strconv.ParseBool(vStr) case "auto_integer": diff --git a/plugins/parsers/value/parser_test.go b/plugins/parsers/value/parser_test.go index 389d0debc..649a14a9d 100644 --- a/plugins/parsers/value/parser_test.go +++ b/plugins/parsers/value/parser_test.go @@ -35,6 +35,12 @@ func TestParseValidValues(t *testing.T) { input: []byte("foobar"), expected: "foobar", }, + { + name: "base64", + dtype: "base64", + input: []byte("foobar"), + expected: "Zm9vYmFy", + }, { name: "boolean", dtype: "boolean", @@ -132,6 +138,12 @@ func TestParseLineValidValues(t *testing.T) { input: "foobar", expected: "foobar", }, + { + name: "base64", + dtype: "base64", + input: "foobar", + expected: "Zm9vYmFy", + }, { name: "boolean", dtype: "boolean",