github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/helper/operator.go (about) 1 package helper 2 3 import ( 4 "github.com/observiq/carbon/errors" 5 "github.com/observiq/carbon/operator" 6 "go.uber.org/zap" 7 ) 8 9 func NewBasicConfig(operatorID, operatorType string) BasicConfig { 10 return BasicConfig{ 11 OperatorID: operatorID, 12 OperatorType: operatorType, 13 } 14 } 15 16 // BasicConfig provides a basic implemention for an operator config. 17 type BasicConfig struct { 18 OperatorID string `json:"id" yaml:"id"` 19 OperatorType string `json:"type" yaml:"type"` 20 } 21 22 // ID will return the operator id. 23 func (c BasicConfig) ID() string { 24 if c.OperatorID == "" { 25 return c.OperatorType 26 } 27 return c.OperatorID 28 } 29 30 // Type will return the operator type. 31 func (c BasicConfig) Type() string { 32 return c.OperatorType 33 } 34 35 // Build will build a basic operator. 36 func (c BasicConfig) Build(context operator.BuildContext) (BasicOperator, error) { 37 if c.OperatorType == "" { 38 return BasicOperator{}, errors.NewError( 39 "missing required `type` field.", 40 "ensure that all operators have a uniquely defined `type` field.", 41 "operator_id", c.ID(), 42 ) 43 } 44 45 if context.Logger == nil { 46 return BasicOperator{}, errors.NewError( 47 "operator build context is missing a logger.", 48 "this is an unexpected internal error", 49 "operator_id", c.ID(), 50 "operator_type", c.Type(), 51 ) 52 } 53 54 operator := BasicOperator{ 55 OperatorID: c.ID(), 56 OperatorType: c.Type(), 57 SugaredLogger: context.Logger.With("operator_id", c.ID(), "operator_type", c.Type()), 58 } 59 60 return operator, nil 61 } 62 63 // SetNamespace will namespace the operator id. 64 func (c *BasicConfig) SetNamespace(namespace string, exclusions ...string) { 65 if CanNamespace(c.ID(), exclusions) { 66 c.OperatorID = AddNamespace(c.ID(), namespace) 67 } 68 } 69 70 // BasicOperator provides a basic implementation of an operator. 71 type BasicOperator struct { 72 OperatorID string 73 OperatorType string 74 *zap.SugaredLogger 75 } 76 77 // ID will return the operator id. 78 func (p *BasicOperator) ID() string { 79 if p.OperatorID == "" { 80 return p.OperatorType 81 } 82 return p.OperatorID 83 } 84 85 // Type will return the operator type. 86 func (p *BasicOperator) Type() string { 87 return p.OperatorType 88 } 89 90 // Logger returns the operator's scoped logger. 91 func (p *BasicOperator) Logger() *zap.SugaredLogger { 92 return p.SugaredLogger 93 } 94 95 // Start will start the operator. 96 func (p *BasicOperator) Start() error { 97 return nil 98 } 99 100 // Stop will stop the operator. 101 func (p *BasicOperator) Stop() error { 102 return nil 103 }