forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
281 lines (237 loc) · 8.11 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
// 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.
//go:generate go run ../../pkg/config/render_config.go dogstatsd ../../pkg/config/config_template.yaml ./dist/dogstatsd.yaml
package main
import (
"context"
_ "expvar"
"fmt"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/spf13/cobra"
"github.com/StackVista/stackstate-agent/pkg/aggregator"
"github.com/StackVista/stackstate-agent/pkg/api/healthprobe"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/dogstatsd"
"github.com/StackVista/stackstate-agent/pkg/forwarder"
"github.com/StackVista/stackstate-agent/pkg/metadata"
"github.com/StackVista/stackstate-agent/pkg/serializer"
"github.com/StackVista/stackstate-agent/pkg/status/health"
"github.com/StackVista/stackstate-agent/pkg/tagger"
"github.com/StackVista/stackstate-agent/pkg/tagger/collectors"
"github.com/StackVista/stackstate-agent/pkg/tagger/local"
"github.com/StackVista/stackstate-agent/pkg/util"
"github.com/StackVista/stackstate-agent/pkg/util/log"
"github.com/StackVista/stackstate-agent/pkg/version"
"github.com/StackVista/stackstate-agent/pkg/workloadmeta"
// register all workloadmeta collectors
_ "github.com/StackVista/stackstate-agent/pkg/workloadmeta/collectors"
)
var (
// dogstatsdCmd is the root command
dogstatsdCmd = &cobra.Command{
Use: "dogstatsd [command]",
Short: "Datadog dogstatsd at your service.",
Long: `
DogStatsD accepts custom application metrics points over UDP, and then
periodically aggregates and forwards them to Datadog, where they can be graphed
on dashboards. DogStatsD implements the StatsD protocol, along with a few
extensions for special Datadog features.`,
}
startCmd = &cobra.Command{
Use: "start",
Short: "Start DogStatsD",
Long: `Runs DogStatsD in the foreground`,
RunE: start,
}
versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
av, _ := version.Agent()
fmt.Println(fmt.Sprintf("DogStatsD from Agent %s - Codename: %s - Commit: %s - Serialization version: %s - Go version: %s",
av.GetNumber(), av.Meta, av.Commit, serializer.AgentPayloadVersion, runtime.Version()))
},
}
confPath string
socketPath string
metaScheduler *metadata.Scheduler
statsd *dogstatsd.Server
)
const (
// loggerName is the name of the dogstatsd logger
loggerName config.LoggerName = "DSD"
)
func init() {
// attach the command to the root
dogstatsdCmd.AddCommand(startCmd)
dogstatsdCmd.AddCommand(versionCmd)
// local flags
startCmd.Flags().StringVarP(&confPath, "cfgpath", "c", "", "path to folder containing dogstatsd.yaml")
config.Datadog.BindPFlag("conf_path", startCmd.Flags().Lookup("cfgpath")) //nolint:errcheck
startCmd.Flags().StringVarP(&socketPath, "socket", "s", "", "listen to this socket instead of UDP")
config.Datadog.BindPFlag("dogstatsd_socket", startCmd.Flags().Lookup("socket")) //nolint:errcheck
}
func start(cmd *cobra.Command, args []string) error {
// Main context passed to components
ctx, cancel := context.WithCancel(context.Background())
defer stopAgent(cancel)
stopCh := make(chan struct{})
go handleSignals(stopCh)
err := runAgent(ctx)
if err != nil {
return err
}
// Block here until we receive a stop signal
<-stopCh
return nil
}
func runAgent(ctx context.Context) (err error) {
configFound := false
// a path to the folder containing the config file was passed
if len(confPath) != 0 {
// we'll search for a config file named `dogstatsd.yaml`
config.Datadog.SetConfigName("dogstatsd")
config.Datadog.AddConfigPath(confPath)
_, confErr := config.Load()
if confErr != nil {
log.Error(confErr)
} else {
configFound = true
}
}
if !configFound {
log.Infof("Config will be read from env variables")
}
// Setup logger
syslogURI := config.GetSyslogURI()
logFile := config.Datadog.GetString("log_file")
if logFile == "" {
logFile = defaultLogFile
}
if config.Datadog.GetBool("disable_file_logging") {
// this will prevent any logging on file
logFile = ""
}
err = config.SetupLogger(
loggerName,
config.Datadog.GetString("log_level"),
logFile,
syslogURI,
config.Datadog.GetBool("syslog_rfc"),
config.Datadog.GetBool("log_to_console"),
config.Datadog.GetBool("log_format_json"),
)
if err != nil {
log.Criticalf("Unable to setup logger: %s", err)
return
}
if err := util.SetupCoreDump(); err != nil {
log.Warnf("Can't setup core dumps: %v, core dumps might not be available after a crash", err)
}
if !config.Datadog.IsSet("api_key") {
err = log.Critical("no API key configured, exiting")
return
}
// Setup healthcheck port
var healthPort = config.Datadog.GetInt("health_port")
if healthPort > 0 {
err = healthprobe.Serve(ctx, healthPort)
if err != nil {
err = log.Errorf("Error starting health port, exiting: %v", err)
return
}
log.Debugf("Health check listening on port %d", healthPort)
}
// setup the forwarder
keysPerDomain, err := config.GetMultipleEndpoints()
if err != nil {
log.Error("Misconfiguration of agent endpoints: ", err)
}
f := forwarder.NewDefaultForwarder(forwarder.NewOptions(keysPerDomain))
f.Start() //nolint:errcheck
s := serializer.NewSerializer(f, nil)
hname, err := util.GetHostname(context.TODO())
if err != nil {
log.Warnf("Error getting hostname: %s", err)
hname = ""
}
log.Debugf("Using hostname: %s", hname)
// setup the metadata collector
metaScheduler = metadata.NewScheduler(s)
if err = metadata.SetupMetadataCollection(metaScheduler, []string{"host"}); err != nil {
metaScheduler.Stop()
return
}
if config.Datadog.GetBool("inventories_enabled") {
if err = metadata.SetupInventories(metaScheduler, nil, nil); err != nil {
return
}
}
// container tagging initialisation if origin detection is on
if config.Datadog.GetBool("dogstatsd_origin_detection") {
// Start workload metadata store before tagger
workloadmeta.GetGlobalStore().Start(context.Background())
tagger.SetDefaultTagger(local.NewTagger(collectors.DefaultCatalog))
if err := tagger.Init(); err != nil {
log.Errorf("failed to start the tagger: %s", err)
}
}
aggregatorInstance := aggregator.InitAggregator(s, nil, hname)
statsd, err = dogstatsd.NewServer(aggregatorInstance, nil)
if err != nil {
log.Criticalf("Unable to start dogstatsd: %s", err)
return
}
// send a starting metric and event
aggregatorInstance.AddAgentStartupTelemetry(version.AgentVersion)
return
}
// handleSignals handles OS signals, and sends a message on stopCh when an interrupt
// signal is received.
func handleSignals(stopCh chan struct{}) {
// Setup a channel to catch OS signals
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGPIPE)
// Block here until we receive the interrupt signal
for signo := range signalCh {
switch signo {
case syscall.SIGPIPE:
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
// We never want dogstatsd to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
default:
log.Infof("Received signal '%s', shutting down...", signo)
stopCh <- struct{}{}
return
}
}
}
func stopAgent(cancel context.CancelFunc) {
// retrieve the agent health before stopping the components
// GetReadyNonBlocking has a 100ms timeout to avoid blocking
health, err := health.GetReadyNonBlocking()
if err != nil {
log.Warnf("Dogstatsd health unknown: %s", err)
} else if len(health.Unhealthy) > 0 {
log.Warnf("Some components were unhealthy: %v", health.Unhealthy)
}
// gracefully shut down any component
cancel()
// stop metaScheduler and statsd if they are instantiated
if metaScheduler != nil {
metaScheduler.Stop()
}
if statsd != nil {
statsd.Stop()
}
log.Info("See ya!")
log.Flush()
return
}