-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathconfig.go
99 lines (81 loc) · 2.34 KB
/
config.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package pipeline // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/pipeline"
import (
"fmt"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
)
// Config is the configuration of a pipeline.
type Config struct {
DefaultOutput operator.Operator
Operators []operator.Config
}
// Build will build a pipeline from the config.
func (c Config) Build(logger *zap.SugaredLogger) (*DirectedPipeline, error) {
if logger == nil {
return nil, errors.NewError("logger must be provided", "")
}
if c.Operators == nil {
return nil, errors.NewError("operators must be specified", "")
}
if len(c.Operators) == 0 {
return nil, errors.NewError("empty pipeline not allowed", "")
}
sampledLogger := logger.Desugar().WithOptions(
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSamplerWithOptions(core, time.Second, 1, 10000)
}),
).Sugar()
dedeplucateIDs(c.Operators)
ops := make([]operator.Operator, 0, len(c.Operators))
for _, opCfg := range c.Operators {
op, err := opCfg.Build(sampledLogger)
if err != nil {
return nil, err
}
ops = append(ops, op)
}
for i, op := range ops {
// Any operator that already has an output will not be changed
if len(op.GetOutputIDs()) > 0 {
continue
}
// Any operator (except the last) will just output to the next
if i+1 < len(ops) {
op.SetOutputIDs([]string{ops[i+1].ID()})
continue
}
// The last operator may output to the default output
if op.CanOutput() && c.DefaultOutput != nil {
ops = append(ops, c.DefaultOutput)
op.SetOutputIDs([]string{ops[i+1].ID()})
}
}
return NewDirectedPipeline(ops)
}
func dedeplucateIDs(ops []operator.Config) {
typeMap := make(map[string]int)
for _, op := range ops {
if op.Type() != op.ID() {
continue
}
if typeMap[op.Type()] == 0 {
typeMap[op.Type()]++
continue
}
newID := fmt.Sprintf("%s%d", op.Type(), typeMap[op.Type()])
for j := 0; j < len(ops); j++ {
if newID == ops[j].ID() {
j = 0
typeMap[op.Type()]++
newID = fmt.Sprintf("%s%d", op.Type(), typeMap[op.Type()])
}
}
typeMap[op.Type()]++
op.SetID(newID)
}
}