forked from jzelinskie/cobrautil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cobrautil.go
417 lines (359 loc) · 14.3 KB
/
cobrautil.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
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
407
408
409
410
411
412
413
414
415
416
417
package cobrautil
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"runtime/debug"
"strings"
"time"
"github.com/jzelinskie/stringz"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/contrib/propagators/ot"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
// IsBuiltinCommand checks against a hard-coded list of the names of commands
// that cobra provides out-of-the-box.
func IsBuiltinCommand(cmd *cobra.Command) bool {
return stringz.SliceContains([]string{
"help [command]",
"completion [command]",
},
cmd.Use,
)
}
// SyncViperPreRunE returns a Cobra run func that synchronizes Viper environment
// flags prefixed with the provided argument.
//
// Thanks to Carolyn Van Slyck: https://github.com/carolynvs/stingoftheviper
func SyncViperPreRunE(prefix string) func(cmd *cobra.Command, args []string) error {
prefix = strings.ReplaceAll(strings.ToUpper(prefix), "-", "_")
return func(cmd *cobra.Command, args []string) error {
if IsBuiltinCommand(cmd) {
return nil // No-op for builtins
}
v := viper.New()
viper.SetEnvPrefix(prefix)
cmd.Flags().VisitAll(func(f *pflag.Flag) {
suffix := strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
_ = v.BindEnv(f.Name, prefix+"_"+suffix)
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
_ = cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
}
})
return nil
}
}
// CobraRunFunc is the signature of cobra.Command RunFuncs.
type CobraRunFunc func(cmd *cobra.Command, args []string) error
// RunFuncStack chains together a collection of CobraCommandFuncs into one.
func CommandStack(cmdfns ...CobraRunFunc) CobraRunFunc {
return func(cmd *cobra.Command, args []string) error {
for _, cmdfn := range cmdfns {
if err := cmdfn(cmd, args); err != nil {
return err
}
}
return nil
}
}
func prefixJoiner(prefix string) func(...string) string {
return func(xs ...string) string {
return stringz.Join("-", append([]string{prefix}, xs...)...)
}
}
// RegisterZeroLogFlags adds flags for use in with ZeroLogPreRunE:
// - "$PREFIX-level"
// - "$PREFIX-format"
func RegisterZeroLogFlags(flags *pflag.FlagSet, flagPrefix string) {
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "log"))
flags.String(prefixed("level"), "info", `verbosity of logging ("trace", "debug", "info", "warn", "error")`)
flags.String(prefixed("format"), "auto", `format of logs ("auto", "console", "json")`)
}
// ZeroLogRunE returns a Cobra run func that configures the corresponding
// log level from a command.
//
// The required flags can be added to a command by using
// RegisterLoggingPersistentFlags().
func ZeroLogRunE(flagPrefix string, prerunLevel zerolog.Level) CobraRunFunc {
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "log"))
return func(cmd *cobra.Command, args []string) error {
if IsBuiltinCommand(cmd) {
return nil // No-op for builtins
}
format := MustGetString(cmd, prefixed("format"))
if format == "console" || format == "auto" && isatty.IsTerminal(os.Stdout.Fd()) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
level := strings.ToLower(MustGetString(cmd, prefixed("level")))
switch level {
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "fatal":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "panic":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
default:
return fmt.Errorf("unknown log level: %s", level)
}
log.WithLevel(prerunLevel).Str("new level", level).Msg("set log level")
return nil
}
}
// RegisterOpenTelemetryFlags adds the following flags for use with
// OpenTelemetryPreRunE:
// - "$PREFIX-provider"
// - "$PREFIX-endpoint"
// - "$PREFIX-service-name"
func RegisterOpenTelemetryFlags(flags *pflag.FlagSet, flagPrefix, serviceName string) {
bi, _ := debug.ReadBuildInfo()
serviceName = stringz.DefaultEmpty(serviceName, bi.Main.Path)
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "otel"))
flags.String(prefixed("provider"), "none", `OpenTelemetry provider for tracing ("none", "jaeger, otlphttp", "otlpgrpc")`)
flags.String(prefixed("endpoint"), "", "OpenTelemetry collector endpoint - the endpoint can also be set by using enviroment variables")
flags.String(prefixed("service-name"), serviceName, "service name for trace data")
flags.String(prefixed("trace-propagator"), "w3c", `OpenTelemetry trace propagation format ("b3", "w3c", "ottrace"). Add multiple propagators separated by comma.`)
}
// OpenTelemetryRunE returns a Cobra run func that configures the
// corresponding otel provider from a command.
//
// The required flags can be added to a command by using
// RegisterOpenTelemetryFlags().
func OpenTelemetryRunE(flagPrefix string, prerunLevel zerolog.Level) CobraRunFunc {
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "otel"))
return func(cmd *cobra.Command, args []string) error {
if IsBuiltinCommand(cmd) {
return nil // No-op for builtins
}
provider := strings.ToLower(MustGetString(cmd, prefixed("provider")))
serviceName := MustGetString(cmd, prefixed("service-name"))
endpoint := MustGetString(cmd, prefixed("endpoint"))
propagators := strings.Split(MustGetString(cmd, prefixed("trace-propagator")), ",")
var exporter trace.SpanExporter
var err error
// If endpoint is not set, the clients are configured via the OpenTelemetry environment variables or
// default values.
// See: https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/otlp/otlptrace#environment-variables
// or https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/jaeger#environment-variables
switch provider {
case "none":
// Nothing.
case "jaeger":
var opts []jaeger.CollectorEndpointOption
if endpoint != "" {
opts = append(opts, jaeger.WithEndpoint(endpoint))
}
exporter, err = jaeger.New(jaeger.WithCollectorEndpoint(opts...))
if err != nil {
return err
}
return initOtelTracer(exporter, serviceName, propagators)
case "otlphttp":
var opts []otlptracehttp.Option
if endpoint != "" {
opts = append(opts, otlptracehttp.WithEndpoint(endpoint))
}
exporter, err = otlptrace.New(context.Background(), otlptracehttp.NewClient(opts...))
if err != nil {
return err
}
return initOtelTracer(exporter, serviceName, propagators)
case "otlpgrpc":
var opts []otlptracegrpc.Option
if endpoint != "" {
opts = append(opts, otlptracegrpc.WithEndpoint(endpoint))
}
exporter, err = otlptrace.New(context.Background(), otlptracegrpc.NewClient(opts...))
if err != nil {
return err
}
return initOtelTracer(exporter, serviceName, propagators)
default:
return fmt.Errorf("unknown tracing provider: %s", provider)
}
log.WithLevel(prerunLevel).Str("new provider", provider).Msg("set tracing provider")
return nil
}
}
func initOtelTracer(exporter trace.SpanExporter, serviceName string, propagators []string) error {
res, _ := resource.Merge(
resource.Default(),
resource.NewSchemaless(semconv.ServiceNameKey.String(serviceName)),
)
tp := trace.NewTracerProvider(
trace.WithSampler(trace.AlwaysSample()),
trace.WithBatcher(exporter),
trace.WithResource(res),
)
otel.SetTracerProvider(tp)
setTracePropagators(propagators)
return nil
}
// setTextMapPropagator sets the OpenTelemetry trace propagation format.
// Currently it supports b3, ot-trace and w3c.
func setTracePropagators(propagators []string) {
var tmPropagators []propagation.TextMapPropagator
for _, p := range propagators {
switch p {
case "b3":
tmPropagators = append(tmPropagators, b3.New())
case "ottrace":
tmPropagators = append(tmPropagators, ot.OT{})
case "w3c":
fallthrough
default:
tmPropagators = append(tmPropagators, propagation.Baggage{}) // W3C baggage support
tmPropagators = append(tmPropagators, propagation.TraceContext{}) // W3C for compatibility with other tracing system
}
}
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(tmPropagators...))
}
// RegisterGrpcServerFlags adds the following flags for use with
// GrpcServerFromFlags:
// - "$PREFIX-addr"
// - "$PREFIX-tls-cert-path"
// - "$PREFIX-tls-key-path"
// - "$PREFIX-max-conn-age"
func RegisterGrpcServerFlags(flags *pflag.FlagSet, flagPrefix, serviceName, defaultAddr string, defaultEnabled bool) {
serviceName = stringz.DefaultEmpty(serviceName, "grpc")
defaultAddr = stringz.DefaultEmpty(defaultAddr, ":50051")
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "grpc"))
flags.String(prefixed("addr"), defaultAddr, "address to listen on to serve "+serviceName)
flags.String(prefixed("network"), "tcp", "network type to serve "+serviceName+` ("tcp", "tcp4", "tcp6", "unix", "unixpacket")`)
flags.String(prefixed("tls-cert-path"), "", "local path to the TLS certificate used to serve "+serviceName)
flags.String(prefixed("tls-key-path"), "", "local path to the TLS key used to serve "+serviceName)
flags.Duration(prefixed("max-conn-age"), 30*time.Second, "how long a connection serving "+serviceName+" should be able to live")
flags.Bool(prefixed("enabled"), defaultEnabled, "enable "+serviceName+" gRPC server")
}
// GrpcServerFromFlags creates an *grpc.Server as configured by the flags from
// RegisterGrpcServerFlags().
func GrpcServerFromFlags(cmd *cobra.Command, flagPrefix string, opts ...grpc.ServerOption) (*grpc.Server, error) {
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "grpc"))
opts = append(opts, grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionAge: MustGetDuration(cmd, prefixed("max-conn-age")),
}))
certPath := MustGetStringExpanded(cmd, prefixed("tls-cert-path"))
keyPath := MustGetStringExpanded(cmd, prefixed("tls-key-path"))
switch {
case certPath == "" && keyPath == "":
log.Warn().Str("prefix", flagPrefix).Msg("grpc server serving plaintext")
return grpc.NewServer(opts...), nil
case certPath != "" && keyPath != "":
creds, err := credentials.NewServerTLSFromFile(certPath, keyPath)
if err != nil {
return nil, err
}
opts = append(opts, grpc.Creds(creds))
return grpc.NewServer(opts...), nil
default:
return nil, fmt.Errorf(
"failed to start gRPC server: must provide both --%s-tls-cert-path and --%s-tls-key-path",
flagPrefix,
flagPrefix,
)
}
}
// GrpcListenFromFlags listens on an gRPC server using the configuration stored
// in the cobra command that was registered with RegisterGrpcServerFlags.
func GrpcListenFromFlags(cmd *cobra.Command, flagPrefix string, srv *grpc.Server, level zerolog.Level) error {
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "grpc"))
if !MustGetBool(cmd, prefixed("enabled")) {
return nil
}
network := MustGetString(cmd, prefixed("network"))
addr := MustGetStringExpanded(cmd, prefixed("addr"))
l, err := net.Listen(network, addr)
if err != nil {
return fmt.Errorf("failed to listen on addr for gRPC server: %w", err)
}
log.WithLevel(level).
Str("addr", addr).
Str("network", network).
Str("prefix", flagPrefix).
Msg("grpc server started listening")
if err := srv.Serve(l); err != nil {
return fmt.Errorf("failed to serve gRPC: %w", err)
}
return nil
}
// RegisterHTTPServerFlags adds the following flags for use with
// HttpServerFromFlags:
// - "$PREFIX-addr"
// - "$PREFIX-tls-cert-path"
// - "$PREFIX-tls-key-path"
// - "$PREFIX-enabled"
func RegisterHTTPServerFlags(flags *pflag.FlagSet, flagPrefix, serviceName, defaultAddr string, defaultEnabled bool) {
serviceName = stringz.DefaultEmpty(serviceName, "http")
defaultAddr = stringz.DefaultEmpty(defaultAddr, ":8443")
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "http"))
flags.String(prefixed("addr"), defaultAddr, "address to listen on to serve "+serviceName)
flags.String(prefixed("tls-cert-path"), "", "local path to the TLS certificate used to serve "+serviceName)
flags.String(prefixed("tls-key-path"), "", "local path to the TLS key used to serve "+serviceName)
flags.Bool(prefixed("enabled"), defaultEnabled, "enable "+serviceName+" http server")
}
// HTTPServerFromFlags creates an *http.Server as configured by the flags from
// RegisterHttpServerFlags().
func HTTPServerFromFlags(cmd *cobra.Command, flagPrefix string) *http.Server {
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "http"))
return &http.Server{
Addr: MustGetStringExpanded(cmd, prefixed("addr")),
}
}
// HTTPListenFromFlags listens on an HTTP server using the configuration stored
// in the cobra command that was registered with RegisterHttpServerFlags.
func HTTPListenFromFlags(cmd *cobra.Command, flagPrefix string, srv *http.Server, level zerolog.Level) error {
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "http"))
if !MustGetBool(cmd, prefixed("enabled")) {
return nil
}
certPath := MustGetStringExpanded(cmd, prefixed("tls-cert-path"))
keyPath := MustGetStringExpanded(cmd, prefixed("tls-key-path"))
switch {
case certPath == "" && keyPath == "":
log.Warn().Str("addr", srv.Addr).Str("prefix", flagPrefix).Msg("http server serving plaintext")
if err := srv.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("failed while serving http: %w", err)
}
return nil
case certPath != "" && keyPath != "":
log.WithLevel(level).Str("addr", srv.Addr).Str("prefix", flagPrefix).Msg("https server started serving")
if err := srv.ListenAndServeTLS(certPath, keyPath); err != nil && errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("failed while serving https: %w", err)
}
return nil
default:
return fmt.Errorf(
"failed to start http server: must provide both --%s-tls-cert-path and --%s-tls-key-path",
flagPrefix,
flagPrefix,
)
}
}