Skip to content

[pkg/stanza] TimeParser unmarshaling ignores original configuration #32169

Closed
@mx-psi

Description

@mx-psi

Component(s)

pkg/stanza

What happened?

Description

The TimeParser struct seems to ignore the original configuration if the struct was initialized with something other than the default values:

func (t *TimeParser) Unmarshal(component *confmap.Conf) error {
cfg := NewTimeParser()
err := component.Unmarshal(&cfg, confmap.WithIgnoreUnused())
if err != nil {
return err
}
*t = cfg
return nil
}

Steps to Reproduce

This test reproduces it

func TestUnmarshal(t *testing.T) {
	conf := confmap.NewFromStringMap(map[string]any{
		"location": "America/Shiprock",
	})
	tp := TimeParser{
		Layout: "1/2/2006 15:04:05",
	}

	require.NoError(t, tp.Unmarshal(conf))
	assert.Equal(t, "America/Shiprock", tp.Location) // ok
	assert.Equal(t, "strptime", tp.LayoutType)           // ok, although weird
	assert.Equal(t, "1/2/2006 15:04:05", tp.Layout)  // FAIL --> empty
}

Expected Result

All assertions pass

Actual Result

The last assertion fails

Collector version

Tested as of b25b78a

Environment information

No response

OpenTelemetry Collector configuration

No response

Log output

No response

Additional context

I think NewTimeParser should be part of the default configuration of time parser instead of this, and we should do the 'set to nil' trick from https://github.com/open-telemetry/opentelemetry-collector/blob/c72092e00151ce8e4cfef32c73b3a80d54076278/receiver/otlpreceiver/config.go#L68-L70 if we want to only set default config if the key exists.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions