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

Code before:
				case errAPI <- err:
				default:
				}
			}
		}(ls)
	}
	apiWG.Wait()
	close(errAPI)

	c.Cleanup()

	// notify systemd that we're shutting down
	notifyStopping()
	shutdownDaemon(ctx, d)

	if err := routerOpts.buildkit.Close(); err != nil {
		log.G(ctx).WithError(err).Error("Failed to close buildkit")
	}

	// Stop notification processing and any background processes
	cancel()

	if err, ok := <-errAPI; ok {
		return errors.Wrap(err, "shutting down due to ServeAPI error")
	}

	if otelShutdown != nil {
		if err := otelShutdown(context.WithoutCancel(ctx)); err != nil {
			log.G(ctx).WithError(err).Error("Failed to shutdown OTEL tracing")
		}
	}

	log.G(ctx).Info("Daemon shutdown complete")
	return nil
}

// The buildkit "detect" package uses grpc as the default proto, which is in conformance with the old spec.
// For a little while now http/protobuf is the default spec, so this function sets the protocol to http/protobuf when the env var is unset
// so that the detect package will use http/protobuf as a default.
// TODO: This can be removed after buildkit is updated to use http/protobuf as the default.
func setOTLPProtoDefault() {
	const (
		tracesEnv  = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
		metricsEnv = "OTEL_EXPORTER_OTLP_METRICS_PROTOCOL"
		protoEnv   = "OTEL_EXPORTER_OTLP_PROTOCOL"

		defaultProto = "http/protobuf"
	)

	if os.Getenv(protoEnv) == "" {
		if os.Getenv(tracesEnv) == "" {

Review comment (suggestion):
Not important; wondering if we should make `newTracerProvider` always return a "closer" function that's already wired up, so that we can just;

```go
tp, otelShutdown := newTracerProvider(ctx)
defer otelShutdown()
```

Code after:
			}
		}(ls)
	}
	apiWG.Wait()
	close(errAPI)

	c.Cleanup()

	// notify systemd that we're shutting down
	notifyStopping()
	shutdownDaemon(ctx, d)

	if err := routerOpts.buildkit.Close(); err != nil {
		log.G(ctx).WithError(err).Error("Failed to close buildkit")
	}

	// Stop notification processing and any background processes
	cancel()

	if err, ok := <-errAPI; ok {
		return errors.Wrap(err, "shutting down due to ServeAPI error")
	}

	if err := otelShutdown(context.WithoutCancel(ctx)); err != nil {
		log.G(ctx).WithError(err).Error("Failed to shutdown OTEL tracing")
	}

	log.G(ctx).Info("Daemon shutdown complete")
	return nil
}

// The buildkit "detect" package uses grpc as the default proto, which is in conformance with the old spec.
// For a little while now http/protobuf is the default spec, so this function sets the protocol to http/protobuf when the env var is unset
// so that the detect package will use http/protobuf as a default.
// TODO: This can be removed after buildkit is updated to use http/protobuf as the default.
func setOTLPProtoDefault() {
	const (
		tracesEnv  = "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
		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)
		}