-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
collector.go
292 lines (244 loc) · 8.84 KB
/
collector.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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package service handles the command-line, configuration, and runs the
// OpenTelemetry Collector.
package otelcol // import "go.opentelemetry.io/collector/otelcol"
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"sync/atomic"
"syscall"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/otelcol/internal/grpclog"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/service"
)
// State defines Collector's state.
type State int
const (
StateStarting State = iota
StateRunning
StateClosing
StateClosed
)
func (s State) String() string {
switch s {
case StateStarting:
return "Starting"
case StateRunning:
return "Running"
case StateClosing:
return "Closing"
case StateClosed:
return "Closed"
}
return "UNKNOWN"
}
// CollectorSettings holds configuration for creating a new Collector.
type CollectorSettings struct {
// Factories service factories.
Factories Factories
// BuildInfo provides collector start information.
BuildInfo component.BuildInfo
// DisableGracefulShutdown disables the automatic graceful shutdown
// of the collector on SIGINT or SIGTERM.
// Users who want to handle signals themselves can disable this behavior
// and manually handle the signals to shutdown the collector.
DisableGracefulShutdown bool
// ConfigProvider provides the service configuration.
// If the provider watches for configuration change, collector may reload the new configuration upon changes.
ConfigProvider ConfigProvider
// LoggingOptions provides a way to change behavior of zap logging.
LoggingOptions []zap.Option
// SkipSettingGRPCLogger avoids setting the grpc logger
SkipSettingGRPCLogger bool
}
// (Internal note) Collector Lifecycle:
// - New constructs a new Collector.
// - Run starts the collector.
// - Run calls setupConfigurationComponents to handle configuration.
// If configuration parser fails, collector's config can be reloaded.
// Collector can be shutdown if parser gets a shutdown error.
// - Run runs runAndWaitForShutdownEvent and waits for a shutdown event.
// SIGINT and SIGTERM, errors, and (*Collector).Shutdown can trigger the shutdown events.
// - Upon shutdown, pipelines are notified, then pipelines and extensions are shut down.
// - Users can call (*Collector).Shutdown anytime to shut down the collector.
// Collector represents a server providing the OpenTelemetry Collector service.
type Collector struct {
set CollectorSettings
service *service.Service
state *atomic.Int32
// shutdownChan is used to terminate the collector.
shutdownChan 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
}
// NewCollector creates and returns a new instance of Collector.
func NewCollector(set CollectorSettings) (*Collector, error) {
if set.ConfigProvider == nil {
return nil, errors.New("invalid nil config provider")
}
state := &atomic.Int32{}
state.Store(int32(StateStarting))
return &Collector{
set: set,
state: state,
shutdownChan: make(chan struct{}),
// Per signal.Notify documentation, a size of the channel equaled with
// the number of signals getting notified on is recommended.
signalsChannel: make(chan os.Signal, 3),
asyncErrorChannel: make(chan error),
}, nil
}
// GetState returns current state of the collector server.
func (col *Collector) GetState() State {
return State(col.state.Load())
}
// Shutdown shuts down the collector server.
func (col *Collector) Shutdown() {
// Only shutdown if we're in a Running or Starting State else noop
state := col.GetState()
if state == StateRunning || state == StateStarting {
defer func() {
recover() // nolint:errcheck
}()
close(col.shutdownChan)
}
}
// 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.setCollectorState(StateStarting)
var conf *confmap.Conf
if cp, ok := col.set.ConfigProvider.(ConfmapProvider); ok {
var err error
conf, err = cp.GetConfmap(ctx)
if err != nil {
return fmt.Errorf("failed to resolve config: %w", err)
}
}
cfg, err := col.set.ConfigProvider.Get(ctx, col.set.Factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}
if err = cfg.Validate(); err != nil {
return fmt.Errorf("invalid configuration: %w", err)
}
col.service, err = service.New(ctx, service.Settings{
BuildInfo: col.set.BuildInfo,
CollectorConf: conf,
Receivers: receiver.NewBuilder(cfg.Receivers, col.set.Factories.Receivers),
Processors: processor.NewBuilder(cfg.Processors, col.set.Factories.Processors),
Exporters: exporter.NewBuilder(cfg.Exporters, col.set.Factories.Exporters),
Connectors: connector.NewBuilder(cfg.Connectors, col.set.Factories.Connectors),
Extensions: extension.NewBuilder(cfg.Extensions, col.set.Factories.Extensions),
AsyncErrorChannel: col.asyncErrorChannel,
LoggingOptions: col.set.LoggingOptions,
}, cfg.Service)
if err != nil {
return err
}
if !col.set.SkipSettingGRPCLogger {
grpclog.SetLogger(col.service.Logger(), cfg.Service.Telemetry.Logs.Level)
}
if err = col.service.Start(ctx); err != nil {
return multierr.Combine(err, col.service.Shutdown(ctx))
}
col.setCollectorState(StateRunning)
return nil
}
func (col *Collector) reloadConfiguration(ctx context.Context) error {
col.service.Logger().Warn("Config updated, restart service")
col.setCollectorState(StateClosing)
if err := col.service.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
}
func (col *Collector) DryRun(ctx context.Context) error {
cfg, err := col.set.ConfigProvider.Get(ctx, col.set.Factories)
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}
return cfg.Validate()
}
// Run starts the collector according to the given configuration, and waits for it to complete.
// Consecutive calls to Run are not allowed, Run shouldn't be called once a collector is shut down.
func (col *Collector) Run(ctx context.Context) error {
if err := col.setupConfigurationComponents(ctx); err != nil {
col.setCollectorState(StateClosed)
return err
}
// Always notify with SIGHUP for configuration reloading.
signal.Notify(col.signalsChannel, syscall.SIGHUP)
defer signal.Stop(col.signalsChannel)
// Only notify with SIGTERM and SIGINT if graceful shutdown is enabled.
if !col.set.DisableGracefulShutdown {
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM)
}
LOOP:
for {
select {
case err := <-col.set.ConfigProvider.Watch():
if err != nil {
col.service.Logger().Error("Config watch failed", zap.Error(err))
break LOOP
}
if err = col.reloadConfiguration(ctx); err != nil {
return err
}
case err := <-col.asyncErrorChannel:
col.service.Logger().Error("Asynchronous error received, terminating process", zap.Error(err))
break LOOP
case s := <-col.signalsChannel:
col.service.Logger().Info("Received signal from OS", zap.String("signal", s.String()))
if s != syscall.SIGHUP {
break LOOP
}
if err := col.reloadConfiguration(ctx); err != nil {
return err
}
case <-col.shutdownChan:
col.service.Logger().Info("Received shutdown request")
break LOOP
case <-ctx.Done():
col.service.Logger().Info("Context done, terminating process", zap.Error(ctx.Err()))
// Call shutdown with background context as the passed in context has been canceled
return col.shutdown(context.Background())
}
}
return col.shutdown(ctx)
}
func (col *Collector) shutdown(ctx context.Context) error {
col.setCollectorState(StateClosing)
// Accumulate errors and proceed with shutting down remaining components.
var errs error
if err := col.set.ConfigProvider.Shutdown(ctx); err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown config provider: %w", err))
}
// shutdown service
if err := col.service.Shutdown(ctx); err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown service after error: %w", err))
}
col.setCollectorState(StateClosed)
return errs
}
// setCollectorState provides current state of the collector
func (col *Collector) setCollectorState(state State) {
col.state.Store(int32(state))
}