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] Add error hint when invalid YAML was passed #12180

Merged
merged 4 commits into from
Feb 25, 2025
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
26 changes: 26 additions & 0 deletions .chloggen/mx-psi_error-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use this changelog template to create an entry for release notes.

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

# 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: Surface YAML parsing errors when they happen at the top-level.

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

# (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: |
This adds context to some instances of the error "retrieved value (type=string) cannot be used as a Conf", which typically happens because of invalid YAML documents

# 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: []
17 changes: 16 additions & 1 deletion confmap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ type ChangeEvent struct {
// Retrieved holds the result of a call to the Retrieve method of a Provider object.
type Retrieved struct {
rawConf any
errorHint error
closeFunc CloseFunc

stringRepresentation string
isSetString bool
}

type retrievedSettings struct {
errorHint error
stringRepresentation string
isSetString bool
closeFunc CloseFunc
Expand Down Expand Up @@ -138,6 +140,12 @@ func withStringRepresentation(stringRepresentation string) RetrievedOption {
})
}

func withErrorHint(errorHint error) RetrievedOption {
return retrievedOptionFunc(func(settings *retrievedSettings) {
settings.errorHint = errorHint
})
}

// NewRetrievedFromYAML returns a new Retrieved instance that contains the deserialized data from the yaml bytes.
// * yamlBytes the yaml bytes that will be deserialized.
// * opts specifies options associated with this Retrieved value, such as CloseFunc.
Expand All @@ -146,7 +154,10 @@ func NewRetrievedFromYAML(yamlBytes []byte, opts ...RetrievedOption) (*Retrieved
if err := yaml.Unmarshal(yamlBytes, &rawConf); err != nil {
// If the string is not valid YAML, we try to use it verbatim as a string.
strRep := string(yamlBytes)
return NewRetrieved(strRep, append(opts, withStringRepresentation(strRep))...)
return NewRetrieved(strRep, append(opts,
withStringRepresentation(strRep),
withErrorHint(fmt.Errorf("assuming string type since contents are not valid YAML: %w", err)),
)...)
}

switch rawConf.(type) {
Expand Down Expand Up @@ -175,6 +186,7 @@ func NewRetrieved(rawConf any, opts ...RetrievedOption) (*Retrieved, error) {
}
return &Retrieved{
rawConf: rawConf,
errorHint: set.errorHint,
closeFunc: set.closeFunc,
stringRepresentation: set.stringRepresentation,
isSetString: set.isSetString,
Expand All @@ -188,6 +200,9 @@ func (r *Retrieved) AsConf() (*Conf, error) {
}
val, ok := r.rawConf.(map[string]any)
if !ok {
if r.errorHint != nil {
return nil, fmt.Errorf("retrieved value (type=%T) cannot be used as a Conf: %w", r.rawConf, r.errorHint)
}
return nil, fmt.Errorf("retrieved value (type=%T) cannot be used as a Conf", r.rawConf)
}
return NewFromStringMap(val), nil
Expand Down
6 changes: 4 additions & 2 deletions confmap/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func TestNewRetrievedFromYAMLInvalidYAMLBytes(t *testing.T) {
require.NoError(t, err)

_, err = ret.AsConf()
require.Error(t, err)
require.EqualError(t, err,
"retrieved value (type=string) cannot be used as a Conf: assuming string type since contents are not valid YAML: yaml: line 1: did not find expected node content",
)

str, err := ret.AsString()
require.NoError(t, err)
Expand All @@ -150,7 +152,7 @@ func TestNewRetrievedFromYAMLInvalidAsMap(t *testing.T) {
require.NoError(t, err)

_, err = ret.AsConf()
require.Error(t, err)
require.EqualError(t, err, "retrieved value (type=string) cannot be used as a Conf")

str, err := ret.AsString()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion otelcol/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func TestCollectorStartInvalidConfig(t *testing.T) {
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid.yaml")}),
})
require.NoError(t, err)
assert.Error(t, col.Run(context.Background()))
assert.EqualError(t, col.Run(context.Background()), "invalid configuration: service::pipelines::traces: references processor \"invalid\" which is not configured")
}

func TestNewCollectorInvalidConfigProviderSettings(t *testing.T) {
Expand Down
Loading