-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_flags.go
326 lines (312 loc) · 10.8 KB
/
cli_flags.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
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024 Datadog, Inc.
package main
import (
"context"
"errors"
"fmt"
"os"
"runtime"
"time"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
"go.opentelemetry.io/ebpf-profiler/tracer"
"github.com/DataDog/dd-otel-host-profiler/version"
)
const (
// Default values for CLI flags
defaultArgSamplesPerSecond = 20
defaultArgReporterInterval = 60 * time.Second
defaultArgMonitorInterval = 5.0 * time.Second
defaultClockSyncInterval = 3 * time.Minute
defaultProbabilisticThreshold = tracer.ProbabilisticThresholdMax
defaultProbabilisticInterval = 1 * time.Minute
defaultArgSendErrorFrames = false
defaultArgAgentURL = "http://localhost:8126"
// This is the X in 2^(n + x) where n is the default hardcoded map size value
defaultArgMapScaleFactor = 0
// 1TB of executable address space
maxArgMapScaleFactor = 8
)
type arguments struct {
bpfVerifierLogLevel uint64
agentURL string
copyright bool
mapScaleFactor uint64
monitorInterval time.Duration
clockSyncInterval time.Duration
noKernelVersionCheck bool
node string
probabilisticInterval time.Duration
probabilisticThreshold uint64
reporterInterval time.Duration
samplesPerSecond uint64
pprofPrefix string
sendErrorFrames bool
serviceName string
environment string
uploadSymbols bool
uploadDynamicSymbols bool
uploadSymbolsDryRun bool
tags string
timeline bool
tracers string
verboseMode bool
apiKey string
appKey string
site string
agentless bool
enableGoRuntimeProfiler bool
cmd *cli.Command
}
func parseArgs() (*arguments, error) {
var args arguments
versionInfo := version.GetVersionInfo()
cli.VersionPrinter = func(_ *cli.Command) {
fmt.Printf("dd-otel-host-profiler, version v%s (revision: %s, date: %s), arch: %v\n",
versionInfo.Version, versionInfo.VcsRevision, versionInfo.VcsTime, runtime.GOARCH)
}
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Usage: "print the version",
}
app := cli.Command{
Name: "dd-otel-host-profiler",
Usage: "Datadog OpenTelemetry host profiler",
Copyright: copyright,
Version: versionInfo.Version,
Flags: []cli.Flag{
&cli.UintFlag{
Name: "bpf-log-level",
Value: 0,
Usage: "Log level of the eBPF verifier output (0,1,2).",
Destination: &args.bpfVerifierLogLevel,
Sources: cli.EnvVars("DD_HOST_PROFILING_BPF_LOG_LEVEL"),
},
&cli.StringFlag{
Name: "agent-url",
Aliases: []string{"U"},
Value: defaultArgAgentURL,
Usage: "The Datadog trace agent URL in the format of http://host:port.",
Destination: &args.agentURL,
Sources: cli.EnvVars("DD_HOST_PROFILING_TRACE_AGENT_URL", "DD_TRACE_AGENT_URL"),
},
&cli.StringFlag{
Name: "service",
Aliases: []string{"S"},
Value: "dd-otel-host-profiler",
Usage: "Service name.",
Destination: &args.serviceName,
Sources: cli.EnvVars("DD_HOST_PROFILING_SERVICE", "DD_SERVICE"),
},
&cli.StringFlag{
Name: "environment",
Aliases: []string{"E"},
Usage: "The name of the environment to use in the Datadog UI.",
Destination: &args.environment,
Sources: cli.EnvVars("DD_HOST_PROFILING_ENV", "DD_ENV"),
},
&cli.StringFlag{
Name: "tags",
Usage: "User-specified tags separated by ',': key1:value1,key2:value2.",
Destination: &args.tags,
Sources: cli.EnvVars("DD_HOST_PROFILING_TAGS", "DD_TAGS"),
},
&cli.UintFlag{
Name: "map-scale-factor",
Value: defaultArgMapScaleFactor,
Usage: fmt.Sprintf("Scaling factor for eBPF map sizes. "+
"Every increase by 1 doubles the map size. Increase if you see eBPF map size errors. "+
"Default is %d corresponding to 4GB of executable address space, max is %d.",
defaultArgMapScaleFactor, maxArgMapScaleFactor),
Destination: &args.mapScaleFactor,
Sources: cli.EnvVars("DD_HOST_PROFILING_MAP_SCALE_FACTOR"),
},
&cli.DurationFlag{
Name: "monitor-interval",
Value: defaultArgMonitorInterval,
Usage: "Set the monitor interval in seconds.",
Destination: &args.monitorInterval,
Sources: cli.EnvVars("DD_HOST_PROFILING_MONITOR_INTERVAL"),
},
&cli.DurationFlag{
Name: "clock-sync-interval",
Value: defaultClockSyncInterval,
Usage: "Set the sync interval with the realtime clock. " +
"If zero, monotonic-realtime clock sync will be performed once, " +
"on agent startup, but not periodically.",
Destination: &args.clockSyncInterval,
Sources: cli.EnvVars("DD_HOST_PROFILING_CLOCK_SYNC_INTERVAL"),
},
&cli.BoolFlag{
Name: "no-kernel-version-check",
Value: false,
Usage: "Disable checking kernel version for eBPF support. " +
"Use at your own risk, to run the agent on older kernels with backported eBPF features.",
Destination: &args.noKernelVersionCheck,
Sources: cli.EnvVars("DD_HOST_PROFILING_NO_KERNEL_VERSION_CHECK"),
},
&cli.DurationFlag{
Name: "probabilistic-interval",
Value: defaultProbabilisticInterval,
Usage: "Time interval for which probabilistic profiling will be enabled or disabled.",
Destination: &args.probabilisticInterval,
Sources: cli.EnvVars("DD_HOST_PROFILING_PROBABILISTIC_INTERVAL"),
},
&cli.UintFlag{
Name: "probabilistic-threshold",
Value: defaultProbabilisticThreshold,
Usage: fmt.Sprintf("If set to a value between 1 and %d will enable probabilistic profiling: "+
"every probabilistic-interval a random number between 0 and %d is chosen. "+
"If the given probabilistic-threshold is greater than this "+
"random number, the agent will collect profiles from this system for the duration of the interval.",
tracer.ProbabilisticThresholdMax-1, tracer.ProbabilisticThresholdMax-1),
Destination: &args.probabilisticThreshold,
Sources: cli.EnvVars("DD_HOST_PROFILING_PROBABILISTIC_THRESHOLD"),
},
&cli.DurationFlag{
Name: "upload-period",
Value: defaultArgReporterInterval,
Usage: "Set the reporter's interval in seconds.",
Destination: &args.reporterInterval,
Sources: cli.EnvVars("DD_HOST_PROFILING_UPLOAD_PERIOD"),
},
&cli.UintFlag{
Name: "sampling-rate",
Value: defaultArgSamplesPerSecond,
Usage: "Set the frequency (in Hz) of stack trace sampling.",
Destination: &args.samplesPerSecond,
Sources: cli.EnvVars("DD_HOST_PROFILING_SAMPLING_RATE"),
},
&cli.BoolFlag{
Name: "timeline",
Value: false,
Usage: "Enable timeline feature.",
Destination: &args.timeline,
Sources: cli.EnvVars("DD_HOST_PROFILING_TIMELINE_ENABLED"),
},
&cli.BoolFlag{
Name: "send-error-frames",
Value: defaultArgSendErrorFrames,
Usage: "Send error frames",
Destination: &args.sendErrorFrames,
Sources: cli.EnvVars("DD_HOST_PROFILING_SEND_ERROR_FRAMES"),
},
&cli.StringFlag{
Name: "tracers",
Aliases: []string{"t"},
Value: "all",
Usage: "Comma-separated list of interpreter tracers to include.",
Destination: &args.tracers,
Sources: cli.EnvVars("DD_HOST_PROFILING_TRACERS"),
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "Enable verbose logging and debugging capabilities.",
Destination: &args.verboseMode,
Sources: cli.EnvVars("DD_HOST_PROFILING_VERBOSE"),
},
&cli.StringFlag{
Name: "pprof-prefix",
Usage: "Dump pprof profile to `FILE`.",
Destination: &args.pprofPrefix,
Sources: cli.EnvVars("DD_HOST_PROFILING_PPROF_PREFIX"),
},
&cli.StringFlag{
Name: "node",
Usage: "The name of the node that the profiler is running on. " +
"If on Kubernetes, this must match the Kubernetes node name.",
Destination: &args.node,
Sources: cli.EnvVars("DD_HOST_PROFILING_NODE"),
},
&cli.BoolFlag{
Name: "upload-symbols",
Value: false,
Usage: "Enable local symbol upload.",
Hidden: true,
Destination: &args.uploadSymbols,
Sources: cli.EnvVars("DD_HOST_PROFILING_EXPERIMENTAL_UPLOAD_SYMBOLS"),
},
&cli.BoolFlag{
Name: "upload-dynamic-symbols",
Usage: "Enable dynamic symbols upload.",
Value: true,
Hidden: true,
Sources: cli.EnvVars("DD_HOST_PROFILING_EXPERIMENTAL_UPLOAD_DYNAMIC_SYMBOLS"),
Destination: &args.uploadDynamicSymbols,
},
&cli.BoolFlag{
Name: "upload-symbols-dry-run",
Value: false,
Usage: "Local symbol upload dry-run.",
Hidden: true,
Destination: &args.uploadSymbolsDryRun,
Sources: cli.EnvVars("DD_HOST_PROFILING_EXPERIMENTAL_UPLOAD_SYMBOLS_DRY_RUN"),
},
&cli.StringFlag{
Name: "api-key",
Usage: "Datadog API key.",
Hidden: true,
Destination: &args.apiKey,
Sources: cli.EnvVars("DD_HOST_PROFILING_API_KEY", "DD_API_KEY"),
Validator: func(s string) error {
if s == "" || isAPIKeyValid(s) {
return nil
}
return errors.New("API key is not valid")
},
},
&cli.StringFlag{
Name: "app-key",
Usage: "Datadog APP key.",
Hidden: true,
Destination: &args.appKey,
Sources: cli.EnvVars("DD_HOST_PROFILING_APP_KEY", "DD_APP_KEY"),
Validator: func(s string) error {
if s == "" || isAPPKeyValid(s) {
return nil
}
return errors.New("APP key is not valid")
},
},
&cli.StringFlag{
Name: "site",
Value: "datadoghq.com",
Usage: "Datadog site.",
Hidden: true,
Destination: &args.site,
Sources: cli.EnvVars("DD_HOST_PROFILING_SITE", "DD_SITE"),
},
&cli.BoolFlag{
Name: "profile",
Value: false,
Usage: "Enable self-profiling with Go runtime profiler.",
Destination: &args.enableGoRuntimeProfiler,
Sources: cli.EnvVars("DD_HOST_PROFILING_RUNTIME_PROFILER"),
},
},
Action: func(_ context.Context, cmd *cli.Command) error {
args.cmd = cmd
return nil
},
}
if err := app.Run(context.Background(), os.Args); err != nil {
return nil, err
}
if args.cmd == nil {
return nil, nil
}
return &args, nil
}
func (args *arguments) dump() {
log.Debug("Config:")
for _, f := range args.cmd.Flags {
setStr := "default"
if args.cmd.IsSet(f.Names()[0]) {
setStr = "set"
}
log.Debugf("%s: \"%v\" [%s]", f.Names()[0], args.cmd.Value(f.Names()[0]), setStr)
}
}