Repository: moby/moby
File: daemon/create.go (Go)

Code before:

	return containertypes.CreateResponse{ID: ctr.ID, Warnings: warnings}, nil
}

func labelsAsOTelAttributes(labels map[string]string) []attribute.KeyValue {
	// This env var is a comma-separated list of labels to be included in the
	// OTel span attributes. The labels are prefixed with "label." to avoid
	// collision with other attributes.
	//
	// Note that, this is an experimental env var that might be removed
	// unceremoniously at any point in time.
	filters := strings.Split(os.Getenv("OTEL_FILTER_CONTAINER_LABELS_ATTRS"), ",")
	attrs := make([]attribute.KeyValue, 0, len(filters))
	for k, v := range labels {
		if slices.Contains(filters, k) {
			attrs = append(attrs, attribute.String("label."+k, v))
		}
	}
	return attrs
}

// Create creates a new container from the given configuration with a given name.
func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts createOpts) (retC *container.Container, retErr error) {
	var (
		ctr         *container.Container
		img         *image.Image
		imgManifest *ocispec.Descriptor
		imgID       image.ID
		err         error
		platform    = platforms.DefaultSpec()
	)

	if opts.params.Config.Image != "" {
		img, err = daemon.imageService.GetImage(ctx, opts.params.Config.Image, backend.GetImageOpts{Platform: opts.params.Platform})
		if err != nil {
			return nil, err
		}
		if img.Details != nil {
			imgManifest = img.Details.ManifestDescriptor
		}
		platform = img.Platform()
		imgID = img.ID()
	} else if isWindows {
		platform.OS = "linux" // 'scratch' case.
	}

	// On WCOW, if are not being invoked by the builder to create this container (where
	// ignoreImagesArgEscaped will be true) - if the image already has its arguments escaped,
	// ensure that this is replicated across to the created container to avoid double-escaping
	// of the arguments/command line when the runtime attempts to run the container.
	if platform.OS == "windows" && !opts.ignoreImagesArgsEscaped && img != nil && img.RunConfig().ArgsEscaped {

Review comment (suggestion):
Trivial in the scheme of things so, just a nit, but ...

As `filters` is a slice (presumably a short one, and normally empty), and `labels` is a map that could be any size - maybe look up each filter value in labels, rather than searching filters for each label?

(Maybe also look up and parse the env var once, rather than for each container create and start?)

Code after:

	return containertypes.CreateResponse{ID: ctr.ID, Warnings: warnings}, nil
}

var (
	containerLabelsFilter     []string
	containerLabelsFilterOnce sync.Once
)

func labelsAsOTelAttributes(labels map[string]string) []attribute.KeyValue {
	containerLabelsFilterOnce.Do(func() {
		containerLabelsFilter = strings.Split(os.Getenv("DOCKER_CONTAINER_LABELS_FILTER"), ",")
	})

	// This env var is a comma-separated list of labels to be included in the
	// OTel span attributes. The labels are prefixed with "label." to avoid
	// collision with other attributes.
	//
	// Note that, this is an experimental env var that might be removed
	// unceremoniously at any point in time.
	attrs := make([]attribute.KeyValue, 0, len(containerLabelsFilter))
	for _, k := range containerLabelsFilter {
		if v, ok := labels[k]; ok {
			attrs = append(attrs, attribute.String("label."+k, v))
		}
	}
	return attrs
}

// Create creates a new container from the given configuration with a given name.
func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts createOpts) (retC *container.Container, retErr error) {
	var (
		ctr         *container.Container
		img         *image.Image
		imgManifest *ocispec.Descriptor
		imgID       image.ID
		err         error
		platform    = platforms.DefaultSpec()
	)

	if opts.params.Config.Image != "" {
		img, err = daemon.imageService.GetImage(ctx, opts.params.Config.Image, backend.GetImageOpts{Platform: opts.params.Platform})
		if err != nil {
			return nil, err
		}
		if img.Details != nil {
			imgManifest = img.Details.ManifestDescriptor
		}
		platform = img.Platform()
		imgID = img.ID()
	} else if isWindows {