OTLPConfig.UnmarshalYAML: Return error on invalid input

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2024-07-18 10:40:47 +02:00
parent ad0a30cdd8
commit 932918cd3f
4 changed files with 30 additions and 18 deletions

View file

@ -19,7 +19,6 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -1324,17 +1323,20 @@ func (c *OTLPConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
} }
seen := map[string]struct{}{} seen := map[string]struct{}{}
i := 0 var err error
for i < len(c.PromoteResourceAttributes) { for i, attr := range c.PromoteResourceAttributes {
s := strings.TrimSpace(c.PromoteResourceAttributes[i]) attr = strings.TrimSpace(attr)
if _, exists := seen[s]; exists { if attr == "" {
c.PromoteResourceAttributes = slices.Delete(c.PromoteResourceAttributes, i, i+1) err = errors.Join(err, fmt.Errorf("empty promoted OTel resource attribute"))
continue
}
if _, exists := seen[attr]; exists {
err = errors.Join(err, fmt.Errorf("duplicated promoted OTel resource attribute %q", attr))
continue continue
} }
seen[s] = struct{}{} seen[attr] = struct{}{}
c.PromoteResourceAttributes[i] = s c.PromoteResourceAttributes[i] = attr
i++
} }
return nil return err
} }

View file

@ -1478,15 +1478,23 @@ func TestRemoteWriteRetryOnRateLimit(t *testing.T) {
} }
func TestOTLPSanitizeResourceAttributes(t *testing.T) { func TestOTLPSanitizeResourceAttributes(t *testing.T) {
want, err := LoadFile(filepath.Join("testdata", "otlp_sanitize_resource_attributes.good.yml"), false, false, log.NewNopLogger()) t.Run("good config", func(t *testing.T) {
require.NoError(t, err) want, err := LoadFile(filepath.Join("testdata", "otlp_sanitize_resource_attributes.good.yml"), false, false, log.NewNopLogger())
require.NoError(t, err)
out, err := yaml.Marshal(want) out, err := yaml.Marshal(want)
require.NoError(t, err) require.NoError(t, err)
var got Config var got Config
require.NoError(t, yaml.UnmarshalStrict(out, &got)) require.NoError(t, yaml.UnmarshalStrict(out, &got))
require.Equal(t, []string{"k8s.cluster.name", "k8s.job.name", "k8s.namespace.name"}, got.OTLPConfig.PromoteResourceAttributes) require.Equal(t, []string{"k8s.cluster.name", "k8s.job.name", "k8s.namespace.name"}, got.OTLPConfig.PromoteResourceAttributes)
})
t.Run("bad config", func(t *testing.T) {
_, err := LoadFile(filepath.Join("testdata", "otlp_sanitize_resource_attributes.bad.yml"), false, false, log.NewNopLogger())
require.ErrorContains(t, err, `duplicated promoted OTel resource attribute "k8s.job.name"`)
require.ErrorContains(t, err, `empty promoted OTel resource attribute`)
})
} }
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {

View file

@ -0,0 +1,2 @@
otlp:
promote_resource_attributes: ["k8s.cluster.name", " k8s.job.name ", "k8s.namespace.name", "k8s.job.name", ""]

View file

@ -1,2 +1,2 @@
otlp: otlp:
promote_resource_attributes: ["k8s.cluster.name", "k8s.job.name", "k8s.namespace.name", " k8s.job.name "] promote_resource_attributes: ["k8s.cluster.name", " k8s.job.name ", "k8s.namespace.name"]