Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading