Skip to content

Commit

Permalink
Changes to configuring otel from env only
Browse files Browse the repository at this point in the history
These are standard environment variables described by the otel spec in
https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/.

The old config options are removed

Also since otel will by default try to connect to https://localhost:4318
if no endpoint is set, this will also just disable the otlp plugin when
there is no endpoint so we don't have otel continuously trying to
connect to the default endpoint, littering the logs with connection
failure messages and collecting traces that won't go anywhere.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
(cherry picked from commit 4fbc984)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
  • Loading branch information
cpuguy83 authored and vvoland committed Mar 25, 2024
1 parent f235489 commit 9360e37
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 199 deletions.
166 changes: 85 additions & 81 deletions tracing/plugin/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"context"
"fmt"
"io"
"net/url"
"os"
"strconv"
"time"

"github.com/containerd/containerd/errdefs"
Expand All @@ -35,13 +36,25 @@ import (
"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.21.0"
)

const exporterPlugin = "otlp"

// OTEL and OTLP standard env vars
// See https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/
const (
sdkDisabledEnv = "OTEL_SDK_DISABLED"

otlpEndpointEnv = "OTEL_EXPORTER_OTLP_ENDPOINT"
otlpTracesEndpointEnv = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"
otlpProtocolEnv = "OTEL_EXPORTER_OTLP_PROTOCOL"
otlpTracesProtocolEnv = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"

otelTracesExporterEnv = "OTEL_TRACES_EXPORTER"
otelServiceNameEnv = "OTEL_SERVICE_NAME"
)

