Skip to content

Commit

Permalink
Cleanup: Remove unused/underused API from the beats feature package (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
faec authored and chrisberkhout committed Jun 1, 2023
1 parent dc93ec8 commit ad950c5
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 443 deletions.
12 changes: 0 additions & 12 deletions filebeat/input/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ type Plugin struct {
Manager InputManager
}

// Details returns a generic feature description that is compatible with the
// feature package.
func (p Plugin) Details() feature.Details {
return feature.Details{
Name: p.Name,
Stability: p.Stability,
Deprecated: p.Deprecated,
Info: p.Info,
Doc: p.Doc,
}
}

func (p Plugin) validate() error {
if p.Name == "" {
return fmt.Errorf("input plugin without name found")
Expand Down
79 changes: 0 additions & 79 deletions libbeat/feature/bundle.go

This file was deleted.

71 changes: 0 additions & 71 deletions libbeat/feature/bundle_test.go

This file was deleted.

88 changes: 13 additions & 75 deletions libbeat/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@

package feature

import (
"fmt"
)

// Registry is the global plugin registry, this variable is meant to be temporary to move all the
// internal factory to receive a context that include the current beat registry.
var registry = NewRegistry()

// Featurable implements the description of a feature.
type Featurable interface {
bundleable

// Namespace is the kind of plugin or functionality we want to expose as a feature.
// Examples: Autodiscover's provider, processors, outputs.
Namespace() string
Expand All @@ -41,19 +35,13 @@ type Featurable interface {
// Factory returns the function used to create an instance of the Feature, the signature
// of the method is type checked by the 'FindFactory' of each namespace.
Factory() interface{}

// Description return the avaiable information for a specific feature.
Description() Details

String() string
}

// Feature contains the information for a specific feature
type Feature struct {
namespace string
name string
factory interface{}
description Details
namespace string
name string
factory interface{}
}

// Namespace return the namespace of the feature.
Expand All @@ -71,28 +59,12 @@ func (f *Feature) Factory() interface{} {
return f.factory
}

// Description return the avaiable information for a specific feature.
func (f *Feature) Description() Details {
return f.description
}

// Features return the current feature as a slice to be compatible with Bundle merging and filtering.
func (f *Feature) Features() []Featurable {
return []Featurable{f}
}

// String return the debug information
func (f *Feature) String() string {
return fmt.Sprintf("%s/%s (description: %s)", f.namespace, f.name, f.description)
}

// New returns a new Feature.
func New(namespace, name string, factory interface{}, description Details) *Feature {
func New(namespace, name string, factory interface{}) *Feature {
return &Feature{
namespace: namespace,
name: name,
factory: factory,
description: description,
namespace: namespace,
name: name,
factory: factory,
}
}

Expand All @@ -101,54 +73,20 @@ func GlobalRegistry() *Registry {
return registry
}

// RegisterBundle registers a bundle of features.
func RegisterBundle(bundle *Bundle) error {
for _, f := range bundle.Features() {
err := GlobalRegistry().Register(f)
// register registers new features on the global registry.
func register(features []Featurable) error {
for _, f := range features {
err := registry.Register(f)
if err != nil {
return err
}
}
return nil
}

// MustRegisterBundle register a new bundle and panic on error.
func MustRegisterBundle(bundle *Bundle) {
err := RegisterBundle(bundle)
if err != nil {
panic(err)
}
}

// OverwriteBundle register a bundle of feature and replace any existing feature with a new
// implementation.
func OverwriteBundle(bundle *Bundle) error {
for _, f := range bundle.Features() {
err := GlobalRegistry().Register(f)
if err != nil {
return err
}
}
return nil
}

// MustOverwriteBundle register a bundle of feature, replace any existing feature with a new
// implementation and panic on error.
func MustOverwriteBundle(bundle *Bundle) {
err := OverwriteBundle(bundle)
if err != nil {
panic(err)
}
}

// Register register a new feature on the global registry.
func Register(feature Featurable) error {
return GlobalRegistry().Register(feature)
}

// MustRegister register a new Feature on the global registry and panic on error.
func MustRegister(feature Featurable) {
err := Register(feature)
func MustRegister(features ...Featurable) {
err := register(features)
if err != nil {
panic(err)
}
Expand Down
37 changes: 2 additions & 35 deletions libbeat/feature/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,6 @@ func (r *Registry) Register(feature Featurable) error {
return nil
}

// Unregister removes a feature from the registry.
func (r *Registry) Unregister(namespace, name string) error {
r.Lock()
defer r.Unlock()
ns := normalize(namespace)

v, found := r.namespaces[ns]
if !found {
return fmt.Errorf("unknown namespace named '%s'", ns)
}

_, found = v[name]
if !found {
return fmt.Errorf("unknown feature '%s' in namespace '%s'", name, ns)
}

delete(r.namespaces[ns], name)
return nil
}

// Lookup searches for a Feature by the namespace-name pair.
func (r *Registry) Lookup(namespace, name string) (Featurable, error) {
r.RLock()
Expand Down Expand Up @@ -159,21 +139,8 @@ func (r *Registry) LookupAll(namespace string) ([]Featurable, error) {
return list, nil
}

// Overwrite allow to replace an existing feature with a new implementation.
func (r *Registry) Overwrite(feature Featurable) error {
_, err := r.Lookup(feature.Namespace(), feature.Name())
if err == nil {
err := r.Unregister(feature.Namespace(), feature.Name())
if err != nil {
return err
}
}

return r.Register(feature)
}

// Size returns the number of registered features in the registry.
func (r *Registry) Size() int {
// size returns the number of registered features in the registry.
func (r *Registry) size() int {
r.RLock()
defer r.RUnlock()

Expand Down
Loading

0 comments on commit ad950c5

Please sign in to comment.