-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
config.go
213 lines (182 loc) · 8.32 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package telemetry // import "go.opentelemetry.io/collector/service/telemetry"
import (
"errors"
"fmt"
"net"
"strconv"
"time"
"go.opentelemetry.io/contrib/config"
"go.uber.org/zap/zapcore"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/featuregate"
)
var _ confmap.Unmarshaler = (*Config)(nil)
var disableAddressFieldForInternalTelemetryFeatureGate = featuregate.GlobalRegistry().MustRegister(
"telemetry.disableAddressFieldForInternalTelemetry",
featuregate.StageAlpha,
featuregate.WithRegisterFromVersion("v0.111.0"),
featuregate.WithRegisterToVersion("v0.114.0"),
featuregate.WithRegisterDescription("controls whether the deprecated address field for internal telemetry is still supported"))
// Config defines the configurable settings for service telemetry.
type Config struct {
Logs LogsConfig `mapstructure:"logs"`
Metrics MetricsConfig `mapstructure:"metrics"`
Traces TracesConfig `mapstructure:"traces"`
// Resource specifies user-defined attributes to include with all emitted telemetry.
// Note that some attributes are added automatically (e.g. service.version) even
// if they are not specified here. In order to suppress such attributes the
// attribute must be specified in this map with null YAML value (nil string pointer).
Resource map[string]*string `mapstructure:"resource"`
}
// LogsConfig defines the configurable settings for service telemetry logs.
// This MUST be compatible with zap.Config. Cannot use directly zap.Config because
// the collector uses mapstructure and not yaml tags.
type LogsConfig struct {
// Level is the minimum enabled logging level.
// (default = "INFO")
Level zapcore.Level `mapstructure:"level"`
// Development puts the logger in development mode, which changes the
// behavior of DPanicLevel and takes stacktraces more liberally.
// (default = false)
Development bool `mapstructure:"development"`
// Encoding sets the logger's encoding.
// Example values are "json", "console".
Encoding string `mapstructure:"encoding"`
// DisableCaller stops annotating logs with the calling function's file
// name and line number. By default, all logs are annotated.
// (default = false)
DisableCaller bool `mapstructure:"disable_caller"`
// DisableStacktrace completely disables automatic stacktrace capturing. By
// default, stacktraces are captured for WarnLevel and above logs in
// development and ErrorLevel and above in production.
// (default = false)
DisableStacktrace bool `mapstructure:"disable_stacktrace"`
// Sampling sets a sampling policy.
// Default:
// sampling:
// enabled: true
// tick: 10s
// initial: 10
// thereafter: 100
// Sampling can be disabled by setting 'enabled' to false
Sampling *LogsSamplingConfig `mapstructure:"sampling"`
// OutputPaths is a list of URLs or file paths to write logging output to.
// The URLs could only be with "file" schema or without schema.
// The URLs with "file" schema must be an absolute path.
// The URLs without schema are treated as local file paths.
// "stdout" and "stderr" are interpreted as os.Stdout and os.Stderr.
// see details at Open in zap/writer.go.
// (default = ["stderr"])
OutputPaths []string `mapstructure:"output_paths"`
// ErrorOutputPaths is a list of URLs or file paths to write zap internal logger errors to.
// The URLs could only be with "file" schema or without schema.
// The URLs with "file" schema must use absolute paths.
// The URLs without schema are treated as local file paths.
// "stdout" and "stderr" are interpreted as os.Stdout and os.Stderr.
// see details at Open in zap/writer.go.
//
// Note that this setting only affects the zap internal logger errors.
// (default = ["stderr"])
ErrorOutputPaths []string `mapstructure:"error_output_paths"`
// InitialFields is a collection of fields to add to the root logger.
// Example:
//
// initial_fields:
// foo: "bar"
//
// By default, there is no initial field.
InitialFields map[string]any `mapstructure:"initial_fields"`
// Processors allow configuration of log record processors to emit logs to
// any number of suported backends.
Processors []config.LogRecordProcessor `mapstructure:"processors"`
}
// LogsSamplingConfig sets a sampling strategy for the logger. Sampling caps the
// global CPU and I/O load that logging puts on your process while attempting
// to preserve a representative subset of your logs.
type LogsSamplingConfig struct {
// Enabled enable sampling logging
Enabled bool `mapstructure:"enabled"`
// Tick represents the interval in seconds that the logger apply each sampling.
Tick time.Duration `mapstructure:"tick"`
// Initial represents the first M messages logged each Tick.
Initial int `mapstructure:"initial"`
// Thereafter represents the sampling rate, every Nth message will be sampled after Initial messages are logged during each Tick.
// If Thereafter is zero, the logger will drop all the messages after the Initial each Tick.
Thereafter int `mapstructure:"thereafter"`
}
// MetricsConfig exposes the common Telemetry configuration for one component.
// Experimental: *NOTE* this structure is subject to change or removal in the future.
type MetricsConfig struct {
// Level is the level of telemetry metrics, the possible values are:
// - "none" indicates that no telemetry data should be collected;
// - "basic" is the recommended and covers the basics of the service telemetry.
// - "normal" adds some other indicators on top of basic.
// - "detailed" adds dimensions and views to the previous levels.
Level configtelemetry.Level `mapstructure:"level"`
// Deprecated: [v0.111.0] use readers configuration.
Address string `mapstructure:"address"`
// Readers allow configuration of metric readers to emit metrics to
// any number of supported backends.
Readers []config.MetricReader `mapstructure:"readers"`
}
// TracesConfig exposes the common Telemetry configuration for collector's internal spans.
// Experimental: *NOTE* this structure is subject to change or removal in the future.
type TracesConfig struct {
// Level configures whether spans are emitted or not, the possible values are:
// - "none" indicates that no tracing data should be collected;
// - "basic" is the recommended and covers the basics of the service telemetry.
Level configtelemetry.Level `mapstructure:"level"`
// Propagators is a list of TextMapPropagators from the supported propagators list. Currently,
// tracecontext and b3 are supported. By default, the value is set to empty list and
// context propagation is disabled.
Propagators []string `mapstructure:"propagators"`
// Processors allow configuration of span processors to emit spans to
// any number of supported backends.
Processors []config.SpanProcessor `mapstructure:"processors"`
}
func (c *Config) Unmarshal(conf *confmap.Conf) error {
if err := conf.Unmarshal(c); err != nil {
return err
}
// If the support for "metrics::address" is disabled, nothing to do.
// TODO: when this gate is marked stable remove the whole Unmarshal definition.
if disableAddressFieldForInternalTelemetryFeatureGate.IsEnabled() {
return nil
}
if len(c.Metrics.Address) != 0 {
host, port, err := net.SplitHostPort(c.Metrics.Address)
if err != nil {
return fmt.Errorf("failing to parse metrics address %q: %w", c.Metrics.Address, err)
}
portInt, err := strconv.Atoi(port)
if err != nil {
return fmt.Errorf("failing to extract the port from the metrics address %q: %w", c.Metrics.Address, err)
}
// User did not overwrite readers, so we will remove the default configured reader.
if !conf.IsSet("metrics::readers") {
c.Metrics.Readers = nil
}
c.Metrics.Readers = append(c.Metrics.Readers, config.MetricReader{
Pull: &config.PullMetricReader{
Exporter: config.MetricExporter{
Prometheus: &config.Prometheus{
Host: &host,
Port: &portInt,
},
},
},
})
}
return nil
}
// Validate checks whether the current configuration is valid
func (c *Config) Validate() error {
// Check when service telemetry metric level is not none, the metrics readers should not be empty
if c.Metrics.Level != configtelemetry.LevelNone && len(c.Metrics.Readers) == 0 {
return errors.New("collector telemetry metrics reader should exist when metric level is not none")
}
return nil
}