feat(parsers.value): Add base64 datatype (#15697)

This commit is contained in:
Joshua Powers 2024-08-07 04:26:46 -06:00 committed by GitHub
parent 1c36e1977e
commit 7822bf1af3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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":

View File

@ -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",