-
Notifications
You must be signed in to change notification settings - Fork 882
/
flags_misc.go
258 lines (222 loc) · 6.11 KB
/
flags_misc.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
package cmd
import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"strings"
otelpyroscope "github.com/grafana/otel-profiling-go"
logging "github.com/ipfs/go-log/v2"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"github.com/celestiaorg/celestia-node/logs"
"github.com/celestiaorg/celestia-node/nodebuilder"
modp2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)
var (
LogLevelFlag = "log.level"
LogLevelModuleFlag = "log.level.module"
pprofFlag = "pprof"
tracingFlag = "tracing"
tracingEndpointFlag = "tracing.endpoint"
tracingTlS = "tracing.tls"
metricsFlag = "metrics"
metricsEndpointFlag = "metrics.endpoint"
metricsTlS = "metrics.tls"
p2pMetrics = "p2p.metrics"
pyroscopeFlag = "pyroscope"
pyroscopeTracing = "pyroscope.tracing"
pyroscopeEndpoint = "pyroscope.endpoint"
)
// MiscFlags gives a set of hardcoded miscellaneous flags.
func MiscFlags() *flag.FlagSet {
flags := &flag.FlagSet{}
flags.String(
LogLevelFlag,
"INFO",
`DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL
and their lower-case forms`,
)
flags.StringSlice(
LogLevelModuleFlag,
nil,
"<module>:<level>, e.g. pubsub:debug",
)
flags.Bool(
pprofFlag,
false,
"Enables standard profiling handler (pprof) and exposes the profiles on port 6000",
)
flags.Bool(
tracingFlag,
false,
"Enables OTLP tracing with HTTP exporter",
)
flags.String(
tracingEndpointFlag,
"localhost:4318",
"Sets HTTP endpoint for OTLP traces to be exported to. Depends on '--tracing'",
)
flags.Bool(
tracingTlS,
true,
"Enable TLS connection to OTLP tracing backend",
)
flags.Bool(
metricsFlag,
false,
"Enables OTLP metrics with HTTP exporter",
)
flags.String(
metricsEndpointFlag,
"localhost:4318",
"Sets HTTP endpoint for OTLP metrics to be exported to. Depends on '--metrics'",
)
flags.Bool(
metricsTlS,
true,
"Enable TLS connection to OTLP metric backend",
)
flags.Bool(
p2pMetrics,
false,
"Enable libp2p metrics",
)
flags.Bool(
pyroscopeFlag,
false,
"Enables Pyroscope profiling",
)
flags.Bool(
pyroscopeTracing,
false,
"Enables Pyroscope tracing integration. Depends on --tracing",
)
flags.String(
pyroscopeEndpoint,
"http://localhost:4040",
"Sets HTTP endpoint for Pyroscope profiles to be exported to. Depends on '--pyroscope'",
)
return flags
}
// ParseMiscFlags parses miscellaneous flags from the given cmd and applies values to Env.
func ParseMiscFlags(ctx context.Context, cmd *cobra.Command) (context.Context, error) {
logLevel := cmd.Flag(LogLevelFlag).Value.String()
if logLevel != "" {
level, err := logging.LevelFromString(logLevel)
if err != nil {
return ctx, fmt.Errorf("cmd: while parsing '%s': %w", LogLevelFlag, err)
}
logs.SetAllLoggers(level)
}
logModules, err := cmd.Flags().GetStringSlice(LogLevelModuleFlag)
if err != nil {
panic(err)
}
for _, ll := range logModules {
params := strings.Split(ll, ":")
if len(params) != 2 {
return ctx, fmt.Errorf("cmd: %s arg must be in form <module>:<level>, e.g. pubsub:debug", LogLevelModuleFlag)
}
err := logging.SetLogLevel(params[0], params[1])
if err != nil {
return ctx, err
}
}
ok, err := cmd.Flags().GetBool(pprofFlag)
if err != nil {
panic(err)
}
if ok {
// TODO(@Wondertan): Eventually, this should be registered on http server in RPC
// by passing the http.Server with preregistered pprof handlers to the node.
// Node should not register pprof itself.
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
err := http.ListenAndServe("0.0.0.0:6000", mux) //nolint:gosec
if err != nil {
log.Fatalw("failed to start pprof server", "err", err)
} else {
log.Info("started pprof server on port 6000")
}
}()
}
ok, err = cmd.Flags().GetBool(pyroscopeFlag)
if err != nil {
panic(err)
}
if ok {
ctx = WithNodeOptions(ctx,
nodebuilder.WithPyroscope(
cmd.Flag(pyroscopeEndpoint).Value.String(),
NodeType(ctx),
),
)
}
ok, err = cmd.Flags().GetBool(tracingFlag)
if err != nil {
panic(err)
}
if ok {
opts := []otlptracehttp.Option{
otlptracehttp.WithCompression(otlptracehttp.GzipCompression),
otlptracehttp.WithEndpoint(cmd.Flag(tracingEndpointFlag).Value.String()),
}
if ok, err := cmd.Flags().GetBool(tracingTlS); err != nil {
panic(err)
} else if !ok {
opts = append(opts, otlptracehttp.WithInsecure())
}
pyroOpts := make([]otelpyroscope.Option, 0)
ok, err = cmd.Flags().GetBool(pyroscopeTracing)
if err != nil {
panic(err)
}
if ok {
pyroOpts = append(pyroOpts,
otelpyroscope.WithAppName("celestia.da-node"),
otelpyroscope.WithPyroscopeURL(cmd.Flag(pyroscopeEndpoint).Value.String()),
otelpyroscope.WithRootSpanOnly(true),
otelpyroscope.WithAddSpanName(true),
otelpyroscope.WithProfileURL(true),
otelpyroscope.WithProfileBaselineURL(true),
)
}
ctx = WithNodeOptions(ctx, nodebuilder.WithTraces(opts, pyroOpts))
}
ok, err = cmd.Flags().GetBool(metricsFlag)
if err != nil {
panic(err)
}
if ok {
opts := []otlpmetrichttp.Option{
otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression),
otlpmetrichttp.WithEndpoint(cmd.Flag(metricsEndpointFlag).Value.String()),
}
if ok, err := cmd.Flags().GetBool(metricsTlS); err != nil {
panic(err)
} else if !ok {
opts = append(opts, otlpmetrichttp.WithInsecure())
}
ctx = WithNodeOptions(ctx, nodebuilder.WithMetrics(opts, NodeType(ctx)))
}
ok, err = cmd.Flags().GetBool(p2pMetrics)
if err != nil {
panic(err)
}
if ok {
if metricsEnabled, _ := cmd.Flags().GetBool(metricsFlag); !metricsEnabled {
log.Error("--p2p.metrics used without --metrics being enabled")
} else {
ctx = WithNodeOptions(ctx, modp2p.WithMetrics())
}
}
return ctx, err
}