-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathagent.go
More file actions
406 lines (345 loc) · 13.1 KB
/
agent.go
File metadata and controls
406 lines (345 loc) · 13.1 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.
// Package agentimpl contains the implementation of the logs agent component.
package agentimpl
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"time"
"go.uber.org/atomic"
"go.uber.org/fx"
api "github.com/DataDog/datadog-agent/comp/api/api/def"
apiutils "github.com/DataDog/datadog-agent/comp/api/api/utils/stream"
configComponent "github.com/DataDog/datadog-agent/comp/core/config"
flaretypes "github.com/DataDog/datadog-agent/comp/core/flare/types"
"github.com/DataDog/datadog-agent/comp/core/hostname"
log "github.com/DataDog/datadog-agent/comp/core/log/def"
statusComponent "github.com/DataDog/datadog-agent/comp/core/status"
tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
healthplatform "github.com/DataDog/datadog-agent/comp/healthplatform/def"
"github.com/DataDog/datadog-agent/comp/logs/agent"
"github.com/DataDog/datadog-agent/comp/logs/agent/config"
flareController "github.com/DataDog/datadog-agent/comp/logs/agent/flare"
auditor "github.com/DataDog/datadog-agent/comp/logs/auditor/def"
integrations "github.com/DataDog/datadog-agent/comp/logs/integrations/def"
integrationsimpl "github.com/DataDog/datadog-agent/comp/logs/integrations/impl"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent"
logscompression "github.com/DataDog/datadog-agent/comp/serializer/logscompression/def"
"github.com/DataDog/datadog-agent/pkg/config/model"
"github.com/DataDog/datadog-agent/pkg/logs/client"
"github.com/DataDog/datadog-agent/pkg/logs/diagnostic"
"github.com/DataDog/datadog-agent/pkg/logs/launchers"
"github.com/DataDog/datadog-agent/pkg/logs/metrics"
"github.com/DataDog/datadog-agent/pkg/logs/pipeline"
"github.com/DataDog/datadog-agent/pkg/logs/schedulers"
"github.com/DataDog/datadog-agent/pkg/logs/service"
"github.com/DataDog/datadog-agent/pkg/logs/sources"
"github.com/DataDog/datadog-agent/pkg/logs/status"
"github.com/DataDog/datadog-agent/pkg/logs/tailers"
"github.com/DataDog/datadog-agent/pkg/logs/types"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/goroutinesdump"
"github.com/DataDog/datadog-agent/pkg/util/option"
"github.com/DataDog/datadog-agent/pkg/util/startstop"
)
const (
// key used to display a warning message on the agent status
invalidProcessingRules = "invalid_global_processing_rules"
invalidEndpoints = "invalid_endpoints"
invalidFingerprintConfig = "invalid_fingerprint_config"
intakeTrackType = "logs"
// Log messages
multiLineWarning = "multi_line processing rules are not supported as global processing rules."
// inventory setting name
logsTransport = "logs_transport"
)
// Module defines the fx options for this component.
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(newLogsAgent))
}
type dependencies struct {
fx.In
Lc fx.Lifecycle
Log log.Component
Config configComponent.Component
InventoryAgent inventoryagent.Component
Hostname hostname.Component
Auditor auditor.Component
WMeta option.Option[workloadmeta.Component]
SchedulerProviders []schedulers.Scheduler `group:"log-agent-scheduler"`
Tagger tagger.Component
Compression logscompression.Component
HealthPlatform option.Option[healthplatform.Component]
}
type provides struct {
fx.Out
Comp option.Option[agent.Component]
FlareProvider flaretypes.Provider
StatusProvider statusComponent.InformationProvider
LogsReciever option.Option[integrations.Component]
APIStreamLogs api.AgentEndpointProvider
}
// logAgent represents the data pipeline that collects, decodes,
// processes and sends logs to the backend. See the package README for
// a description of its operation.
type logAgent struct {
log log.Component
config model.Reader
inventoryAgent inventoryagent.Component
hostname hostname.Component
tagger tagger.Component
sources *sources.LogSources
services *service.Services
endpoints *config.Endpoints
tracker *tailers.TailerTracker
schedulers *schedulers.Schedulers
auditor auditor.Component
destinationsCtx *client.DestinationsContext
pipelineProvider pipeline.Provider
launchers *launchers.Launchers
diagnosticMessageReceiver *diagnostic.BufferedMessageReceiver
flarecontroller *flareController.FlareController
wmeta option.Option[workloadmeta.Component]
schedulerProviders []schedulers.Scheduler
integrationsLogs integrations.Component
compression logscompression.Component
healthPlatform option.Option[healthplatform.Component]
// make sure this is done only once, when we're ready
prepareSchedulers sync.Once
// started is true if the logs agent is running
started *atomic.Uint32
// make restart thread safe
restartMutex sync.Mutex
// HTTP retry state for TCP fallback recovery
httpRetryCtx context.Context
httpRetryCancel context.CancelFunc
httpRetryMutex sync.Mutex
}
func newLogsAgent(deps dependencies) provides {
if deps.Config.GetBool("logs_enabled") || deps.Config.GetBool("log_enabled") {
if deps.Config.GetBool("log_enabled") {
deps.Log.Warn(`"log_enabled" is deprecated, use "logs_enabled" instead`)
}
integrationsLogs := integrationsimpl.NewLogsIntegration()
logsAgent := &logAgent{
log: deps.Log,
config: deps.Config,
inventoryAgent: deps.InventoryAgent,
hostname: deps.Hostname,
started: atomic.NewUint32(status.StatusNotStarted),
auditor: deps.Auditor,
sources: sources.NewLogSources(),
services: service.NewServices(),
tracker: tailers.NewTailerTracker(),
flarecontroller: flareController.NewFlareController(),
wmeta: deps.WMeta,
schedulerProviders: deps.SchedulerProviders,
integrationsLogs: integrationsLogs,
tagger: deps.Tagger,
compression: deps.Compression,
healthPlatform: deps.HealthPlatform,
}
deps.Lc.Append(fx.Hook{
OnStart: logsAgent.start,
OnStop: logsAgent.stop,
})
return provides{
Comp: option.New[agent.Component](logsAgent),
StatusProvider: statusComponent.NewInformationProvider(NewStatusProvider()),
FlareProvider: flaretypes.NewProvider(logsAgent.flarecontroller.FillFlare),
LogsReciever: option.New[integrations.Component](integrationsLogs),
APIStreamLogs: api.NewAgentEndpointProvider(streamLogsEvents(logsAgent),
"/stream-logs",
"POST",
),
}
}
deps.Log.Info("logs-agent disabled")
return provides{
Comp: option.None[agent.Component](),
StatusProvider: statusComponent.NewInformationProvider(NewStatusProvider()),
LogsReciever: option.None[integrations.Component](),
}
}
func (a *logAgent) start(context.Context) error {
a.restartMutex.Lock()
defer a.restartMutex.Unlock()
a.log.Info("Starting logs-agent...")
// setup the server config
endpoints, err := buildEndpoints(a.config)
if err != nil {
message := fmt.Sprintf("Invalid endpoints: %v", err)
status.AddGlobalError(invalidEndpoints, message)
return errors.New(message)
}
a.endpoints = endpoints
err = a.setupAgent()
if err != nil {
a.log.Error("Could not start logs-agent: ", err)
return err
}
a.startPipeline()
// If we're currently sending over TCP, attempt restart over HTTP
if !endpoints.UseHTTP {
a.smartHTTPRestart()
}
return nil
}
// This is used to switch between transport protocols (TCP to HTTP)
// without disrupting the entire agent.
func (a *logAgent) setupAgent() error {
processingRules, fingerprintConfig, err := a.configureAgent()
if err != nil {
return err
}
a.SetupPipeline(processingRules, a.wmeta, a.integrationsLogs, *fingerprintConfig)
return nil
}
// configureAgent validates and retrieves configuration settings needed for agent operation.
func (a *logAgent) configureAgent() ([]*config.ProcessingRule, *types.FingerprintConfig, error) {
if a.endpoints.UseHTTP {
status.SetCurrentTransport(status.TransportHTTP)
} else {
status.SetCurrentTransport(status.TransportTCP)
}
// The severless agent doesn't use FX for now. This means that the logs agent will not have 'inventoryAgent'
// initialized for serverless. This is ok since metadata is not enabled for serverless.
// TODO: (components) - This condition should be removed once the serverless agent use FX.
if a.inventoryAgent != nil {
a.inventoryAgent.Set(logsTransport, string(status.GetCurrentTransport()))
}
// setup global processing rules
processingRules, err := config.GlobalProcessingRules(a.config)
if err != nil {
message := fmt.Sprintf("Invalid processing rules: %v", err)
status.AddGlobalError(invalidProcessingRules, message)
return nil, nil, errors.New(message)
}
if config.HasMultiLineRule(processingRules) {
a.log.Warn(multiLineWarning)
status.AddGlobalWarning(invalidProcessingRules, multiLineWarning)
}
fingerprintConfig, err := config.GlobalFingerprintConfig(a.config)
if err != nil {
message := fmt.Sprintf("Invalid fingerprint_config setting: %v", err)
status.AddGlobalError(invalidFingerprintConfig, message)
return nil, nil, errors.New(message)
}
return processingRules, fingerprintConfig, nil
}
// Start starts all the elements of the data pipeline
// in the right order to prevent data loss
func (a *logAgent) startPipeline() {
// setup the status
status.Init(a.started, a.endpoints, a.sources, a.tracker, metrics.LogsExpvars)
starter := startstop.NewStarter(
a.destinationsCtx,
a.auditor,
a.pipelineProvider,
a.diagnosticMessageReceiver,
a.launchers,
)
starter.Start()
a.startSchedulers()
}
func (a *logAgent) startSchedulers() {
a.prepareSchedulers.Do(func() {
a.schedulers.Start()
for _, scheduler := range a.schedulerProviders {
a.AddScheduler(scheduler)
}
a.log.Info("logs-agent started")
a.started.Store(status.StatusRunning)
})
}
func (a *logAgent) stop(context.Context) error {
a.restartMutex.Lock()
defer a.restartMutex.Unlock()
a.log.Info("Stopping logs-agent")
// Stop HTTP retry loop if running
a.stopHTTPRetry()
status.Clear()
toStop := []startstop.Stoppable{
a.schedulers,
a.launchers,
a.pipelineProvider,
a.auditor,
a.destinationsCtx,
a.diagnosticMessageReceiver,
}
a.stopComponents(toStop, func() {
a.destinationsCtx.Stop()
})
return nil
}
// stopComponents stops the provided components using SerialStopper with a grace period timeout.
//
// Attempts graceful shutdown within the configured stop_grace_period
// If timeout expires, calls forceClose to force-flush pending data
// 3. Waits 5 seconds for cleanup, then dumps goroutines for debugging and exits
func (a *logAgent) stopComponents(components []startstop.Stoppable, forceClose func()) {
stopper := startstop.NewSerialStopper(components...)
// This will try to stop everything in order, including the potentially blocking
// parts like the sender. After StopTimeout it will just stop the last part of the
// pipeline, disconnecting it from the auditor, to make sure that the pipeline is
// flushed before stopping.
// TODO: Add this feature in the stopper.
c := make(chan struct{})
go func() {
stopper.Stop()
close(c)
}()
timeout := time.Duration(a.config.GetInt("logs_config.stop_grace_period")) * time.Second
select {
case <-c:
a.log.Debug("Components stopped gracefully")
case <-time.After(timeout):
a.log.Info("Timed out when stopping logs-agent, forcing it to stop now")
// We force all destinations to read/flush all the messages they get without
// trying to write to the network.
if forceClose != nil {
forceClose()
}
// Wait again for the stopper to complete.
// In some situation, the stopper unfortunately never succeed to complete,
// we've already reached the grace period, give it some more seconds and
// then force quit.
timeout := time.NewTimer(5 * time.Second)
select {
case <-c:
case <-timeout.C:
a.log.Warn("Force close of the Logs Agent, dumping the Go routines.")
if stack, err := goroutinesdump.Get(); err != nil {
a.log.Warnf("can't get the Go routines dump: %s\n", err)
} else {
a.log.Warn(stack)
}
}
}
a.log.Info("logs-agent stopped")
}
// AddScheduler adds the given scheduler to the agent.
func (a *logAgent) AddScheduler(scheduler schedulers.Scheduler) {
a.schedulers.AddScheduler(scheduler)
}
func (a *logAgent) GetSources() *sources.LogSources {
return a.sources
}
func (a *logAgent) GetMessageReceiver() *diagnostic.BufferedMessageReceiver {
return a.diagnosticMessageReceiver
}
func (a *logAgent) GetPipelineProvider() pipeline.Provider {
return a.pipelineProvider
}
func streamLogsEvents(logsAgent agent.Component) func(w http.ResponseWriter, r *http.Request) {
return apiutils.GetStreamFunc(func() apiutils.MessageReceiver {
return logsAgent.GetMessageReceiver()
}, "logs", "logs agent")
}