forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
340 lines (287 loc) · 10.6 KB
/
main.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
// 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 2016-present Datadog, Inc.
package main
import (
_ "expvar"
"fmt"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/StackVista/stackstate-agent/cmd/agent/common"
"github.com/StackVista/stackstate-agent/pkg/autodiscovery"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/logs"
logConfig "github.com/StackVista/stackstate-agent/pkg/logs/config"
"github.com/StackVista/stackstate-agent/pkg/serializer"
"github.com/StackVista/stackstate-agent/pkg/serverless"
"github.com/StackVista/stackstate-agent/pkg/serverless/daemon"
"github.com/StackVista/stackstate-agent/pkg/serverless/flush"
"github.com/StackVista/stackstate-agent/pkg/serverless/metrics"
"github.com/StackVista/stackstate-agent/pkg/serverless/registration"
"github.com/StackVista/stackstate-agent/pkg/serverless/trace"
"github.com/StackVista/stackstate-agent/pkg/util/flavor"
"github.com/StackVista/stackstate-agent/pkg/util/log"
"github.com/StackVista/stackstate-agent/pkg/version"
)
var (
// serverlessAgentCmd is the root command
serverlessAgentCmd = &cobra.Command{
Use: "agent [command]",
Short: "Serverless Datadog Agent at your service.",
Long: `
Datadog Serverless Agent accepts custom application metrics points over UDP, aggregates and forwards them to Datadog,
where they can be graphed on dashboards. The Datadog Serverless Agent implements the StatsD protocol, along with a few extensions for special Datadog features.`,
}
runCmd = &cobra.Command{
Use: "run",
Short: "Runs the Serverless Datadog Agent",
Long: `Runs the Serverless Datadog Agent`,
RunE: run,
}
versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
av, _ := version.Agent()
fmt.Printf("Serverless Datadog Agent %s - Codename: %s - Commit: %s - Serialization version: %s - Go version: %s\n",
av.GetNumber(), av.Meta, av.Commit, serializer.AgentPayloadVersion, runtime.Version())
},
}
kmsAPIKeyEnvVar = "DD_KMS_API_KEY"
secretsManagerAPIKeyEnvVar = "DD_API_KEY_SECRET_ARN"
apiKeyEnvVar = "DD_API_KEY"
logLevelEnvVar = "DD_LOG_LEVEL"
flushStrategyEnvVar = "DD_SERVERLESS_FLUSH_STRATEGY"
logsLogsTypeSubscribed = "DD_LOGS_CONFIG_LAMBDA_LOGS_TYPE"
// AWS Lambda is writing the Lambda function files in /var/task, we want the
// configuration file to be at the root of this directory.
datadogConfigPath = "/var/task/datadog.yaml"
)
const (
loggerName config.LoggerName = "DD_EXTENSION"
runtimeAPIEnvVar = "AWS_LAMBDA_RUNTIME_API"
extensionRegistrationRoute = "/2020-01-01/extension/register"
extensionRegistrationTimeout = 5 * time.Second
// httpServerAddr will be the default addr used to run the HTTP server listening
// to calls from the client libraries and to logs from the AWS environment.
httpServerAddr = ":8124"
logsAPIRegistrationRoute = "/2020-08-15/logs"
logsAPIRegistrationTimeout = 5 * time.Second
logsAPIHttpServerPort = 8124
logsAPICollectionRoute = "/lambda/logs"
logsAPITimeout = 25
logsAPIMaxBytes = 262144
logsAPIMaxItems = 1000
)
func init() {
// attach the command to the root
serverlessAgentCmd.AddCommand(runCmd)
serverlessAgentCmd.AddCommand(versionCmd)
}
func run(cmd *cobra.Command, args []string) error {
stopCh := make(chan struct{})
// run the agent
serverlessDaemon, err := runAgent(stopCh)
if err != nil {
return err
}
// handle SIGTERM
go handleSignals(serverlessDaemon, stopCh)
// block here until we receive a stop signal
<-stopCh
return nil
}
func main() {
flavor.SetFlavor(flavor.ServerlessAgent)
// if not command has been provided, run the agent
if len(os.Args) == 1 {
os.Args = append(os.Args, "run")
}
if err := serverlessAgentCmd.Execute(); err != nil {
log.Error(err)
os.Exit(-1)
}
}
func runAgent(stopCh chan struct{}) (serverlessDaemon *daemon.Daemon, err error) {
startTime := time.Now()
// setup logger
// -----------
// init the logger configuring it to not log in a file (the first empty string)
if err = config.SetupLogger(
loggerName,
"error", // will be re-set later with the value from the env var
"", // logFile -> by setting this to an empty string, we don't write the logs to any file
"", // syslog URI
false, // syslog_rfc
true, // log_to_console
false, // log_format_json
); err != nil {
log.Errorf("Unable to setup logger: %s", err)
}
if logLevel := os.Getenv(logLevelEnvVar); len(logLevel) > 0 {
if err := config.ChangeLogLevel(logLevel); err != nil {
log.Errorf("While changing the loglevel: %s", err)
}
}
// immediately starts the communication server
serverlessDaemon = daemon.StartDaemon(httpServerAddr)
err = serverlessDaemon.RestoreCurrentStateFromFile()
if err != nil {
log.Debug("Unable to restore the state from file")
} else {
serverlessDaemon.ComputeGlobalTags(config.GetConfiguredTags(true))
}
// serverless parts
// ----------------
// extension registration
serverlessID, err := registration.RegisterExtension(os.Getenv(runtimeAPIEnvVar), extensionRegistrationRoute, extensionRegistrationTimeout)
if err != nil {
// at this point, we were not even able to register, thus, we don't have
// any ID assigned, thus, we can't report an error to the init error route
// which needs an Id.
log.Errorf("Can't register as a serverless agent: %s", err)
return
}
// api key reading
// ---------------
// API key reading priority:
// KSM > Secrets Manager > Plaintext API key
// If one is set but failing, the next will be tried
// some useful warnings first
var apikeySetIn = []string{}
if os.Getenv(kmsAPIKeyEnvVar) != "" {
apikeySetIn = append(apikeySetIn, "KMS")
}
if os.Getenv(secretsManagerAPIKeyEnvVar) != "" {
apikeySetIn = append(apikeySetIn, "SSM")
}
if os.Getenv(apiKeyEnvVar) != "" {
apikeySetIn = append(apikeySetIn, "environment variable")
}
if len(apikeySetIn) > 1 {
log.Warn("An API Key has been set in multiple places:", strings.Join(apikeySetIn, ", "))
}
// try to read API key from KMS
var apiKey string
if apiKey, err = readAPIKeyFromKMS(); err != nil {
log.Errorf("Error while trying to read an API Key from KMS: %s", err)
} else if apiKey != "" {
log.Info("Using deciphered KMS API Key.")
os.Setenv(apiKeyEnvVar, apiKey)
}
// try to read the API key from Secrets Manager, only if not set from KMS
if apiKey == "" {
if apiKey, err = readAPIKeyFromSecretsManager(); err != nil {
log.Errorf("Error while trying to read an API Key from Secrets Manager: %s", err)
} else if apiKey != "" {
log.Info("Using API key set in Secrets Manager.")
os.Setenv(apiKeyEnvVar, apiKey)
}
}
// adaptive flush configuration
if v, exists := os.LookupEnv(flushStrategyEnvVar); exists {
if flushStrategy, err := flush.StrategyFromString(v); err != nil {
log.Debugf("Invalid flush strategy %s, will use adaptive flush instead. Err: %s", v, err)
} else {
serverlessDaemon.UseAdaptiveFlush(false) // we're forcing the flush strategy, we won't be using the adaptive flush
serverlessDaemon.SetFlushStrategy(flushStrategy)
}
} else {
serverlessDaemon.UseAdaptiveFlush(true) // already initialized to true, but let's be explicit just in case
}
// validate that an apikey has been set, either by the env var, read from KMS or Secrets Manager.
// ---------------------------
if !config.Datadog.IsSet("api_key") {
// we're not reporting the error to AWS because we don't want the function
// execution to be stopped. TODO(remy): discuss with AWS if there is way
// of reporting non-critical init errors.
// serverless.ReportInitError(serverlessID, serverless.FatalNoAPIKey)
log.Error("No API key configured, exiting")
}
logChannel := make(chan *logConfig.ChannelMessage)
metricAgent := &metrics.ServerlessMetricAgent{}
metricAgent.Start(daemon.FlushTimeout, &metrics.MetricConfig{}, &metrics.MetricDogStatsD{})
serverlessDaemon.SetStatsdServer(metricAgent)
serverlessDaemon.SetupLogCollectionHandler(logsAPICollectionRoute, logChannel, config.Datadog.GetBool("serverless.logs_enabled"), config.Datadog.GetBool("enhanced_metrics"))
wg := sync.WaitGroup{}
wg.Add(1)
wg.Add(1)
// starts trace agent
go func() {
defer wg.Done()
traceAgent := &trace.ServerlessTraceAgent{}
traceAgent.Start(config.Datadog.GetBool("apm_config.enabled"), &trace.LoadConfig{Path: datadogConfigPath})
serverlessDaemon.SetTraceAgent(traceAgent)
}()
// enable logs collection
go func() {
defer wg.Done()
log.Debug("Enabling logs collection HTTP route")
logRegistrationURL := registration.BuildURL(os.Getenv(runtimeAPIEnvVar), logsAPIRegistrationRoute)
logRegistrationError := registration.EnableLogsCollection(
serverlessID,
logRegistrationURL,
logsAPIRegistrationTimeout,
os.Getenv(logsLogsTypeSubscribed),
logsAPIHttpServerPort,
logsAPICollectionRoute,
logsAPITimeout,
logsAPIMaxBytes,
logsAPIMaxItems)
if logRegistrationError != nil {
log.Error("Can't subscribe to logs:", logRegistrationError)
} else {
setupLogAgent(logChannel)
}
}()
wg.Wait()
// run the invocation loop in a routine
// we don't want to start this mainloop before because once we're waiting on
// the invocation route, we can't report init errors anymore.
go func() {
for {
if err := serverless.WaitForNextInvocation(stopCh, serverlessDaemon, serverlessID); err != nil {
log.Error(err)
}
}
}()
// this log line is used for performance checks during CI
// please be careful before modifying/removing it
log.Debugf("serverless agent ready in %v", time.Since(startTime))
return
}
// handleSignals handles OS signals, if a SIGTERM is received,
// the serverless agent stops.
func handleSignals(serverlessDaemon *daemon.Daemon, stopCh chan struct{}) {
// setup a channel to catch OS signals
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
// block here until we receive the interrupt signal
// when received, shutdown the serverless agent.
for signo := range signalCh {
switch signo {
default:
log.Infof("Received signal '%s', shutting down...", signo)
serverlessDaemon.Stop()
stopCh <- struct{}{}
return
}
}
}
func setupLogAgent(logChannel chan *logConfig.ChannelMessage) {
if err := logs.StartServerless(
func() *autodiscovery.AutoConfig { return common.AC },
logChannel, nil,
); err != nil {
log.Error("Could not start an instance of the Logs Agent:", err)
}
}