forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
89 lines (73 loc) · 2.91 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
import (
"errors"
"strings"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)
var (
errUnsupportedPort = errors.New("unsupported port: port is required, must be in the range 1-65535")
errInvalidEndpoint = errors.New("invalid endpoint: endpoint is required but it is not configured")
errUnsupportedNetwork = errors.New("unsupported network: network is required, only tcp/udp supported")
errUnsupportedProtocol = errors.New("unsupported protocol: Only rfc5424 and rfc3164 supported")
errOctetCounting = errors.New("octet counting is only supported for rfc5424 protocol")
)
// Config defines configuration for Syslog exporter.
type Config struct {
// Syslog server address
Endpoint string `mapstructure:"endpoint"`
// Syslog server port
Port int `mapstructure:"port"`
// Network for syslog communication
// options: tcp, udp
Network string `mapstructure:"network"`
// Protocol of syslog messages
// options: rfc5424, rfc3164
Protocol string `mapstructure:"protocol"`
// Wether or not to enable RFC 6587 Octet Counting.
EnableOctetCounting bool `mapstructure:"enable_octet_counting"`
// TLSSetting struct exposes TLS client configuration.
TLSSetting configtls.ClientConfig `mapstructure:"tls"`
QueueSettings exporterhelper.QueueConfig `mapstructure:"sending_queue"`
configretry.BackOffConfig `mapstructure:"retry_on_failure"`
TimeoutSettings exporterhelper.TimeoutConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
}
// Validate the configuration for errors. This is required by component.Config.
func (cfg *Config) Validate() error {
invalidFields := []error{}
if cfg.Port < 1 || cfg.Port > 65535 {
invalidFields = append(invalidFields, errUnsupportedPort)
}
if cfg.Endpoint == "" {
invalidFields = append(invalidFields, errInvalidEndpoint)
}
cfg.Network = strings.ToLower(cfg.Network)
if cfg.Network != string(confignet.TransportTypeTCP) && cfg.Network != string(confignet.TransportTypeUDP) {
invalidFields = append(invalidFields, errUnsupportedNetwork)
}
switch cfg.Protocol {
case protocolRFC3164Str:
case protocolRFC5424Str:
default:
invalidFields = append(invalidFields, errUnsupportedProtocol)
}
if cfg.EnableOctetCounting && cfg.Protocol != protocolRFC5424Str {
invalidFields = append(invalidFields, errOctetCounting)
}
if len(invalidFields) > 0 {
return errors.Join(invalidFields...)
}
return nil
}
const (
// Syslog Network
DefaultNetwork = string(confignet.TransportTypeTCP)
// Syslog Port
DefaultPort = 514
// Syslog Protocol
DefaultProtocol = "rfc5424"
)