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

[confmap] Fix for unset env var setting non-string field. #10950

Merged
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
25 changes: 25 additions & 0 deletions .chloggen/confmap-fix-non-string-nil-case.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: Fix bug where an unset env var used with a non-string field resulted in a panic

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

# (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: []
15 changes: 11 additions & 4 deletions confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@
return a == b
}

func castTo(exp expandedValue, useOriginal bool) (any, error) {
func castTo(exp expandedValue, useOriginal bool) any {
// If the target field is a string, use `exp.Original` or fail if not available.
if useOriginal {
return exp.Original, nil
return exp.Original
}
// Otherwise, use the parsed value (previous behavior).
return exp.Value, nil
return exp.Value
}

// Check if a reflect.Type is of the form T, where:
Expand Down Expand Up @@ -284,7 +284,14 @@
to reflect.Type,
data any) (any, error) {
if exp, ok := data.(expandedValue); ok {
return castTo(exp, to.Kind() == reflect.String)
v := castTo(exp, to.Kind() == reflect.String)
// See https://github.com/open-telemetry/opentelemetry-collector/issues/10949
// If the `to.Kind` is not a string, then expandValue's original value is useless and
// the casted-to value will be nil. In that scenario, we need to use the default value of `to`'s kind.
if v == nil {
return reflect.Zero(to).Interface(), nil

Check warning on line 292 in confmap/confmap.go

View check run for this annotation

Codecov / codecov/patch

confmap/confmap.go#L292

Added line #L292 was not covered by tests
}
return v, nil
}

switch to.Kind() {
Expand Down
11 changes: 11 additions & 0 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,14 @@ func TestIssue10937_ComplexType(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []map[string][]any{{"key": {1234}}}, cfgNotStringy.Field)
}

func TestIssue10949_UnsetVar(t *testing.T) {
resolver := NewResolver(t, "types_expand.yaml")
conf, err := resolver.Resolve(context.Background())
require.NoError(t, err)

var cfg TargetConfig[int]
err = conf.Unmarshal(&cfg)
require.NoError(t, err)
require.Equal(t, 0, cfg.Field)
}
Loading