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

fix(outputs.loki): Option to sanitize label names #15277

Merged
merged 1 commit into from
May 2, 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
5 changes: 5 additions & 0 deletions plugins/outputs/loki/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ to use them.
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"

## Sanitize Tag Names
## If true, all tag names will have invalid characters replaced with
## underscores that do not match the regex: ^[a-zA-Z_:][a-zA-Z0-9_:]*.
# sanitize_label_names = false

## Metric Name Label
## Label to use for the metric name to when sending metrics. If set to an
## empty string, this will not add the label. This is NOT suggested as there
Expand Down
42 changes: 29 additions & 13 deletions plugins/outputs/loki/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
Expand All @@ -34,18 +35,19 @@ const (
)

type Loki struct {
Domain string `toml:"domain"`
Endpoint string `toml:"endpoint"`
Timeout config.Duration `toml:"timeout"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
Headers map[string]string `toml:"http_headers"`
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
TokenURL string `toml:"token_url"`
Scopes []string `toml:"scopes"`
GZipRequest bool `toml:"gzip_request"`
MetricNameLabel string `toml:"metric_name_label"`
Domain string `toml:"domain"`
Endpoint string `toml:"endpoint"`
Timeout config.Duration `toml:"timeout"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
Headers map[string]string `toml:"http_headers"`
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
TokenURL string `toml:"token_url"`
Scopes []string `toml:"scopes"`
GZipRequest bool `toml:"gzip_request"`
MetricNameLabel string `toml:"metric_name_label"`
SanitizeLabelNames bool `toml:"sanitize_label_names"`

url string
client *http.Client
Expand Down Expand Up @@ -127,8 +129,13 @@ func (l *Loki) Write(metrics []telegraf.Metric) error {
}

tags := m.TagList()
var line string
if l.SanitizeLabelNames {
for _, t := range tags {
t.Key = sanitizeLabelName(t.Key)
}
}

var line string
for _, f := range m.FieldList() {
line += fmt.Sprintf("%s=\"%v\" ", f.Key, f.Value)
}
Expand Down Expand Up @@ -200,6 +207,15 @@ func (l *Loki) writeMetrics(s Streams) error {
return nil
}

// Verify the label name matches the regex [a-zA-Z_:][a-zA-Z0-9_:]*
func sanitizeLabelName(name string) string {
re := regexp.MustCompile(`^[^a-zA-Z_:]`)
result := re.ReplaceAllString(name, "_")

re = regexp.MustCompile(`[^a-zA-Z0-9_:]`)
return re.ReplaceAllString(result, "_")
}

func init() {
outputs.Add("loki", func() telegraf.Output {
return &Loki{
Expand Down
35 changes: 35 additions & 0 deletions plugins/outputs/loki/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,38 @@ func TestMetricSorting(t *testing.T) {
require.NoError(t, err)
})
}

func TestSanitizeLabelName(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "no change",
input: "foobar",
expected: "foobar",
},
{
name: "replace invalid first charachter",
input: "3foobar",
expected: "_foobar",
},
{
name: "replace invalid later charachter",
input: "foobar.foobar",
expected: "foobar_foobar",
},
{
name: "numbers allowed later",
input: "foobar.foobar.2002",
expected: "foobar_foobar_2002",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, sanitizeLabelName(tt.input))
})
}
}
5 changes: 5 additions & 0 deletions plugins/outputs/loki/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"

## Sanitize Tag Names
## If true, all tag names will have invalid characters replaced with
## underscores that do not match the regex: ^[a-zA-Z_:][a-zA-Z0-9_:]*.
# sanitize_label_names = false

## Metric Name Label
## Label to use for the metric name to when sending metrics. If set to an
## empty string, this will not add the label. This is NOT suggested as there
Expand Down
Loading