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