-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
config.go
239 lines (206 loc) · 8.51 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package fileconsumer // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer"
import (
"bufio"
"errors"
"fmt"
"time"
"go.opentelemetry.io/collector/featuregate"
"go.uber.org/zap"
"golang.org/x/text/encoding"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/decode"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/emit"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/header"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/reader"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher"
"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/split"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/trim"
)
const (
defaultMaxLogSize = 1024 * 1024
defaultMaxConcurrentFiles = 1024
defaultEncoding = "utf-8"
defaultPollInterval = 200 * time.Millisecond
defaultFlushPeriod = 500 * time.Millisecond
)
var allowFileDeletion = featuregate.GlobalRegistry().MustRegister(
"filelog.allowFileDeletion",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, allows usage of the `delete_after_read` setting."),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/16314"),
)
var AllowHeaderMetadataParsing = featuregate.GlobalRegistry().MustRegister(
"filelog.allowHeaderMetadataParsing",
featuregate.StageBeta,
featuregate.WithRegisterDescription("When enabled, allows usage of the `header` setting."),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/18198"),
)
// NewConfig creates a new input config with default values
func NewConfig() *Config {
return &Config{
IncludeFileName: true,
IncludeFilePath: false,
IncludeFileNameResolved: false,
IncludeFilePathResolved: false,
PollInterval: defaultPollInterval,
Encoding: defaultEncoding,
StartAt: "end",
FingerprintSize: fingerprint.DefaultSize,
MaxLogSize: defaultMaxLogSize,
MaxConcurrentFiles: defaultMaxConcurrentFiles,
MaxBatches: 0,
FlushPeriod: defaultFlushPeriod,
}
}
// Config is the configuration of a file input operator
type Config struct {
matcher.Criteria `mapstructure:",squash"`
IncludeFileName bool `mapstructure:"include_file_name,omitempty"`
IncludeFilePath bool `mapstructure:"include_file_path,omitempty"`
IncludeFileNameResolved bool `mapstructure:"include_file_name_resolved,omitempty"`
IncludeFilePathResolved bool `mapstructure:"include_file_path_resolved,omitempty"`
PollInterval time.Duration `mapstructure:"poll_interval,omitempty"`
StartAt string `mapstructure:"start_at,omitempty"`
FingerprintSize helper.ByteSize `mapstructure:"fingerprint_size,omitempty"`
MaxLogSize helper.ByteSize `mapstructure:"max_log_size,omitempty"`
MaxConcurrentFiles int `mapstructure:"max_concurrent_files,omitempty"`
MaxBatches int `mapstructure:"max_batches,omitempty"`
DeleteAfterRead bool `mapstructure:"delete_after_read,omitempty"`
SplitConfig split.Config `mapstructure:"multiline,omitempty"`
TrimConfig trim.Config `mapstructure:",squash,omitempty"`
Encoding string `mapstructure:"encoding,omitempty"`
FlushPeriod time.Duration `mapstructure:"force_flush_period,omitempty"`
Header *HeaderConfig `mapstructure:"header,omitempty"`
}
type HeaderConfig struct {
Pattern string `mapstructure:"pattern"`
MetadataOperators []operator.Config `mapstructure:"metadata_operators"`
}
// Build will build a file input operator from the supplied configuration
func (c Config) Build(logger *zap.SugaredLogger, emit emit.Callback) (*Manager, error) {
if err := c.validate(); err != nil {
return nil, err
}
enc, err := decode.LookupEncoding(c.Encoding)
if err != nil {
return nil, err
}
splitFunc, err := c.SplitConfig.Func(enc, false, int(c.MaxLogSize))
if err != nil {
return nil, err
}
trimFunc := trim.Nop
if enc != encoding.Nop {
trimFunc = c.TrimConfig.Func()
}
return c.buildManager(logger, emit, splitFunc, trimFunc)
}
// BuildWithSplitFunc will build a file input operator with customized splitFunc function
func (c Config) BuildWithSplitFunc(logger *zap.SugaredLogger, emit emit.Callback, splitFunc bufio.SplitFunc) (*Manager, error) {
if err := c.validate(); err != nil {
return nil, err
}
return c.buildManager(logger, emit, splitFunc, c.TrimConfig.Func())
}
func (c Config) buildManager(logger *zap.SugaredLogger, emit emit.Callback, splitFunc bufio.SplitFunc, trimFunc trim.Func) (*Manager, error) {
if emit == nil {
return nil, fmt.Errorf("must provide emit function")
}
var startAtBeginning bool
switch c.StartAt {
case "beginning":
startAtBeginning = true
case "end":
startAtBeginning = false
default:
return nil, fmt.Errorf("invalid start_at location '%s'", c.StartAt)
}
enc, err := decode.LookupEncoding(c.Encoding)
if err != nil {
return nil, fmt.Errorf("failed to find encoding: %w", err)
}
var hCfg *header.Config
if c.Header != nil {
hCfg, err = header.NewConfig(c.Header.Pattern, c.Header.MetadataOperators, enc)
if err != nil {
return nil, fmt.Errorf("failed to build header config: %w", err)
}
}
fileMatcher, err := matcher.New(c.Criteria)
if err != nil {
return nil, err
}
return &Manager{
SugaredLogger: logger.With("component", "fileconsumer"),
cancel: func() {},
readerFactory: reader.Factory{
SugaredLogger: logger.With("component", "fileconsumer"),
Config: &reader.Config{
FingerprintSize: int(c.FingerprintSize),
MaxLogSize: int(c.MaxLogSize),
Emit: emit,
IncludeFileName: c.IncludeFileName,
IncludeFilePath: c.IncludeFilePath,
IncludeFileNameResolved: c.IncludeFileNameResolved,
IncludeFilePathResolved: c.IncludeFilePathResolved,
DeleteAtEOF: c.DeleteAfterRead,
FlushTimeout: c.FlushPeriod,
},
FromBeginning: startAtBeginning,
Encoding: enc,
SplitFunc: splitFunc,
TrimFunc: trimFunc,
HeaderConfig: hCfg,
},
fileMatcher: fileMatcher,
pollInterval: c.PollInterval,
maxBatchFiles: c.MaxConcurrentFiles / 2,
maxBatches: c.MaxBatches,
previousPollFiles: make([]*reader.Reader, 0, c.MaxConcurrentFiles/2),
knownFiles: make([]*reader.Reader, 0, 10*c.MaxConcurrentFiles),
seenPaths: make(map[string]struct{}, 100),
}, nil
}
func (c Config) validate() error {
if c.DeleteAfterRead && !allowFileDeletion.IsEnabled() {
return fmt.Errorf("`delete_after_read` requires feature gate `%s`", allowFileDeletion.ID())
}
if c.Header != nil && !AllowHeaderMetadataParsing.IsEnabled() {
return fmt.Errorf("`header` requires feature gate `%s`", AllowHeaderMetadataParsing.ID())
}
if _, err := matcher.New(c.Criteria); err != nil {
return err
}
if c.MaxLogSize <= 0 {
return fmt.Errorf("`max_log_size` must be positive")
}
if c.MaxConcurrentFiles <= 1 {
return fmt.Errorf("`max_concurrent_files` must be greater than 1")
}
if c.FingerprintSize < fingerprint.MinSize {
return fmt.Errorf("`fingerprint_size` must be at least %d bytes", fingerprint.MinSize)
}
if c.DeleteAfterRead && c.StartAt == "end" {
return fmt.Errorf("`delete_after_read` cannot be used with `start_at: end`")
}
if c.Header != nil && c.StartAt == "end" {
return fmt.Errorf("`header` cannot be specified with `start_at: end`")
}
if c.MaxBatches < 0 {
return errors.New("`max_batches` must not be negative")
}
enc, err := decode.LookupEncoding(c.Encoding)
if err != nil {
return err
}
if c.Header != nil {
if _, err := header.NewConfig(c.Header.Pattern, c.Header.MetadataOperators, enc); err != nil {
return fmt.Errorf("invalid config for `header`: %w", err)
}
}
return nil
}