Skip to content

Commit

Permalink
don't panic loading twice
Browse files Browse the repository at this point in the history
  • Loading branch information
leehinman committed Dec 3, 2024
1 parent 8e20316 commit 2ac0b3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Add `translate_ldap_attribute` processor. {pull}41472[41472]
- Remove unnecessary debug logs during idle connection teardown {issue}40824[40824]
- Fix incorrect cloud provider identification in add_cloud_metadata processor using provider priority mechanism {pull}41636[41636]
- Prevent panic if libbeat processors are loaded more than once. {issue}41475[41475] {pull}41857[51857]

*Auditbeat*

Expand Down
11 changes: 8 additions & 3 deletions libbeat/processors/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type pluginer interface {
Plugin() Constructor
}

var (
ErrExistsAlready error = errors.New("exists already")
ErrPluginAlreadyRegistered error = errors.New("non-namespace plugin already registered")
)

func NewNamespace() *Namespace {
return &Namespace{
reg: map[string]pluginer{},
Expand All @@ -47,7 +52,7 @@ func NewNamespace() *Namespace {
func (ns *Namespace) Register(name string, factory Constructor) error {
p := plugin{NewConditional(factory)}
names := strings.Split(name, ".")
if err := ns.add(names, p); err != nil {
if err := ns.add(names, p); err != nil && !errors.Is(err, ErrExistsAlready) && !errors.Is(err, ErrPluginAlreadyRegistered) {
return fmt.Errorf("plugin %s registration fail %w", name, err)
}
return nil
Expand All @@ -59,7 +64,7 @@ func (ns *Namespace) add(names []string, p pluginer) error {
// register plugin if intermediate node in path being processed
if len(names) == 1 {
if _, found := ns.reg[name]; found {
return fmt.Errorf("%v exists already", name)
return fmt.Errorf("%s %w", name, ErrExistsAlready)
}

ns.reg[name] = p
Expand All @@ -71,7 +76,7 @@ func (ns *Namespace) add(names []string, p pluginer) error {
if found {
ns, ok := tmp.(*Namespace)
if !ok {
return errors.New("non-namespace plugin already registered")
return ErrPluginAlreadyRegistered
}
return ns.add(names[1:], p)
}
Expand Down
2 changes: 1 addition & 1 deletion libbeat/processors/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestNamespaceRegisterFail(t *testing.T) {
fatalError(t, err)

err = ns.Register("test", newTestFilterRule)
assert.Error(t, err)
assert.NoError(t, err)
}

func TestNamespaceError(t *testing.T) {
Expand Down

0 comments on commit 2ac0b3d

Please sign in to comment.