func init() {
plugin.Register(&plugin.Registration{
ID: exporterPlugin,
Expand All @@ -51,27 +64,38 @@ func init() {
if err := warnOTLPConfig(ic); err != nil {
return nil, err
}
cfg := ic.Config.(*OTLPConfig)
if cfg.Endpoint == "" {
return nil, fmt.Errorf("no OpenTelemetry endpoint: %w", plugin.ErrSkipPlugin)
if err := checkDisabled(); err != nil {
return nil, err
}
exp, err := newExporter(ic.Context, cfg)

// If OTEL_TRACES_EXPORTER is set, it must be "otlp"
if v := os.Getenv(otelTracesExporterEnv); v != "" && v != "otlp" {
return nil, fmt.Errorf("unsupported traces exporter %q: %w", v, errdefs.ErrInvalidArgument)
}

exp, err := newExporter(ic.Context)
if err != nil {
return nil, err
}
return trace.NewBatchSpanProcessor(exp), nil
},
})
plugin.Register(&plugin.Registration{
ID: "tracing",
Type: plugin.InternalPlugin,
Requires: []plugin.Type{plugin.TracingProcessorPlugin},
Config: &TraceConfig{ServiceName: "containerd", TraceSamplingRatio: 1.0},
ID: "tracing",
Type: plugin.InternalPlugin,
Config: &TraceConfig{},
Requires: []plugin.Type{
plugin.TracingProcessorPlugin,
},
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
//get TracingProcessorPlugin which is a dependency
if err := warnTraceConfig(ic); err != nil {
return nil, err
}
if err := checkDisabled(); err != nil {
return nil, err
}

//get TracingProcessorPlugin which is a dependency
plugins, err := ic.GetByType(plugin.TracingProcessorPlugin)
if err != nil {
return nil, fmt.Errorf("failed to get tracing processors: %w", err)
Expand All @@ -90,7 +114,7 @@ func init() {
proc := p.(trace.SpanProcessor)
procs = append(procs, proc)
}
return newTracer(ic.Context, ic.Config.(*TraceConfig), procs)
return newTracer(ic.Context, procs)
},
})

Expand All @@ -100,108 +124,88 @@ func init() {

// OTLPConfig holds the configurations for the built-in otlp span processor
type OTLPConfig struct {
Endpoint string `toml:"endpoint"`
Protocol string `toml:"protocol"`
Insecure bool `toml:"insecure"`
Endpoint string `toml:"endpoint,omitempty"`
Protocol string `toml:"protocol,omitempty"`
Insecure bool `toml:"insecure,omitempty"`
}

// TraceConfig is the common configuration for open telemetry.
type TraceConfig struct {
ServiceName string `toml:"service_name"`
TraceSamplingRatio float64 `toml:"sampling_ratio"`
ServiceName string `toml:"service_name,omitempty"`
TraceSamplingRatio float64 `toml:"sampling_ratio,omitempty"`
}

type closer struct {
close func() error
func checkDisabled() error {
v := os.Getenv(sdkDisabledEnv)
if v != "" {
disable, err := strconv.ParseBool(v)
if err != nil {
return fmt.Errorf("invalid value for %s: %w: %w", sdkDisabledEnv, err, errdefs.ErrInvalidArgument)
}
if disable {
return fmt.Errorf("%w: tracing disabled by env %s=%s", plugin.ErrSkipPlugin, sdkDisabledEnv, v)
}
}

if os.Getenv(otlpEndpointEnv) == "" && os.Getenv(otlpTracesEndpointEnv) == "" {
return fmt.Errorf("%w: tracing endpoint not configured", plugin.ErrSkipPlugin)
}
return nil
}

func (c *closer) Close() error {
return c.close()
type closerFunc func() error

func (f closerFunc) Close() error {
return f()
}

// newExporter creates an exporter based on the given configuration.
//
// The default protocol is http/protobuf since it is recommended by
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/protocol/exporter.md#specify-protocol.
func newExporter(ctx context.Context, cfg *OTLPConfig) (*otlptrace.Exporter, error) {
func newExporter(ctx context.Context) (*otlptrace.Exporter, error) {
const timeout = 5 * time.Second

v := os.Getenv(otlpTracesProtocolEnv)
if v == "" {
v = os.Getenv(otlpProtocolEnv)
}

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())
}
return otlptracehttp.New(ctx, opts...)
} else if cfg.Protocol == "grpc" {
opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(cfg.Endpoint),
}
if cfg.Insecure {
opts = append(opts, otlptracegrpc.WithInsecure())
}
return otlptracegrpc.New(ctx, opts...)
switch v {
case "", "http/protobuf":
return otlptracehttp.New(ctx)
case "grpc":
return otlptracegrpc.New(ctx)
default:
// Other protocols such as "http/json" are not supported.
return nil, fmt.Errorf("OpenTelemetry protocol %q : %w", v, errdefs.ErrNotImplemented)
}
// Other protocols such as "http/json" are not supported.
return nil, fmt.Errorf("OpenTelemetry protocol %q : %w", cfg.Protocol, errdefs.ErrNotImplemented)
}

// newTracer configures protocol-agonostic tracing settings such as
// its sampling ratio and returns io.Closer.
//
// 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(
// Service name used to displace traces in backends
semconv.ServiceNameKey.String(config.ServiceName),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
func newTracer(ctx context.Context, procs []trace.SpanProcessor) (io.Closer, error) {
// Let otel configure the service name from env
if os.Getenv(otelServiceNameEnv) == "" {
os.Setenv(otelServiceNameEnv, "containerd")
}

sampler := trace.ParentBased(trace.TraceIDRatioBased(config.TraceSamplingRatio))

opts := []trace.TracerProviderOption{
trace.WithSampler(sampler),
trace.WithResource(res),
}
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))

opts := make([]trace.TracerProviderOption, 0, len(procs))
for _, proc := range procs {
opts = append(opts, trace.WithSpanProcessor(proc))
}

provider := trace.NewTracerProvider(opts...)

otel.SetTracerProvider(provider)

otel.SetTextMapPropagator(propagators())

return &closer{close: func() error {
for _, p := range procs {
if err := p.Shutdown(ctx); err != nil {
return err
}
}
return nil
}}, nil

}

// Returns a composite TestMap propagator
func propagators() propagation.TextMapPropagator {
return propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
return closerFunc(func() error {
return provider.Shutdown(ctx)
}), nil
}

func warnTraceConfig(ic *plugin.InitContext) error {
Expand Down
118 changes: 0 additions & 118 deletions tracing/plugin/otlp_test.go

This file was deleted.

0 comments on commit 9360e37

Please sign in to comment.