New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make OTel endpoint optional and use defaults if note set #4
Changes from 1 commit
563630c
b759719
c4a5fd9
e4781fe
22acbd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,7 +155,7 @@ func RegisterOpenTelemetryFlags(flags *pflag.FlagSet, flagPrefix, serviceName st | |
prefixed := prefixJoiner(stringz.DefaultEmpty(flagPrefix, "otel")) | ||
|
||
flags.String(prefixed("provider"), "none", `OpenTelemetry provider for tracing ("none", "jaeger, otlphttp", "otlpgrpc")`) | ||
flags.String(prefixed("endpoint"), "http://collector:14268/api/traces", "collector endpoint") | ||
flags.String(prefixed("endpoint"), "", "OpenTelemetry collector endpoint") | ||
flags.String(prefixed("service-name"), serviceName, "service name for trace data") | ||
} | ||
|
||
|
@@ -173,24 +173,44 @@ func OpenTelemetryRunE(flagPrefix string, prerunLevel zerolog.Level) CobraRunFun | |
|
||
provider := strings.ToLower(MustGetString(cmd, prefixed("provider"))) | ||
serviceName := MustGetString(cmd, prefixed("service-name")) | ||
|
||
endpoint, _ := cmd.Flags().GetString(prefixed("endpoint")) | ||
|
||
var exporter trace.SpanExporter | ||
var err error | ||
|
||
// If endpoint is not set, the clients are configured via the OpenTelemetry environment variables or | ||
// default values. | ||
jzelinskie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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": | ||
exporter, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(MustGetString(cmd, prefixed("endpoint"))))) | ||
if endpoint != "" { | ||
exporter, err = jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(endpoint))) | ||
} else { | ||
exporter, err = jaeger.New(jaeger.WithCollectorEndpoint()) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems fine for now, but in the future when there could be more options, it might make more sense to do something like var opts []jaeger.CollectorEndpointOption
if endpoint != "" {
opts = append(opts, jaeger.WithEndpoint(endpoint))
}
exporter, err := jaeger.New(opts...) This strategy can likely be applied to all of the other providers, too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed in c4a5fd9 |
||
if err != nil { | ||
return err | ||
} | ||
return initOtelTracer(exporter, serviceName) | ||
case "otlphttp": | ||
exporter, err := otlptrace.New(context.Background(), otlptracehttp.NewClient()) | ||
if endpoint != "" { | ||
exporter, err = otlptrace.New(context.Background(), otlptracehttp.NewClient(otlptracehttp.WithEndpoint(endpoint))) | ||
} else { | ||
exporter, err = otlptrace.New(context.Background(), otlptracehttp.NewClient()) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
return initOtelTracer(exporter, serviceName) | ||
case "otlpgrpc": | ||
exporter, err := otlptrace.New(context.Background(), otlptracegrpc.NewClient()) | ||
if endpoint != "" { | ||
exporter, err = otlptrace.New(context.Background(), otlptracegrpc.NewClient(otlptracegrpc.WithEndpoint(endpoint))) | ||
} else { | ||
exporter, err = otlptrace.New(context.Background(), otlptracegrpc.NewClient()) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be
MustGetString(cmd, prefixed("endpoint"))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, updated it so the function uses the same Method to get the Flag value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed this comment. This should not be
MustGetString
, since my intention was to make specifying the endpoint via the command line optional. The reasoning isWithEndpoint()
does not allow configuration of the URL path, so if that is needed, configuration via environment variable is required.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my Test, the
MustGetString
will just render an empty String as that is set as default.