Move tracing to plugin

This just makes the implementation a little cleaner.
It also makes the trace exporter pluggable.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff
2021-09-14 02:03:55 +00:00
parent 6fd80dea34
commit 084387e0b4
6 changed files with 150 additions and 121 deletions

View File

@@ -19,87 +19,17 @@ package tracing
import (
"context"
srvconfig "github.com/containerd/containerd/services/server/config"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
)
// InitOpenTelemetry reads config and initializes otel middleware, sets the exporter
// propagator and global tracer provider
func InitOpenTelemetry(config *srvconfig.Config) (func(), error) {
ctx := context.Background()
// Check if tracing is configured
if config.OpenTelemetry == (srvconfig.OpenTelemetryConfig{}) {
logrus.Info("OpenTelemetry configuration not found, tracing is disabled")
return nil, nil
}
// Validate configuration
if err := config.OpenTelemetry.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid open telemetry configuration")
}
res, err := resource.New(ctx,
resource.WithAttributes(
// Service name used to displace traces in backends
semconv.ServiceNameKey.String(config.OpenTelemetry.ServiceName),
),
)
if err != nil {
return nil, errors.Wrap(err, "failed to create resource")
}
// Configure OTLP trace exporter and set it up to connect to OpenTelemetry collector
// running on a local host.
ctrdTraceExporter, err := otlptracegrpc.New(ctx,
otlptracegrpc.WithEndpoint(config.OpenTelemetry.ExporterEndpoint),
otlptracegrpc.WithDialOption(grpc.WithBlock()),
)
if err != nil {
return nil, errors.Wrap(err, "failed to create trace exporter")
}
// Register the trace exporter with a TracerProvider, using a batch span
// process to aggregate spans before export.
ctrdBatchSpanProcessor := sdktrace.NewBatchSpanProcessor(ctrdTraceExporter)
ctrdTracerProvider := sdktrace.NewTracerProvider(
// We use TraceIDRatioBased sampling. Ratio read from config translated into following
// if sampling ratio < 0 it is interpreted as 0. If ratio >= 1, it will always sample.
sdktrace.WithSampler(sdktrace.TraceIDRatioBased(config.OpenTelemetry.TraceSamplingRatio)),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(ctrdBatchSpanProcessor),
)
otel.SetTracerProvider(ctrdTracerProvider)
// set global propagator to tracecontext
otel.SetTextMapPropagator(propagation.TraceContext{})
return func() {
// Shutdown will flush any remaining spans and shut down the exporter.
err := ctrdTracerProvider.Shutdown(ctx)
if err != nil {
logrus.WithError(err).Errorf("failed to shutdown TracerProvider")
}
}, nil
}
// StartSpan starts child span in a context.
func StartSpan(ctx context.Context, opName string, opts ...trace.SpanStartOption) (trace.Span, context.Context) {
parentSpan := trace.SpanFromContext(ctx)
tracer := trace.NewNoopTracerProvider().Tracer("")
if parentSpan.SpanContext().IsValid() {
tracer = parentSpan.TracerProvider().Tracer("")
if parent := trace.SpanFromContext(ctx); parent != nil && parent.SpanContext().IsValid() {
ctx, span := parent.TracerProvider().Tracer("").Start(ctx, opName, opts...)
return span, ctx
}
ctx, span := tracer.Start(ctx, opName, opts...)
ctx, span := otel.Tracer("").Start(ctx, opName, opts...)
return span, ctx
}