mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
otel: Use non-noop tracer provider for grpc
Needed for Buildkit history
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
(cherry picked from commit d8358ebc87)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
@@ -12,11 +12,11 @@ import (
|
||||
"github.com/containerd/containerd/defaults"
|
||||
"github.com/containerd/log"
|
||||
"github.com/docker/docker/api/server/router"
|
||||
"github.com/docker/docker/internal/otelutil"
|
||||
"github.com/moby/buildkit/util/grpcerrors"
|
||||
"github.com/moby/buildkit/util/stack"
|
||||
"github.com/moby/buildkit/util/tracing"
|
||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||
"go.opentelemetry.io/otel"
|
||||
"golang.org/x/net/http2"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
@@ -29,8 +29,9 @@ type grpcRouter struct {
|
||||
|
||||
// NewRouter initializes a new grpc http router
|
||||
func NewRouter(backends ...Backend) router.Router {
|
||||
tp, _ := otelutil.NewTracerProvider(context.Background(), false)
|
||||
opts := []grpc.ServerOption{
|
||||
grpc.StatsHandler(tracing.ServerStatsHandler(otelgrpc.WithTracerProvider(otel.GetTracerProvider()))),
|
||||
grpc.StatsHandler(tracing.ServerStatsHandler(otelgrpc.WithTracerProvider(tp))),
|
||||
grpc.ChainUnaryInterceptor(unaryInterceptor, grpcerrors.UnaryServerInterceptor),
|
||||
grpc.StreamInterceptor(grpcerrors.StreamServerInterceptor),
|
||||
grpc.MaxRecvMsgSize(defaults.DefaultMaxRecvMsgSize),
|
||||
|
||||
@@ -44,6 +44,7 @@ import (
|
||||
"github.com/docker/docker/daemon/config"
|
||||
"github.com/docker/docker/daemon/listeners"
|
||||
"github.com/docker/docker/dockerversion"
|
||||
"github.com/docker/docker/internal/otelutil"
|
||||
"github.com/docker/docker/libcontainerd/supervisor"
|
||||
dopts "github.com/docker/docker/opts"
|
||||
"github.com/docker/docker/pkg/authorization"
|
||||
@@ -64,10 +65,6 @@ import (
|
||||
"github.com/spf13/pflag"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.opentelemetry.io/otel/trace/noop"
|
||||
"tags.cncf.io/container-device-interface/pkg/cdi"
|
||||
)
|
||||
|
||||
@@ -247,7 +244,7 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
|
||||
// Initialize the trace recorder for buildkit.
|
||||
detect.Recorder = detect.NewTraceRecorder()
|
||||
|
||||
tp, otelShutdown := newTracerProvider(ctx)
|
||||
tp, otelShutdown := otelutil.NewTracerProvider(ctx, true)
|
||||
otel.SetTracerProvider(tp)
|
||||
log.G(ctx).Logger.AddHook(tracing.NewLogrusHook())
|
||||
|
||||
@@ -394,28 +391,6 @@ func setOTLPProtoDefault() {
|
||||
}
|
||||
}
|
||||
|
||||
func newTracerProvider(ctx context.Context) (trace.TracerProvider, func(context.Context) error) {
|
||||
noopShutdown := func(ctx context.Context) error { return nil }
|
||||
|
||||
exp, err := detect.NewSpanExporter(ctx)
|
||||
if err != nil {
|
||||
log.G(ctx).WithError(err).Warn("Failed to initialize tracing, skipping")
|
||||
return noop.NewTracerProvider(), noopShutdown
|
||||
}
|
||||
|
||||
if detect.IsNoneSpanExporter(exp) {
|
||||
log.G(ctx).Info("OTEL tracing is not configured, using no-op tracer provider")
|
||||
return noop.NewTracerProvider(), noopShutdown
|
||||
}
|
||||
|
||||
tp := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithResource(resource.Default()),
|
||||
sdktrace.WithSyncer(detect.Recorder),
|
||||
sdktrace.WithBatcher(exp),
|
||||
)
|
||||
return tp, tp.Shutdown
|
||||
}
|
||||
|
||||
type routerOptions struct {
|
||||
sessionManager *session.Manager
|
||||
buildBackend *buildbackend.Backend
|
||||
|
||||
36
internal/otelutil/provider.go
Normal file
36
internal/otelutil/provider.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package otelutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/containerd/log"
|
||||
"github.com/moby/buildkit/util/tracing/detect"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.opentelemetry.io/otel/trace/noop"
|
||||
)
|
||||
|
||||
func NewTracerProvider(ctx context.Context, allowNoop bool) (trace.TracerProvider, func(context.Context) error) {
|
||||
noopShutdown := func(ctx context.Context) error { return nil }
|
||||
|
||||
exp, err := detect.NewSpanExporter(ctx)
|
||||
if err != nil {
|
||||
log.G(ctx).WithError(err).Warn("Failed to initialize tracing, skipping")
|
||||
if allowNoop {
|
||||
return noop.NewTracerProvider(), noopShutdown
|
||||
}
|
||||
}
|
||||
|
||||
if allowNoop && detect.IsNoneSpanExporter(exp) {
|
||||
log.G(ctx).Info("OTEL tracing is not configured, using no-op tracer provider")
|
||||
return noop.NewTracerProvider(), noopShutdown
|
||||
}
|
||||
|
||||
tp := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithResource(resource.Default()),
|
||||
sdktrace.WithSyncer(detect.Recorder),
|
||||
sdktrace.WithBatcher(exp),
|
||||
)
|
||||
return tp, tp.Shutdown
|
||||
}
|
||||
Reference in New Issue
Block a user