Skip to content

Commit

Permalink
feat(parsers.value): Add base64 datatype (#15697)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Aug 7, 2024
1 parent 1c36e19 commit 7822bf1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/parsers/value/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions plugins/parsers/value/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package value

import (
"bytes"
"encoding/base64"
"fmt"
"strconv"
"strings"
Expand All @@ -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":
Expand Down Expand Up @@ -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":
Expand Down
12 changes: 12 additions & 0 deletions plugins/parsers/value/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 7822bf1

Please sign in to comment.