forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
131 lines (109 loc) · 3.83 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package awscloudwatchreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscloudwatchreceiver"
import (
"errors"
"fmt"
"net/url"
"time"
"go.opentelemetry.io/collector/confmap"
)
var (
defaultPollInterval = time.Minute
defaultEventLimit = 1000
defaultLogGroupLimit = 50
)
// Config is the overall config structure for the awscloudwatchreceiver
type Config struct {
Region string `mapstructure:"region"`
Profile string `mapstructure:"profile"`
IMDSEndpoint string `mapstructure:"imds_endpoint"`
Logs *LogsConfig `mapstructure:"logs"`
}
// LogsConfig is the configuration for the logs portion of this receiver
type LogsConfig struct {
PollInterval time.Duration `mapstructure:"poll_interval"`
MaxEventsPerRequest int `mapstructure:"max_events_per_request"`
Groups GroupConfig `mapstructure:"groups"`
}
// GroupConfig is the configuration for log group collection
type GroupConfig struct {
AutodiscoverConfig *AutodiscoverConfig `mapstructure:"autodiscover,omitempty"`
NamedConfigs map[string]StreamConfig `mapstructure:"named"`
}
// AutodiscoverConfig is the configuration for the autodiscovery functionality of log groups
type AutodiscoverConfig struct {
Prefix string `mapstructure:"prefix"`
Limit int `mapstructure:"limit"`
Streams StreamConfig `mapstructure:"streams"`
}
// StreamConfig represents the configuration for the log stream filtering
type StreamConfig struct {
Prefixes []*string `mapstructure:"prefixes"`
Names []*string `mapstructure:"names"`
}
var (
errNoRegion = errors.New("no region was specified")
errNoLogsConfigured = errors.New("no logs configured")
errInvalidEventLimit = errors.New("event limit is improperly configured, value must be greater than 0")
errInvalidPollInterval = errors.New("poll interval is incorrect, it must be a duration greater than one second")
errInvalidAutodiscoverLimit = errors.New("the limit of autodiscovery of log groups is improperly configured, value must be greater than 0")
errAutodiscoverAndNamedConfigured = errors.New("both autodiscover and named configs are configured, Only one or the other is permitted")
)
// Validate validates all portions of the relevant config
func (c *Config) Validate() error {
if c.Region == "" {
return errNoRegion
}
if c.IMDSEndpoint != "" {
_, err := url.ParseRequestURI(c.IMDSEndpoint)
if err != nil {
return fmt.Errorf("unable to parse URI for imds_endpoint: %w", err)
}
}
var errs error
errs = errors.Join(errs, c.validateLogsConfig())
return errs
}
// Unmarshal is a custom unmarshaller that ensures that autodiscover is nil if
// autodiscover is not specified
func (c *Config) Unmarshal(componentParser *confmap.Conf) error {
if componentParser == nil {
return errors.New("")
}
err := componentParser.Unmarshal(c)
if err != nil {
return err
}
if componentParser.IsSet("logs::groups::named") && !componentParser.IsSet("logs::groups::autodiscover") {
c.Logs.Groups.AutodiscoverConfig = nil
}
return nil
}
func (c *Config) validateLogsConfig() error {
if c.Logs == nil {
return errNoLogsConfigured
}
if c.Logs.MaxEventsPerRequest <= 0 {
return errInvalidEventLimit
}
if c.Logs.PollInterval < time.Second {
return errInvalidPollInterval
}
return c.Logs.Groups.validate()
}
func (c *GroupConfig) validate() error {
if c.AutodiscoverConfig != nil && len(c.NamedConfigs) > 0 {
return errAutodiscoverAndNamedConfigured
}
if c.AutodiscoverConfig != nil {
return validateAutodiscover(*c.AutodiscoverConfig)
}
return nil
}
func validateAutodiscover(cfg AutodiscoverConfig) error {
if cfg.Limit <= 0 {
return errInvalidAutodiscoverLimit
}
return nil
}