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

This commit is contained in:
Joshua Powers 2024-05-02 12:45:46 -06:00 committed by GitHub
parent 07b99156b4
commit 60cf9772a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 74 additions and 13 deletions

View File

@ -53,6 +53,11 @@ to use them.
# tls_cert = "/etc/telegraf/cert.pem" # tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.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 ## Metric Name Label
## Label to use for the metric name to when sending metrics. If set to an ## 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 ## empty string, this will not add the label. This is NOT suggested as there

View File

@ -10,6 +10,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -34,18 +35,19 @@ const (
) )
type Loki struct { type Loki struct {
Domain string `toml:"domain"` Domain string `toml:"domain"`
Endpoint string `toml:"endpoint"` Endpoint string `toml:"endpoint"`
Timeout config.Duration `toml:"timeout"` Timeout config.Duration `toml:"timeout"`
Username config.Secret `toml:"username"` Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"` Password config.Secret `toml:"password"`
Headers map[string]string `toml:"http_headers"` Headers map[string]string `toml:"http_headers"`
ClientID string `toml:"client_id"` ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"` ClientSecret string `toml:"client_secret"`
TokenURL string `toml:"token_url"` TokenURL string `toml:"token_url"`
Scopes []string `toml:"scopes"` Scopes []string `toml:"scopes"`
GZipRequest bool `toml:"gzip_request"` GZipRequest bool `toml:"gzip_request"`
MetricNameLabel string `toml:"metric_name_label"` MetricNameLabel string `toml:"metric_name_label"`
SanitizeLabelNames bool `toml:"sanitize_label_names"`
url string url string
client *http.Client client *http.Client
@ -127,8 +129,13 @@ func (l *Loki) Write(metrics []telegraf.Metric) error {
} }
tags := m.TagList() 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() { for _, f := range m.FieldList() {
line += fmt.Sprintf("%s=\"%v\" ", f.Key, f.Value) line += fmt.Sprintf("%s=\"%v\" ", f.Key, f.Value)
} }
@ -200,6 +207,15 @@ func (l *Loki) writeMetrics(s Streams) error {
return nil 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() { func init() {
outputs.Add("loki", func() telegraf.Output { outputs.Add("loki", func() telegraf.Output {
return &Loki{ return &Loki{

View File

@ -469,3 +469,38 @@ func TestMetricSorting(t *testing.T) {
require.NoError(t, err) 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))
})
}
}

View File

@ -24,6 +24,11 @@
# tls_cert = "/etc/telegraf/cert.pem" # tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.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 ## Metric Name Label
## Label to use for the metric name to when sending metrics. If set to an ## 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 ## empty string, this will not add the label. This is NOT suggested as there