Skip to content

[discovery] Don't use component.MustNewIDWithName #4565

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

Merged
merged 1 commit into from
Mar 31, 2024
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
19 changes: 12 additions & 7 deletions internal/confmapprovider/discovery/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (d *discoverer) createDiscoveryReceiversAndObservers(cfg *Config) (map[comp
if lr, err = discoveryReceiverFactory.CreateLogsReceiver(context.Background(), discoveryReceiverSettings, discoveryReceiverDefaultConfig, d); err != nil {
return nil, nil, fmt.Errorf("failed creating discovery receiver: %w", err)
}
discoveryReceivers[component.MustNewIDWithName(discoveryReceiverFactory.Type().String(), observerID.String())] = lr
discoveryReceivers[component.NewIDWithName(discoveryReceiverFactory.Type(), observerID.String())] = lr
}

return discoveryReceivers, discoveryObservers, nil
Expand Down Expand Up @@ -654,10 +654,11 @@ func (d *discoverer) ConsumeLogs(_ context.Context, ld plog.Logs) error {
rlogs := ld.ResourceLogs()
for i := 0; i < rlogs.Len(); i++ {
var (
receiverType, receiverName string
receiverConfig, obsID string
observerID component.ID
err error
receiverType component.Type
receiverName string
receiverConfig, obsID string
observerID component.ID
err error
)
rlog := rlogs.At(i)
rAttrs := rlog.Resource().Attributes()
Expand All @@ -669,7 +670,11 @@ func (d *discoverer) ConsumeLogs(_ context.Context, ld plog.Logs) error {
// nothing we can do without this one
continue
}
receiverType = rType.Str()
receiverType, err = component.NewType(rType.Str())
if err != nil {
d.logger.Debug("invalid receiver type", zap.Error(err))
continue
}
if rConfig, ok := rAttrs.Get(discovery.ReceiverConfigAttr); ok {
receiverConfig = rConfig.Str()
}
Expand All @@ -695,7 +700,7 @@ func (d *discoverer) ConsumeLogs(_ context.Context, ld plog.Logs) error {

var rule string
var configSection map[string]any
receiverID := component.MustNewIDWithName(receiverType, receiverName)
receiverID := component.NewIDWithName(receiverType, receiverName)
if rCfg, hasConfig := d.getUnexpandedReceiverConfig(receiverID, observerID); hasConfig {
if r, hasRule := rCfg["rule"]; hasRule {
rule = r.(string)
Expand Down
6 changes: 5 additions & 1 deletion internal/confmapprovider/discovery/properties/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ func NewProperty(property, val string) (*Property, error) {
}
subStringMap = map[string]any{"config": confmap.NewFromStringMap(dst).ToStringMap()}
}
receiverType, err := component.NewType(p.Component.Type)
if err != nil {
return nil, fmt.Errorf("invalid receiver type %q: %w", p.Component.Type, err)
}
p.stringMap = map[string]any{
p.ComponentType: map[string]any{
component.MustNewIDWithName(p.Component.Type, p.Component.Name).String(): subStringMap,
component.NewIDWithName(receiverType, p.Component.Name).String(): subStringMap,
},
}
p.Input = property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func TestInvalidProperties(t *testing.T) {
{property: "splunk.discovery.invalid", expectedError: "invalid property \"splunk.discovery.invalid\" (parsing error): splunk.discovery:1:18: unexpected token \"invalid\" (expected (\"receivers\" | \"extensions\") <dot> ComponentID <dot> ((\"config\" <dot>) | \"enabled\") (<string> | <dot> | <forwardslash>)*)"},
{property: "splunk.discovery.extensions.config.one.two", expectedError: "invalid property \"splunk.discovery.extensions.config.one.two\" (parsing error): splunk.discovery:1:43: unexpected token \"<EOF>\" (expected <dot> ((\"config\" <dot>) | \"enabled\") (<string> | <dot> | <forwardslash>)*)"},
{property: "splunk.discovery.receivers.type/name.config", expectedError: "invalid property \"splunk.discovery.receivers.type/name.config\" (parsing error): splunk.discovery:1:44: unexpected token \"<EOF>\" (expected <dot>)"},
{property: "splunk.discovery.extensions.extension--0-1-with-config-in-type-_x64__x86_🙈🙉🙊4:000x0;;0;;0;;-___-----type/e/x/t/e%ns<i>o<=n=>nam/e-with-config.config.o::n::e.config", expectedError: "invalid receiver type \"extension--0-1-with-config-in-type-_x64__x86_🙈🙉🙊4:000x0;;0;;0;;-___-----type\": invalid character(s) in type \"extension--0-1-with-config-in-type-_x64__x86_🙈🙉🙊4:000x0;;0;;0;;-___-----type\""},
} {
t.Run(tt.property, func(t *testing.T) {
p, err := NewProperty(tt.property, "val")
Expand All @@ -229,13 +230,4 @@ func TestInvalidProperties(t *testing.T) {
require.Nil(t, p)
})
}
for _, tt := range []struct {
property, expectedError string
}{
{property: "splunk.discovery.extensions.extension--0-1-with-config-in-type-_x64__x86_🙈🙉🙊4:000x0;;0;;0;;-___-----type/e/x/t/e%ns<i>o<=n=>nam/e-with-config.config.o::n::e.config", expectedError: `invalid character(s) in type "extension--0-1-with-config-in-type-_x64__x86_🙈🙉🙊4:000x0;;0;;0;;-___-----type"`},
} {
t.Run(tt.property, func(t *testing.T) {
require.PanicsWithError(t, tt.expectedError, func() { NewProperty(tt.property, "val") })
})
}
}