-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
service.go
381 lines (317 loc) · 10.7 KB
/
service.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2019, 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"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector/config"
"github.com/open-telemetry/opentelemetry-collector/config/configcheck"
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
"github.com/open-telemetry/opentelemetry-collector/extension"
"github.com/open-telemetry/opentelemetry-collector/receiver"
"github.com/open-telemetry/opentelemetry-collector/service/builder"
)
// Application represents a collector application
type Application struct {
info ApplicationStartInfo
rootCmd *cobra.Command
v *viper.Viper
logger *zap.Logger
exporters builder.Exporters
builtReceivers builder.Receivers
factories config.Factories
config *configmodels.Config
extensions []extension.ServiceExtension
// stopTestChan is used to terminate the application in end to end tests.
stopTestChan chan struct{}
// readyChan is used in tests to indicate that the application is ready.
readyChan chan struct{}
// asyncErrorChannel is used to signal a fatal error from any component.
asyncErrorChannel chan error
}
// ApplicationStartInfo is the information that is logged at the application start.
// This information can be overridden in custom builds.
type ApplicationStartInfo struct {
// Executable file name, e.g. "otelcol".
ExeName string
// Long name, used e.g. in the logs.
LongName string
// Version string.
Version string
// Git hash of the source code.
GitHash string
}
var _ receiver.Host = (*Application)(nil)
// Context returns a context provided by the host to be used on the receiver
// operations.
func (app *Application) Context() context.Context {
// For now simply the background context.
return context.Background()
}
// New creates and returns a new instance of Application.
func New(
factories config.Factories,
appInfo ApplicationStartInfo,
) (*Application, error) {
if err := configcheck.ValidateConfigFromFactories(factories); err != nil {
return nil, err
}
app := &Application{
info: appInfo,
v: viper.New(),
readyChan: make(chan struct{}),
factories: factories,
}
rootCmd := &cobra.Command{
Use: appInfo.ExeName,
Long: appInfo.LongName,
Run: func(cmd *cobra.Command, args []string) {
app.init()
app.execute()
},
}
// TODO: coalesce this code and expose this information to other components.
flagSet := new(flag.FlagSet)
addFlagsFns := []func(*flag.FlagSet){
telemetryFlags,
builder.Flags,
loggerFlags,
}
for _, addFlags := range addFlagsFns {
addFlags(flagSet)
}
rootCmd.Flags().AddGoFlagSet(flagSet)
app.rootCmd = rootCmd
return app, nil
}
// ReportFatalError is used to report to the host that the receiver encountered
// a fatal error (i.e.: an error that the instance can't recover from) after
// its start function has already returned.
func (app *Application) ReportFatalError(err error) {
app.asyncErrorChannel <- err
}
func (app *Application) init() {
file := builder.GetConfigFile()
if file == "" {
log.Fatalf("Config file not specified")
}
app.v.SetConfigFile(file)
err := app.v.ReadInConfig()
if err != nil {
log.Fatalf("Error loading config file %q: %v", file, err)
}
app.logger, err = newLogger()
if err != nil {
log.Fatalf("Failed to get logger: %v", err)
}
}
func (app *Application) setupTelemetry(ballastSizeBytes uint64) {
app.logger.Info("Setting up own telemetry...")
err := AppTelemetry.init(app.asyncErrorChannel, ballastSizeBytes, app.logger)
if err != nil {
app.logger.Error("Failed to initialize telemetry", zap.Error(err))
os.Exit(1)
}
}
// runAndWaitForShutdownEvent waits for one of the shutdown events that can happen.
func (app *Application) runAndWaitForShutdownEvent() {
app.logger.Info("Everything is ready. Begin running and processing data.")
// Plug SIGTERM signal into a channel.
signalsChannel := make(chan os.Signal, 1)
signal.Notify(signalsChannel, os.Interrupt, syscall.SIGTERM)
// set the channel to stop testing.
app.stopTestChan = make(chan struct{})
// notify tests that it is ready.
close(app.readyChan)
select {
case err := <-app.asyncErrorChannel:
app.logger.Error("Asynchronous error received, terminating process", zap.Error(err))
case s := <-signalsChannel:
app.logger.Info("Received signal from OS", zap.String("signal", s.String()))
case <-app.stopTestChan:
app.logger.Info("Received stop test request")
}
}
func (app *Application) setupConfigurationComponents() {
// Load configuration.
app.logger.Info("Loading configuration...")
cfg, err := config.Load(app.v, app.factories, app.logger)
if err != nil {
log.Fatalf("Cannot load configuration: %v", err)
}
app.config = cfg
app.logger.Info("Applying configuration...")
if err := app.setupExtensions(); err != nil {
log.Fatalf("Cannot setup extensions: %v", err)
}
app.setupPipelines()
}
func (app *Application) setupExtensions() error {
for _, extName := range app.config.Service.Extensions {
extCfg, exists := app.config.Extensions[extName]
if !exists {
return fmt.Errorf("extension %q is not configured", extName)
}
factory, exists := app.factories.Extensions[extCfg.Type()]
if !exists {
return fmt.Errorf("extension factory for type %q is not configured", extCfg.Type())
}
ext, err := factory.CreateExtension(app.logger, extCfg)
if err != nil {
return fmt.Errorf("failed to create extension %q: %v", extName, err)
}
// Check if the factory really created the extension.
if ext == nil {
return fmt.Errorf("factory for %q produced a nil extension", extName)
}
if err := ext.Start(app); err != nil {
return fmt.Errorf("error starting extension %q: %v", extName, err)
}
app.extensions = append(app.extensions, ext)
}
return nil
}
func (app *Application) setupPipelines() {
// Pipeline is built backwards, starting from exporters, so that we create objects
// which are referenced before objects which reference them.
// First create exporters.
var err error
app.exporters, err = builder.NewExportersBuilder(app.logger, app.config, app.factories.Exporters).Build()
if err != nil {
log.Fatalf("Cannot build exporters: %v", err)
}
app.logger.Info("Starting exporters...")
err = app.exporters.StartAll(app.logger, app)
if err != nil {
log.Fatalf("Cannot start exporters: %v", err)
}
// Create pipelines and their processors and plug exporters to the
// end of the pipelines.
pipelines, err := builder.NewPipelinesBuilder(app.logger, app.config, app.exporters, app.factories.Processors).Build()
if err != nil {
log.Fatalf("Cannot build pipelines: %v", err)
}
// Create receivers and plug them into the start of the pipelines.
app.builtReceivers, err = builder.NewReceiversBuilder(app.logger, app.config, pipelines, app.factories.Receivers).Build()
if err != nil {
log.Fatalf("Cannot build receivers: %v", err)
}
app.logger.Info("Starting receivers...")
err = app.builtReceivers.StartAll(app.logger, app)
if err != nil {
log.Fatalf("Cannot start receivers: %v", err)
}
}
func (app *Application) notifyPipelineReady() {
for i, ext := range app.extensions {
if pw, ok := ext.(extension.PipelineWatcher); ok {
if err := pw.Ready(); err != nil {
log.Fatalf(
"Error notifying extension %q that the pipeline was started: %v",
app.config.Service.Extensions[i],
err,
)
}
}
}
}
func (app *Application) notifyPipelineNotReady() {
// Notify on reverse order.
for i := len(app.extensions) - 1; i >= 0; i-- {
ext := app.extensions[i]
if pw, ok := ext.(extension.PipelineWatcher); ok {
if err := pw.NotReady(); err != nil {
app.logger.Warn(
"Error notifying extension that the pipeline was shutdown",
zap.Error(err),
zap.String("extension", app.config.Service.Extensions[i]),
)
}
}
}
}
func (app *Application) shutdownPipelines() {
// Shutdown order is the reverse of building: first receivers, then flushing pipelines
// giving senders a chance to send all their data. This may take time, the allowed
// time should be part of configuration.
app.logger.Info("Stopping receivers...")
app.builtReceivers.StopAll()
// TODO: shutdown processors by calling Shutdown() for each processor in the
// order they are arranged in the pipeline.
app.logger.Info("Shutting down exporters...")
app.exporters.ShutdownAll()
}
func (app *Application) shutdownExtensions() {
// Shutdown on reverse order.
for i := len(app.extensions) - 1; i >= 0; i-- {
ext := app.extensions[i]
if err := ext.Shutdown(); err != nil {
app.logger.Warn(
"Error shutting down extension",
zap.Error(err),
zap.String("extension", app.config.Service.Extensions[i]),
)
}
}
}
func (app *Application) execute() {
app.logger.Info("Starting "+app.info.LongName+"...",
zap.String("Version", app.info.Version),
zap.String("GitHash", app.info.GitHash),
zap.Int("NumCPU", runtime.NumCPU()),
)
// Set memory ballast
ballast, ballastSizeBytes := app.createMemoryBallast()
app.asyncErrorChannel = make(chan error)
// Setup everything.
app.setupTelemetry(ballastSizeBytes)
app.setupConfigurationComponents()
app.notifyPipelineReady()
// Everything is ready, now run until an event requiring shutdown happens.
app.runAndWaitForShutdownEvent()
// Begin shutdown sequence.
runtime.KeepAlive(ballast)
app.logger.Info("Starting shutdown...")
app.notifyPipelineNotReady()
app.shutdownPipelines()
app.shutdownExtensions()
AppTelemetry.shutdown()
app.logger.Info("Shutdown complete.")
}
// Start starts the collector according to the command and configuration
// given by the user.
func (app *Application) Start() error {
return app.rootCmd.Execute()
}
func (app *Application) createMemoryBallast() ([]byte, uint64) {
ballastSizeMiB := builder.MemBallastSize()
if ballastSizeMiB > 0 {
ballastSizeBytes := uint64(ballastSizeMiB) * 1024 * 1024
ballast := make([]byte, ballastSizeBytes)
app.logger.Info("Using memory ballast", zap.Int("MiBs", ballastSizeMiB))
return ballast, ballastSizeBytes
}
return nil, 0
}