Repository: moby/moby
File: cmd/dockerd/daemon.go (Go)

Code before:
		metricsEnv = "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL"
		protoEnv   = "OTEL_EXPORTER_OTLP_PROTOCOL"

		defaultProto = "http/protobuf"
	)

	if os.Getenv(protoEnv) == "" {
		if os.Getenv(tracesEnv) == "" {
			_ = os.Setenv(tracesEnv, defaultProto)
		}
		if os.Getenv(metricsEnv) == "" {
			_ = os.Setenv(metricsEnv, defaultProto)
		}
	}
}

func newTracerProvider(ctx context.Context) (trace.TracerProvider, func(context.Context) error) {
	opts := []sdktrace.TracerProviderOption{
		sdktrace.WithResource(resource.Default()),
		sdktrace.WithSyncer(detect.Recorder),
	}

	exp, err := detect.NewSpanExporter(ctx)
	if err != nil {
		log.G(ctx).WithError(err).Warn("Failed to initialize tracing, skipping")
		return noop.NewTracerProvider(), nil
	}

	if detect.IsNoneSpanExporter(exp) {
		log.G(ctx).Info("OTEL tracing is not configured, using no-op tracer provider")
		return noop.NewTracerProvider(), nil
	}
	tp := sdktrace.NewTracerProvider(opts...)
	return tp, tp.Shutdown
}

type routerOptions struct {
	sessionManager *session.Manager
	buildBackend   *buildbackend.Backend
	features       func() map[string]bool
	buildkit       *buildkit.Builder
	daemon         *daemon.Daemon
	cluster        *cluster.Cluster
}

func newRouterOptions(ctx context.Context, cfg *config.Config, d *daemon.Daemon, c *cluster.Cluster) (routerOptions, error) {
	sm, err := session.NewManager()
	if err != nil {
		return routerOptions{}, errors.Wrap(err, "failed to create sessionmanager")
	}


Review comment (performance):
Curious, as the basic issue is quite similar; would it make sense to apply the workaround in the same location as we did for that previous OTEL memory leak, or is it not possible to set them up in the same location? (sorry didn't look in-depth, mostly considering that it would be nice to have such workarounds close together);

- https://github.com/moby/moby/pull/48690

Code after:

	if os.Getenv(protoEnv) == "" {
		if os.Getenv(tracesEnv) == "" {
			_ = os.Setenv(tracesEnv, defaultProto)
		}
		if os.Getenv(metricsEnv) == "" {
			_ = os.Setenv(metricsEnv, defaultProto)
		}
	}
}

type routerOptions struct {
	sessionManager *session.Manager
	buildBackend   *buildbackend.Backend
	features       func() map[string]bool
	buildkit       *buildkit.Builder
	daemon         *daemon.Daemon
	cluster        *cluster.Cluster
}

func newRouterOptions(ctx context.Context, cfg *config.Config, d *daemon.Daemon, c *cluster.Cluster) (routerOptions, error) {
	sm, err := session.NewManager()
	if err != nil {
		return routerOptions{}, errors.Wrap(err, "failed to create sessionmanager")
	}

	manager, err := dockerfile.NewBuildManager(d.BuilderBackend(), d.IdentityMapping())
	if err != nil {
		return routerOptions{}, err
	}

	bk, err := buildkit.New(ctx, buildkit.Opt{
		SessionManager:      sm,
		Root:                filepath.Join(cfg.Root, "buildkit"),
		EngineID:            d.ID(),
		Dist:                d.DistributionServices(),
		ImageTagger:         d.ImageService(),
		NetworkController:   d.NetworkController(),
		DefaultCgroupParent: newCgroupParent(cfg),
		RegistryHosts:       d.RegistryHosts,
		BuilderConfig:       cfg.Builder,
		Rootless:            daemon.Rootless(cfg),
		IdentityMapping:     d.IdentityMapping(),
		DNSConfig:           cfg.DNSConfig,
		ApparmorProfile:     daemon.DefaultApparmorProfile(),
		UseSnapshotter:      d.UsesSnapshotter(),
		Snapshotter:         d.ImageService().StorageDriver(),
		ContainerdAddress:   cfg.ContainerdAddr,
		ContainerdNamespace: cfg.ContainerdNamespace,
		Callbacks: exporter.BuildkitCallbacks{
			Exported: d.ImageExportedByBuildkit,