-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config.go
114 lines (93 loc) · 3.77 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslog // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/syslog"
import (
"errors"
"fmt"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/tcp"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/udp"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/syslog"
)
const operatorType = "syslog_input"
func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}
// NewConfig creates a new input config with default values
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}
// NewConfigWithID creates a new input config with default values
func NewConfigWithID(operatorID string) *Config {
return &Config{
InputConfig: helper.NewInputConfig(operatorID, operatorType),
}
}
type Config struct {
helper.InputConfig `mapstructure:",squash"`
syslog.BaseConfig `mapstructure:",squash"`
TCP *tcp.BaseConfig `mapstructure:"tcp"`
UDP *udp.BaseConfig `mapstructure:"udp"`
}
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
inputBase, err := c.InputConfig.Build(logger)
if err != nil {
return nil, err
}
syslogParserCfg := syslog.NewConfigWithID(inputBase.ID() + "_internal_tcp")
syslogParserCfg.BaseConfig = c.BaseConfig
syslogParserCfg.SetID(inputBase.ID() + "_internal_parser")
syslogParserCfg.OutputIDs = c.OutputIDs
syslogParser, err := syslogParserCfg.Build(logger)
if err != nil {
return nil, fmt.Errorf("failed to resolve syslog config: %w", err)
}
if c.TCP != nil {
tcpInputCfg := tcp.NewConfigWithID(inputBase.ID() + "_internal_tcp")
tcpInputCfg.InputConfig.AttributerConfig = c.InputConfig.AttributerConfig
tcpInputCfg.InputConfig.IdentifierConfig = c.InputConfig.IdentifierConfig
tcpInputCfg.BaseConfig = *c.TCP
if syslogParserCfg.EnableOctetCounting {
tcpInputCfg.SplitFuncBuilder = OctetSplitFuncBuilder
}
tcpInput, err := tcpInputCfg.Build(logger)
if err != nil {
return nil, fmt.Errorf("failed to resolve tcp config: %w", err)
}
tcpInput.SetOutputIDs([]string{syslogParser.ID()})
if err := tcpInput.SetOutputs([]operator.Operator{syslogParser}); err != nil {
return nil, fmt.Errorf("failed to set outputs")
}
return &Input{
InputOperator: inputBase,
tcp: tcpInput.(*tcp.Input),
parser: syslogParser.(*syslog.Parser),
}, nil
}
if c.UDP != nil {
udpInputCfg := udp.NewConfigWithID(inputBase.ID() + "_internal_udp")
udpInputCfg.InputConfig.AttributerConfig = c.InputConfig.AttributerConfig
udpInputCfg.InputConfig.IdentifierConfig = c.InputConfig.IdentifierConfig
udpInputCfg.BaseConfig = *c.UDP
// Octet counting and Non-Transparent-Framing are invalid for UDP connections
if syslogParserCfg.EnableOctetCounting || syslogParserCfg.NonTransparentFramingTrailer != nil {
return nil, errors.New("octet_counting and non_transparent_framing is not compatible with UDP")
}
udpInput, err := udpInputCfg.Build(logger)
if err != nil {
return nil, fmt.Errorf("failed to resolve udp config: %w", err)
}
udpInput.SetOutputIDs([]string{syslogParser.ID()})
if err := udpInput.SetOutputs([]operator.Operator{syslogParser}); err != nil {
return nil, fmt.Errorf("failed to set outputs")
}
return &Input{
InputOperator: inputBase,
udp: udpInput.(*udp.Input),
parser: syslogParser.(*syslog.Parser),
}, nil
}
return nil, fmt.Errorf("need tcp config or udp config")
}