Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 29 additions & 32 deletions tracing/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/els0r/telemetry/tracing/internal/flagutil"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
Expand All @@ -21,23 +20,20 @@ var supportedCollectors = []string{
stdout,
}

// Tracing flag names and their help text. The '.' is the hierarchy delimiter
// Tracing flag names. The '.' is the hierarchy delimiter.
const (
TracingKey = "tracing"

TracingEnabledArg string = TracingKey + ".enabled"
TracingEnabledDefault bool = false
TracingEnabledHelp string = "enable tracing"
TracingEnabledArg string = TracingKey + ".enabled"
TracingEnabledHelp string = "enable tracing"

TracingCollectorKey string = TracingKey + ".collector"

TracingCollectorTypeArg string = TracingCollectorKey + ".type"
TracingCollectorTypeDefault string = ""
TracingCollectorTypeHelp string = "tracing collector type"
TracingCollectorTypeArg string = TracingCollectorKey + ".type"
TracingCollectorTypeHelp string = "tracing collector type"

TracingCollectorEndpointArg string = TracingCollectorKey + ".endpoint"
TracingCollectorEndpointDefault string = ""
TracingCollectorEndpointHelp string = "endpoint collecting traces"
TracingCollectorEndpointArg string = TracingCollectorKey + ".endpoint"
TracingCollectorEndpointHelp string = "endpoint collecting traces"
)

type traceFlagsConfig struct {
Expand All @@ -47,38 +43,36 @@ type traceFlagsConfig struct {
}

func (t traceFlagsConfig) options(ctx context.Context) (opts []Option) {
if !(t.enabled) {
if !t.enabled {
return opts
}

switch t.collectorType {
case stdout:
opts = append(opts, WithStdoutTraceExporter(true))
case otelGRPC:
opts = append(opts, WithGRPCExporter(ctx, traceFlags.collectorEndpoint))
opts = append(opts, WithGRPCExporter(ctx, t.collectorEndpoint))
default:
return opts
}
return opts
}

var traceFlags traceFlagsConfig

var registered bool
var registration = sync.Once{}
var (
registeredFlags *flag.FlagSet
registered bool
registration = sync.Once{}
)

// RegisterFlags registers CLI arguments into flags. It accepts the Flagger interface, allowing for the use of pflag
// or other flag libraries such as cobra. Registration of the flags is done exactly once
// RegisterFlags registers CLI arguments into flags. Registration of the flags is done exactly once.
func RegisterFlags(flags *flag.FlagSet) {
registration.Do(func() {
registered = true
registeredFlags = flags

flags.Bool(TracingEnabledArg, TracingEnabledDefault, TracingEnabledHelp)
flags.String(TracingCollectorTypeArg, TracingCollectorTypeDefault, flagutil.WithSupported(TracingCollectorTypeHelp, supportedCollectors))
flags.String(TracingCollectorEndpointArg, TracingCollectorEndpointDefault, TracingCollectorEndpointHelp)

// bind the flags to make sure they can be read from other sources (such as env and config files)
_ = viper.BindPFlags(flags)
flags.Bool(TracingEnabledArg, false, TracingEnabledHelp)
flags.String(TracingCollectorTypeArg, "", flagutil.WithSupported(TracingCollectorTypeHelp, supportedCollectors))
flags.String(TracingCollectorEndpointArg, "", TracingCollectorEndpointHelp)
})
}

Expand All @@ -87,18 +81,21 @@ type ShutdownFunc func(context.Context) error

var noShutdown ShutdownFunc = func(context.Context) error { return nil }

// InitFromFlags initializes tracing from the set of registered flags. Replaces the Init method
// InitFromFlags initializes tracing from the set of registered flags. Replaces the Init method.
func InitFromFlags(ctx context.Context) (ShutdownFunc, error) {
if !registered {
if !registered || registeredFlags == nil {
fmt.Fprintf(os.Stderr, "CLI flags have not been registered. Use RegisterFlags in your command definition. Defaulting to no tracing\n") //revive:disable-line
return noShutdown, nil
}

// access them through viper once to make sure their value is loaded
traceFlags = traceFlagsConfig{
enabled: viper.GetBool(TracingEnabledArg),
collectorType: viper.GetString(TracingCollectorTypeArg),
collectorEndpoint: viper.GetString(TracingCollectorEndpointArg),
enabled, _ := registeredFlags.GetBool(TracingEnabledArg)
collectorType, _ := registeredFlags.GetString(TracingCollectorTypeArg)
collectorEndpoint, _ := registeredFlags.GetString(TracingCollectorEndpointArg)

traceFlags := traceFlagsConfig{
enabled: enabled,
collectorType: collectorType,
collectorEndpoint: collectorEndpoint,
}

opts := traceFlags.options(ctx)
Expand Down
16 changes: 0 additions & 16 deletions tracing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ toolchain go1.22.4

require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/contrib/propagators/b3 v1.30.0
go.opentelemetry.io/otel v1.30.0
Expand All @@ -20,34 +19,19 @@ require (
require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading