Skip to content

Commit

Permalink
Setup otlp from env
Browse files Browse the repository at this point in the history
This allows standard OTLP env vars to be used for configuring tracing
exporters.

Note: This does mean that, as written now, if no env var is set the
trace exporter will try to connect to the default OTLP address
(`localhost:4318`).
I've left this alone for now, but we could detect the OTLP vars
ourselves and if not set don't configure the exporter.
  • Loading branch information
cpuguy83 committed Jun 5, 2023
1 parent 4b7145c commit 50edbf5
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions tracing/plugin/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ func init() {
Config: &OTLPConfig{},
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
cfg := ic.Config.(*OTLPConfig)
if cfg.Endpoint == "" {
return nil, fmt.Errorf("no OpenTelemetry endpoint: %w", plugin.ErrSkipPlugin)
}
exp, err := newExporter(ic.Context, cfg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -121,27 +118,30 @@ func newExporter(ctx context.Context, cfg *OTLPConfig) (*otlptrace.Exporter, err
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

if cfg.Protocol == "http/protobuf" || cfg.Protocol == "" {
u, err := url.Parse(cfg.Endpoint)
if err != nil {
return nil, fmt.Errorf("OpenTelemetry endpoint %q %w : %v", cfg.Endpoint, errdefs.ErrInvalidArgument, err)
}
opts := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(u.Host),
}
if u.Scheme == "http" {
opts = append(opts, otlptracehttp.WithInsecure())
switch cfg.Protocol {
case "", "http/protobuf":
var opts []otlptracehttp.Option
if cfg.Endpoint != "" {
u, err := url.Parse(cfg.Endpoint)
if err != nil {
return nil, fmt.Errorf("OpenTelemetry endpoint %q %w : %v", cfg.Endpoint, errdefs.ErrInvalidArgument, err)
}
opts = append(opts, otlptracehttp.WithEndpoint(u.Host))
if u.Scheme == "http" {
opts = append(opts, otlptracehttp.WithInsecure())
}
}
return otlptracehttp.New(ctx, opts...)
} else if cfg.Protocol == "grpc" {
opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(cfg.Endpoint),
case "grpc":
var opts []otlptracegrpc.Option
if cfg.Endpoint != "" {
opts = append(opts, otlptracegrpc.WithEndpoint(cfg.Endpoint))
}
if cfg.Insecure {
opts = append(opts, otlptracegrpc.WithInsecure())
}
return otlptracegrpc.New(ctx, opts...)
} else {
default:
// Other protocols such as "http/json" are not supported.
return nil, fmt.Errorf("OpenTelemetry protocol %q : %w", cfg.Protocol, errdefs.ErrNotImplemented)
}
Expand All @@ -152,7 +152,6 @@ func newExporter(ctx context.Context, cfg *OTLPConfig) (*otlptrace.Exporter, err
//
// Note that this function sets process-wide tracing configuration.
func newTracer(ctx context.Context, config *TraceConfig, procs []trace.SpanProcessor) (io.Closer, error) {

res, err := resource.New(ctx,
resource.WithHost(),
resource.WithAttributes(
Expand Down

0 comments on commit 50edbf5

Please sign in to comment.