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

Remove feature.Describer and enhance Details #16658

Merged
merged 1 commit into from
Feb 27, 2020
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
2 changes: 1 addition & 1 deletion libbeat/feature/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func MustBundle(bundle ...bundleable) *Bundle {
func HasStabilityPred(stabilities ...Stability) FilterFunc {
return func(f Featurable) bool {
for _, s := range stabilities {
if s == f.Description().Stability() {
if s == f.Description().Stability {
return true
}
}
Expand Down
20 changes: 10 additions & 10 deletions libbeat/feature/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,39 @@ import (
func TestBundle(t *testing.T) {
factory := func() {}
features := []Featurable{
New("libbeat.outputs", "elasticsearch", factory, &Details{stability: Stable}),
New("libbeat.outputs", "edge", factory, &Details{stability: Experimental}),
New("libbeat.input", "tcp", factory, &Details{stability: Beta}),
New("libbeat.outputs", "elasticsearch", factory, Details{Stability: Stable}),
New("libbeat.outputs", "edge", factory, Details{Stability: Experimental}),
New("libbeat.input", "tcp", factory, Details{Stability: Beta}),
}

t.Run("Creates a new Bundle", func(t *testing.T) {
b := NewBundle(features...)
assert.Equal(t, 3, len(b.Features()))
})

t.Run("Filters feature based on stability", func(t *testing.T) {
t.Run("Filters feature based on Stability", func(t *testing.T) {
b := NewBundle(features...)
new := b.Filter(Experimental)
assert.Equal(t, 1, len(new.Features()))
})

t.Run("Filters feature based on multiple different stability", func(t *testing.T) {
t.Run("Filters feature based on multiple different Stability", func(t *testing.T) {
b := NewBundle(features...)
new := b.Filter(Experimental, Stable)
assert.Equal(t, 2, len(new.Features()))
})

t.Run("Creates a new Bundle from specified feature", func(t *testing.T) {
f1 := New("libbeat.outputs", "elasticsearch", factory, &Details{stability: Stable})
f1 := New("libbeat.outputs", "elasticsearch", factory, Details{Stability: Stable})
b := MustBundle(f1)
assert.Equal(t, 1, len(b.Features()))
})

t.Run("Creates a new Bundle with grouped features", func(t *testing.T) {
f1 := New("libbeat.outputs", "elasticsearch", factory, &Details{stability: Stable})
f2 := New("libbeat.outputs", "edge", factory, &Details{stability: Experimental})
f3 := New("libbeat.input", "tcp", factory, &Details{stability: Beta})
f4 := New("libbeat.input", "udp", factory, &Details{stability: Beta})
f1 := New("libbeat.outputs", "elasticsearch", factory, Details{Stability: Stable})
f2 := New("libbeat.outputs", "edge", factory, Details{Stability: Experimental})
f3 := New("libbeat.input", "tcp", factory, Details{Stability: Beta})
f4 := New("libbeat.input", "udp", factory, Details{Stability: Beta})

b := MustBundle(
MustBundle(f1),
Expand Down
63 changes: 14 additions & 49 deletions libbeat/feature/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,24 @@ package feature

import "fmt"

// Describer contains general information for a specific feature, the fields will be used to report
// useful information by the factories or any future CLI.
type Describer interface {
// Stability is the stability of the Feature, this allow the user to filter embedded functionality
// by their maturity at runtime.
// Example: Beta, Experimental, Stable or Undefined.
Stability() Stability

// Doc is a one liner describing the current feature.
// Example: Dissect allows to define patterns to extract useful information from a string.
Doc() string

// FullName is the human readable name of the feature.
// Example: Jolokia Discovery
FullName() string
}

// Details minimal information that you must provide when creating a feature.
type Details struct {
stability Stability
doc string
fullName string
}

// Stability is the stability of the Feature, this allow the user to filter embedded functionality
// by their maturity at runtime.
// Example: Beta, Experimental, Stable or Undefined.
func (d *Details) Stability() Stability {
return d.stability
}

// Doc is a one liner describing the current feature.
// Example: Dissect allows to define patterns to extract useful information from a string.
func (d *Details) Doc() string {
return d.doc
}

// FullName is the human readable name of the feature.
// Example: Jolokia Discovery
func (d *Details) FullName() string {
return d.fullName
Name string
Stability Stability
Deprecated bool
Info string // short info string
Doc string // long doc string
}

func (d *Details) String() string {
return fmt.Sprintf(
"name: %s, description: %s (stability: %s)",
d.fullName,
d.doc,
d.stability,
)
func (d Details) String() string {
fmtStr := "name: %s, description: %s (%s)"
if d.Deprecated {
fmtStr = "name: %s, description: %s (deprecated, %s)"
}
return fmt.Sprintf(fmtStr, d.Name, d.Info, d.Stability)
}

// NewDetails return the minimal information a new feature must provide.
func NewDetails(fullName string, doc string, stability Stability) *Details {
return &Details{fullName: fullName, doc: doc, stability: stability}
// MakeDetails return the minimal information a new feature must provide.
func MakeDetails(fullName string, doc string, stability Stability) Details {
return Details{Name: fullName, Info: doc, Stability: stability}
}
8 changes: 4 additions & 4 deletions libbeat/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Featurable interface {
Factory() interface{}

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

String() string
}
Expand All @@ -53,7 +53,7 @@ type Feature struct {
namespace string
name string
factory interface{}
description Describer
description Details
}

// Namespace return the namespace of the feature.
Expand All @@ -72,7 +72,7 @@ func (f *Feature) Factory() interface{} {
}

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

Expand All @@ -87,7 +87,7 @@ func (f *Feature) String() string {
}

// New returns a new Feature.
func New(namespace, name string, factory interface{}, description Describer) *Feature {
func New(namespace, name string, factory interface{}, description Details) *Feature {
return &Feature{
namespace: namespace,
name: name,
Expand Down
2 changes: 1 addition & 1 deletion libbeat/feature/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/stretchr/testify/assert"
)

var defaultDetails = &Details{stability: Stable}
var defaultDetails = Details{Stability: Stable}

func TestRegister(t *testing.T) {
f := func() {}
Expand Down
2 changes: 1 addition & 1 deletion libbeat/management/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type FactoryFunc func(*common.Config, *reload.Registry, uuid.UUID) (ConfigManage

// Register a config manager
func Register(name string, fn FactoryFunc, stability feature.Stability) {
f := feature.New(Namespace, name, fn, feature.NewDetails(name, "", stability))
f := feature.New(Namespace, name, fn, feature.MakeDetails(name, "", stability))
feature.MustRegister(f)
}

Expand Down
2 changes: 1 addition & 1 deletion libbeat/publisher/queue/memqueue/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// Feature exposes a memory queue.
var Feature = queue.Feature("mem",
create,
feature.NewDetails(
feature.MakeDetails(
"Memory queue",
"Buffer events in memory before sending to the output.",
feature.Stable),
Expand Down
4 changes: 2 additions & 2 deletions libbeat/publisher/queue/queue_reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var Namespace = "libbeat.queue"

// RegisterType registers a new queue type.
func RegisterType(name string, fn Factory) {
f := Feature(name, fn, feature.NewDetails(name, "", feature.Undefined))
f := Feature(name, fn, feature.MakeDetails(name, "", feature.Undefined))
feature.MustRegister(f)
}

Expand All @@ -45,6 +45,6 @@ func FindFactory(name string) Factory {
}

// Feature creates a new type of queue.
func Feature(name string, factory Factory, description feature.Describer) *feature.Feature {
func Feature(name string, factory Factory, description feature.Details) *feature.Feature {
return feature.New(Namespace, name, factory, description)
}
2 changes: 1 addition & 1 deletion libbeat/publisher/queue/spool/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// Feature exposes a spooling to disk queue.
var Feature = queue.Feature("spool", create,
feature.NewDetails(
feature.MakeDetails(
"Memory queue",
"Buffer events in memory before sending to the output.",
feature.Beta),
Expand Down
16 changes: 8 additions & 8 deletions x-pack/functionbeat/function/provider/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ func getNamespace(provider string) string {

// Feature creates a new Provider feature to be added to the global registry.
// The namespace will be 'functionbeat.provider' in the registry.
func Feature(name string, factory Factory, description feature.Describer) *feature.Feature {
return feature.New(namespace, name, factory, description)
func Feature(name string, factory Factory, details feature.Details) *feature.Feature {
return feature.New(namespace, name, factory, details)
}

// FunctionFeature Feature creates a new function feature to be added to the global registry
// The namespace will be 'functionbeat.provider.local' in the registry.
func FunctionFeature(
provider, name string,
factory FunctionFactory,
description feature.Describer,
details feature.Details,
) *feature.Feature {
return feature.New(getNamespace(provider), name, factory, description)
return feature.New(getNamespace(provider), name, factory, details)
}

// Builder is used to have a fluent interface to build a set of function for a specific provider, it
Expand All @@ -41,8 +41,8 @@ type Builder struct {

// MustCreate creates a new provider builder, it is used to define a provider and the function
// it supports.
func MustCreate(name string, factory Factory, description feature.Describer) *Builder {
return &Builder{name: name, bundle: feature.NewBundle(Feature(name, factory, description))}
func MustCreate(name string, factory Factory, details feature.Details) *Builder {
return &Builder{name: name, bundle: feature.NewBundle(Feature(name, factory, details))}
}

// Bundle transforms the provider and the functions into a bundle feature.
Expand All @@ -54,8 +54,8 @@ func (b *Builder) Bundle() *feature.Bundle {
func (b *Builder) MustAddFunction(
name string,
factory FunctionFactory,
description feature.Describer,
details feature.Details,
) *Builder {
b.bundle = feature.MustBundle(b.bundle, FunctionFeature(b.name, name, factory, description))
b.bundle = feature.MustBundle(b.bundle, FunctionFeature(b.name, name, factory, details))
return b
}
6 changes: 3 additions & 3 deletions x-pack/functionbeat/function/provider/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestBuilder(t *testing.T) {
b := MustCreate(
provider,
providerFactory,
feature.NewDetails("myprovider", "myprovider", feature.Experimental),
feature.MakeDetails("myprovider", "myprovider", feature.Experimental),
).MustAddFunction(
"f1",
fnFactory1,
feature.NewDetails("fn1 description", "fn1", feature.Experimental),
).MustAddFunction("f2", fnFactory2, feature.NewDetails(
feature.MakeDetails("fn1 description", "fn1", feature.Experimental),
).MustAddFunction("f2", fnFactory2, feature.MakeDetails(
"fn1 description",
"fn1",
feature.Experimental,
Expand Down
10 changes: 5 additions & 5 deletions x-pack/functionbeat/function/provider/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func testProviderLookup(t *testing.T) {
f := Feature(
name,
providerFn,
feature.NewDetails(name, "provider for testing", feature.Experimental),
feature.MakeDetails(name, "provider for testing", feature.Experimental),
)

t.Run("adding and retrieving a provider", withRegistry(func(
Expand Down Expand Up @@ -115,7 +115,7 @@ func testFunctionLookup(t *testing.T) {
f := Feature(
name,
providerFn,
feature.NewDetails(name, "provider for testing", feature.Experimental),
feature.MakeDetails(name, "provider for testing", feature.Experimental),
)

fnName := "myfunc"
Expand All @@ -124,7 +124,7 @@ func testFunctionLookup(t *testing.T) {
return myfunction, nil
}

fnFeature := FunctionFeature(name, fnName, functionFn, feature.NewDetails(
fnFeature := FunctionFeature(name, fnName, functionFn, feature.MakeDetails(
name,
"provider for testing",
feature.Experimental,
Expand Down Expand Up @@ -252,14 +252,14 @@ func TestFindFunctionByName(t *testing.T) {
providerFn := func(log *logp.Logger, registry *Registry, config *common.Config) (Provider, error) {
return myprovider, nil
}
f := Feature(name, providerFn, feature.NewDetails(name, "provider for testing", feature.Experimental))
f := Feature(name, providerFn, feature.MakeDetails(name, "provider for testing", feature.Experimental))

myfunction := &mockFunction{name}
functionFn := func(provider Provider, config *common.Config) (Function, error) {
return myfunction, nil
}

fnFeature := FunctionFeature(name, fnName, functionFn, feature.NewDetails(
fnFeature := FunctionFeature(name, fnName, functionFn, feature.MakeDetails(
name,
"provider for testing",
feature.Experimental,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/functionbeat/manager/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var Bundle = provider.MustCreate(
"aws",
provider.NewDefaultProvider("aws", NewCLI, NewTemplateBuilder),
feature.NewDetails("AWS Lambda", "listen to events on AWS lambda", feature.Stable),
feature.MakeDetails("AWS Lambda", "listen to events on AWS lambda", feature.Stable),
).MustAddFunction("cloudwatch_logs",
aws.NewCloudwatchLogs,
aws.CloudwatchLogsDetails(),
Expand Down
2 changes: 1 addition & 1 deletion x-pack/functionbeat/manager/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var Bundle = provider.MustCreate(
"gcp",
provider.NewDefaultProvider("gcp", NewCLI, NewTemplateBuilder),
feature.NewDetails("Google Cloud Functions", "listen to events on Google Cloud", feature.Stable),
feature.MakeDetails("Google Cloud Functions", "listen to events on Google Cloud", feature.Stable),
).MustAddFunction("pubsub",
gcp.NewPubSub,
gcp.PubSubDetails(),
Expand Down
4 changes: 2 additions & 2 deletions x-pack/functionbeat/provider/aws/aws/api_gateway_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func NewAPIGatewayProxy(provider provider.Provider, config *common.Config) (prov
}

// APIGatewayProxyDetails returns the details of the feature.
func APIGatewayProxyDetails() *feature.Details {
return feature.NewDetails("API Gateway proxy trigger", "receive events from the api gateway proxy", feature.Experimental)
func APIGatewayProxyDetails() feature.Details {
return feature.MakeDetails("API Gateway proxy trigger", "receive events from the api gateway proxy", feature.Experimental)
}

// Run starts the lambda function and wait for web triggers.
Expand Down
4 changes: 2 additions & 2 deletions x-pack/functionbeat/provider/aws/aws/cloudwatch_kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func defaultCloudwatchKinesisConfig() *CloudwatchKinesisConfig {
}

// CloudwatchKinesisDetails returns the details of the feature.
func CloudwatchKinesisDetails() *feature.Details {
return feature.NewDetails("Cloudwatch logs via Kinesis trigger", "receive Cloudwatch logs from a Kinesis stream", feature.Experimental)
func CloudwatchKinesisDetails() feature.Details {
return feature.MakeDetails("Cloudwatch logs via Kinesis trigger", "receive Cloudwatch logs from a Kinesis stream", feature.Experimental)
}

// Run starts the lambda function and wait for web triggers.
Expand Down
4 changes: 2 additions & 2 deletions x-pack/functionbeat/provider/aws/aws/cloudwatch_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func NewCloudwatchLogs(provider provider.Provider, cfg *common.Config) (provider
}

// CloudwatchLogsDetails returns the details of the feature.
func CloudwatchLogsDetails() *feature.Details {
return feature.NewDetails("Cloudwatch Logs trigger", "receive events from cloudwatch logs.", feature.Stable)
func CloudwatchLogsDetails() feature.Details {
return feature.MakeDetails("Cloudwatch Logs trigger", "receive events from cloudwatch logs.", feature.Stable)
}

// Run start the AWS lambda handles and will transform any events received to the pipeline.
Expand Down
4 changes: 2 additions & 2 deletions x-pack/functionbeat/provider/aws/aws/kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func NewKinesis(provider provider.Provider, cfg *common.Config) (provider.Functi
}

// KinesisDetails returns the details of the feature.
func KinesisDetails() *feature.Details {
return feature.NewDetails("Kinesis trigger", "receive events from a Kinesis stream", feature.Stable)
func KinesisDetails() feature.Details {
return feature.MakeDetails("Kinesis trigger", "receive events from a Kinesis stream", feature.Stable)
}

// Run starts the lambda function and wait for web triggers.
Expand Down
4 changes: 2 additions & 2 deletions x-pack/functionbeat/provider/aws/aws/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func NewSQS(provider provider.Provider, cfg *common.Config) (provider.Function,
}

// SQSDetails returns the details of the feature.
func SQSDetails() *feature.Details {
return feature.NewDetails("SQS trigger", "receive events from a SQS queue", feature.Stable)
func SQSDetails() feature.Details {
return feature.MakeDetails("SQS trigger", "receive events from a SQS queue", feature.Stable)
}

// Run starts the lambda function and wait for web triggers.
Expand Down
Loading