Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_map-types-support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confmap

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Use string representation for field types where all primitive types are strings.

# One or more tracking issues or pull requests related to the change
issues: [10937]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
43 changes: 38 additions & 5 deletions confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,36 @@ func (l *Conf) unsanitizedGet(key string) any {
return l.k.Get(key)
}

// sanitize recursively removes expandedValue references from the given data.
// It uses the expandedValue.Value field to replace the expandedValue references.
func sanitize(a any) any {
return sanitizeExpanded(a, false)
}

// sanitizeToStringMap recursively removes expandedValue references from the given data.
// It uses the expandedValue.Original field to replace the expandedValue references.
func sanitizeToStr(a any) any {
return sanitizeExpanded(a, true)
}

func sanitizeExpanded(a any, useOriginal bool) any {
switch m := a.(type) {
case map[string]any:
c := maps.Copy(m)
for k, v := range m {
c[k] = sanitize(v)
c[k] = sanitizeExpanded(v, useOriginal)
}
return c
case []any:
var newSlice []any
for _, e := range m {
newSlice = append(newSlice, sanitize(e))
newSlice = append(newSlice, sanitizeExpanded(e, useOriginal))
}
return newSlice
case expandedValue:
if useOriginal {
return m.Original
}
return m.Value
}
return a
Expand All @@ -134,7 +149,7 @@ func sanitize(a any) any {
// Get can retrieve any value given the key to use.
func (l *Conf) Get(key string) any {
val := l.unsanitizedGet(key)
return sanitize(val)
return sanitizeExpanded(val, false)
}

// IsSet checks to see if the key has been set in any of the data locations.
Expand Down Expand Up @@ -244,6 +259,21 @@ func castTo(exp expandedValue, useOriginal bool) (any, error) {
return exp.Value, nil
}

// Check if a reflect.Type is of the form T:
// T = string | map[string]T | []T | [n]T
func isStringyStructure(t reflect.Type) bool {
if t.Kind() == reflect.String {
return true
}
if t.Kind() == reflect.Map {
return t.Key().Kind() == reflect.String && isStringyStructure(t.Elem())
}
if t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
return isStringyStructure(t.Elem())
}
return false
}

// When a value has been loaded from an external source via a provider, we keep both the
// parsed value and the original string value. This allows us to expand the value to its
// original string representation when decoding into a string field, and use the original otherwise.
Expand All @@ -256,10 +286,13 @@ func useExpandValue() mapstructure.DecodeHookFuncType {
return castTo(exp, to.Kind() == reflect.String)
}

// If the target field is a map or slice, sanitize input to remove expandedValue references.
switch to.Kind() {
case reflect.Array, reflect.Slice, reflect.Map:
// This does not handle map[string]string and []string explicitly.
if isStringyStructure(to) {
// If the target field is a stringy structure, sanitize to use the original string value everywhere.
return sanitizeToStr(data), nil
}
// Otherwise, sanitize to use the parsed value everywhere.
return sanitize(data), nil
}
return data, nil
Expand Down
55 changes: 55 additions & 0 deletions confmap/confmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -876,3 +877,57 @@ func TestExpandedValue(t *testing.T) {
cfgBool := ConfigBool{}
assert.Error(t, cm.Unmarshal(&cfgBool))
}

func TestStringyTypes(t *testing.T) {
tests := []struct {
valueOfType any
isStringy bool
}{
{
valueOfType: "string",
isStringy: true,
},
{
valueOfType: 1,
isStringy: false,
},
{
valueOfType: map[string]any{},
isStringy: false,
},
{
valueOfType: []any{},
isStringy: false,
},
{
valueOfType: map[string]string{},
isStringy: true,
},
{
valueOfType: []string{},
isStringy: true,
},
{
valueOfType: map[string][]string{},
isStringy: true,
},
{
valueOfType: map[string]map[string]string{},
isStringy: true,
},
{
valueOfType: []map[string]any{},
isStringy: false,
},
{
valueOfType: []map[string]string{},
isStringy: true,
},
}

for _, tt := range tests {
// Create a reflect.Type from the value
to := reflect.TypeOf(tt.valueOfType)
assert.Equal(t, tt.isStringy, isStringyStructure(to))
}
}
3 changes: 3 additions & 0 deletions confmap/internal/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.0

require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/config/configopaque v1.13.0
go.opentelemetry.io/collector/confmap v0.107.0
go.opentelemetry.io/collector/confmap/provider/envprovider v0.107.0
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.107.0
Expand All @@ -28,3 +29,5 @@ replace go.opentelemetry.io/collector/confmap => ../../
replace go.opentelemetry.io/collector/confmap/provider/fileprovider => ../../provider/fileprovider

replace go.opentelemetry.io/collector/confmap/provider/envprovider => ../../provider/envprovider

replace go.opentelemetry.io/collector/config/configopaque => ../../../config/configopaque
1 change: 1 addition & 0 deletions confmap/internal/e2e/testdata/types_complex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
field: [key: ["${env:VALUE}"]]
2 changes: 2 additions & 0 deletions confmap/internal/e2e/testdata/types_map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
field:
key: ${env:VALUE}
1 change: 1 addition & 0 deletions confmap/internal/e2e/testdata/types_slice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
field: ["${env:VALUE}"]
60 changes: 60 additions & 0 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/provider/envprovider"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
Expand Down Expand Up @@ -585,3 +586,62 @@ func TestIndirectSliceEnvVar(t *testing.T) {
assert.Equal(t, collectorConf.Service.Pipelines.Logs.Receivers, []string{"nop", "otlp"})
assert.Equal(t, collectorConf.Service.Pipelines.Logs.Exporters, []string{"otlp", "nop"})
}

func TestIssue10937_MapType(t *testing.T) {
t.Setenv("VALUE", "1234")

resolver := NewResolver(t, "types_map.yaml")
conf, err := resolver.Resolve(context.Background())
require.NoError(t, err)

var cfg TargetConfig[map[string]configopaque.String]
err = conf.Unmarshal(&cfg)
require.NoError(t, err)
require.Equal(t, map[string]configopaque.String{"key": "1234"}, cfg.Field)
}

func TestIssue10937_ArrayType(t *testing.T) {
t.Setenv("VALUE", "1234")

resolver := NewResolver(t, "types_slice.yaml")
conf, err := resolver.Resolve(context.Background())
require.NoError(t, err)

var cfgStrSlice TargetConfig[[]string]
err = conf.Unmarshal(&cfgStrSlice)
require.NoError(t, err)
require.Equal(t, []string{"1234"}, cfgStrSlice.Field)

var cfgStrArray TargetConfig[[1]string]
err = conf.Unmarshal(&cfgStrArray)
require.NoError(t, err)
require.Equal(t, [1]string{"1234"}, cfgStrArray.Field)

var cfgAnySlice TargetConfig[[]any]
err = conf.Unmarshal(&cfgAnySlice)
require.NoError(t, err)
require.Equal(t, []any{1234}, cfgAnySlice.Field)

var cfgAnyArray TargetConfig[[1]any]
err = conf.Unmarshal(&cfgAnyArray)
require.NoError(t, err)
require.Equal(t, [1]any{1234}, cfgAnyArray.Field)
}

func TestIssue10937_ComplexType(t *testing.T) {
t.Setenv("VALUE", "1234")

resolver := NewResolver(t, "types_complex.yaml")
conf, err := resolver.Resolve(context.Background())
require.NoError(t, err)

var cfgStringy TargetConfig[[]map[string][]string]
err = conf.Unmarshal(&cfgStringy)
require.NoError(t, err)
require.Equal(t, []map[string][]string{{"key": {"1234"}}}, cfgStringy.Field)

var cfgNotStringy TargetConfig[[]map[string][]any]
err = conf.Unmarshal(&cfgNotStringy)
require.NoError(t, err)
require.Equal(t, []map[string][]any{{"key": {1234}}}, cfgNotStringy.Field)
}