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

[chore] reuse the timer object when parsing #31802

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/stanza/operator/helper/time.go
Expand Up @@ -31,7 +31,7 @@ const NativeKey = "native" // provided for operator development
// NewTimeParser creates a new time parser with default values
func NewTimeParser() TimeParser {
return TimeParser{
LayoutType: "strptime",
LayoutType: StrptimeKey,
}
}

Expand All @@ -47,12 +47,13 @@ type TimeParser struct {

// Unmarshal starting from default settings
func (t *TimeParser) Unmarshal(component *confmap.Conf) error {
cfg := NewTimeParser()
err := component.Unmarshal(&cfg, confmap.WithIgnoreUnused())
err := component.Unmarshal(t, confmap.WithIgnoreUnused())
atoulme marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
*t = cfg
if t.LayoutType == "" {
t.LayoutType = StrptimeKey
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to set default values in the unmarshaller? If we need this one then why not others? Maybe this is just a matter of test cases creating the default config consistently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code was doing this previously in the NewTimeParser path. Instead of creating a new struct, I just set the default here now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why though. The pattern we use elsewhere is to instantiate a default struct and then unmarshal into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, that's the only instance this occurs afaict? By patching this code open-telemetry/opentelemetry-collector#9750 works well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a well established pattern throughout the repo to create a default config and then unmarshal into them. For pkg/stanza operators, it is somewhat cryptic but is handled here where we've basically looked up a factory for the specific operator and asked it for a new (default) config. I don't think we should be handling default values here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, in that case, since the default value is set earlier, we can safely remove the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like that didn't work: the registry works well to set default values, but because TimeParser is optional on helper.ParserConfig, it is nil by default, and since it's not initialized with default values, they don't get applied later on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atoulme Can't we do the trick from https://github.com/open-telemetry/opentelemetry-collector/blob/c72092e00151ce8e4cfef32c73b3a80d54076278/receiver/otlpreceiver/config.go#L68-L70 ? That is, set the configuration, but remove it during unmarshaling if the key is not set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, that's the right fix. I will try to get to it.

return nil
}

Expand Down