-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcollector.go
More file actions
351 lines (290 loc) · 9.85 KB
/
collector.go
File metadata and controls
351 lines (290 loc) · 9.85 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package service handles the command-line, configuration, and runs the
// OpenTelemetry Collector.
package service
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcheck"
"go.opentelemetry.io/collector/config/configloader"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/config/experimental/configsource"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/internal/collector/telemetry"
"go.opentelemetry.io/collector/service/internal/builder"
"go.opentelemetry.io/collector/service/parserprovider"
)
const (
servicezPath = "servicez"
pipelinezPath = "pipelinez"
extensionzPath = "extensionz"
)
// State defines Collector's state.
type State int
const (
Starting State = iota
Running
Closing
Closed
)
// Collector represents a server providing the OpenTelemetry Collector service.
type Collector struct {
info component.BuildInfo
rootCmd *cobra.Command
logger *zap.Logger
service *service
stateChannel chan State
factories component.Factories
parserProvider parserprovider.ParserProvider
// stopTestChan is used to terminate the collector server in end to end tests.
stopTestChan chan struct{}
// signalsChannel is used to receive termination signals from the OS.
signalsChannel chan os.Signal
// asyncErrorChannel is used to signal a fatal error from any component.
asyncErrorChannel chan error
}
// New creates and returns a new instance of Collector.
func New(set CollectorSettings) (*Collector, error) {
if err := configcheck.ValidateConfigFromFactories(set.Factories); err != nil {
return nil, err
}
col := &Collector{
info: set.BuildInfo,
factories: set.Factories,
stateChannel: make(chan State, Closed+1),
}
rootCmd := &cobra.Command{
Use: set.BuildInfo.Command,
Version: set.BuildInfo.Version,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
if col.logger, err = newLogger(set.LoggingOptions); err != nil {
return fmt.Errorf("failed to get logger: %w", err)
}
return col.execute(cmd.Context())
},
}
// TODO: coalesce this code and expose this information to other components.
flagSet := new(flag.FlagSet)
addFlagsFns := []func(*flag.FlagSet){
configtelemetry.Flags,
parserprovider.Flags,
telemetry.Flags,
builder.Flags,
loggerFlags,
}
for _, addFlags := range addFlagsFns {
addFlags(flagSet)
}
rootCmd.Flags().AddGoFlagSet(flagSet)
col.rootCmd = rootCmd
parserProvider := set.ParserProvider
if parserProvider == nil {
// use default provider.
parserProvider = parserprovider.Default()
}
col.parserProvider = parserProvider
return col, nil
}
// Run starts the collector according to the command and configuration
// given by the user, and waits for it to complete.
func (col *Collector) Run() error {
// From this point on do not show usage in case of error.
col.rootCmd.SilenceUsage = true
return col.rootCmd.Execute()
}
// GetStateChannel returns state channel of the collector server.
func (col *Collector) GetStateChannel() chan State {
return col.stateChannel
}
// Command returns Collector's root command.
func (col *Collector) Command() *cobra.Command {
return col.rootCmd
}
// GetLogger returns logger used by the Collector.
// The logger is initialized after collector server start.
func (col *Collector) GetLogger() *zap.Logger {
return col.logger
}
// Shutdown shuts down the collector server.
func (col *Collector) Shutdown() {
// TODO: Implement a proper shutdown with graceful draining of the pipeline.
// See https://github.com/open-telemetry/opentelemetry-collector/issues/483.
defer func() {
if r := recover(); r != nil {
col.logger.Info("stopTestChan already closed")
}
}()
close(col.stopTestChan)
}
func (col *Collector) setupTelemetry(ballastSizeBytes uint64) error {
col.logger.Info("Setting up own telemetry...")
err := applicationTelemetry.init(col.asyncErrorChannel, ballastSizeBytes, col.logger)
if err != nil {
return fmt.Errorf("failed to initialize telemetry: %w", err)
}
return nil
}
// runAndWaitForShutdownEvent waits for one of the shutdown events that can happen.
func (col *Collector) runAndWaitForShutdownEvent() {
col.logger.Info("Everything is ready. Begin running and processing data.")
// plug SIGTERM signal into a channel.
col.signalsChannel = make(chan os.Signal, 1)
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM)
// set the channel to stop testing.
col.stopTestChan = make(chan struct{})
col.stateChannel <- Running
select {
case err := <-col.asyncErrorChannel:
col.logger.Error("Asynchronous error received, terminating process", zap.Error(err))
case s := <-col.signalsChannel:
col.logger.Info("Received signal from OS", zap.String("signal", s.String()))
case <-col.stopTestChan:
col.logger.Info("Received stop test request")
}
col.stateChannel <- Closing
}
// setupConfigurationComponents loads the config and starts the components. If all the steps succeeds it
// sets the col.service with the service currently running.
func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
col.logger.Info("Loading configuration...")
cp, err := col.parserProvider.Get()
if err != nil {
return fmt.Errorf("cannot load configuration's parser: %w", err)
}
cfg, err := configloader.Load(cp, col.factories)
if err != nil {
return fmt.Errorf("cannot load configuration: %w", err)
}
if err = cfg.Validate(); err != nil {
return fmt.Errorf("invalid configuration: %w", err)
}
col.logger.Info("Applying configuration...")
service, err := newService(&svcSettings{
BuildInfo: col.info,
Factories: col.factories,
Config: cfg,
Logger: col.logger,
AsyncErrorChannel: col.asyncErrorChannel,
})
if err != nil {
return err
}
err = service.Start(ctx)
if err != nil {
return err
}
col.service = service
// If provider is watchable start a goroutine watching for updates.
if watchable, ok := col.parserProvider.(parserprovider.Watchable); ok {
go func() {
err := watchable.WatchForUpdate()
switch {
// TODO: Move configsource.ErrSessionClosed to providerparser package to avoid depending on configsource.
case errors.Is(err, configsource.ErrSessionClosed):
// This is the case of shutdown of the whole collector server, nothing to do.
col.logger.Info("Config WatchForUpdate closed", zap.Error(err))
return
default:
col.logger.Warn("Config WatchForUpdated exited", zap.Error(err))
col.reloadService(context.Background())
}
}()
}
return nil
}
func (col *Collector) execute(ctx context.Context) error {
col.logger.Info("Starting "+col.info.Command+"...",
zap.String("Version", col.info.Version),
zap.Int("NumCPU", runtime.NumCPU()),
)
col.stateChannel <- Starting
// Set memory ballast
ballast, ballastSizeBytes := col.createMemoryBallast()
col.asyncErrorChannel = make(chan error)
// Setup everything.
err := col.setupTelemetry(ballastSizeBytes)
if err != nil {
return err
}
err = col.setupConfigurationComponents(ctx)
if err != nil {
return err
}
// Everything is ready, now run until an event requiring shutdown happens.
col.runAndWaitForShutdownEvent()
// Accumulate errors and proceed with shutting down remaining components.
var errs []error
// Begin shutdown sequence.
runtime.KeepAlive(ballast)
col.logger.Info("Starting shutdown...")
if closable, ok := col.parserProvider.(parserprovider.Closeable); ok {
if err := closable.Close(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to close config: %w", err))
}
}
if col.service != nil {
if err := col.service.Shutdown(ctx); err != nil {
errs = append(errs, fmt.Errorf("failed to shutdown service: %w", err))
}
}
if err := applicationTelemetry.shutdown(); err != nil {
errs = append(errs, fmt.Errorf("failed to shutdown application telemetry: %w", err))
}
col.logger.Info("Shutdown complete.")
col.stateChannel <- Closed
close(col.stateChannel)
return consumererror.Combine(errs)
}
func (col *Collector) createMemoryBallast() ([]byte, uint64) {
ballastSizeMiB := builder.MemBallastSize()
if ballastSizeMiB > 0 {
ballastSizeBytes := uint64(ballastSizeMiB) * 1024 * 1024
ballast := make([]byte, ballastSizeBytes)
col.logger.Info("Using memory ballast", zap.Int("MiBs", ballastSizeMiB))
return ballast, ballastSizeBytes
}
return nil, 0
}
// reloadService shutdowns the current col.service and setups a new one according
// to the latest configuration. It requires that col.parserProvider and col.factories
// are properly populated to finish successfully.
func (col *Collector) reloadService(ctx context.Context) error {
if closeable, ok := col.parserProvider.(parserprovider.Closeable); ok {
if err := closeable.Close(ctx); err != nil {
return fmt.Errorf("failed close current config provider: %w", err)
}
}
if col.service != nil {
retiringService := col.service
col.service = nil
if err := retiringService.Shutdown(ctx); err != nil {
return fmt.Errorf("failed to shutdown the retiring config: %w", err)
}
}
if err := col.setupConfigurationComponents(ctx); err != nil {
return fmt.Errorf("failed to setup configuration components: %w", err)
}
return nil
}