From 617eb5271ac08dbefcef1ce44998b7dc1e6aab47 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 24 Jun 2024 14:49:29 +0200 Subject: [PATCH] cli: make initializing the global meter- and tracing providers optional Signed-off-by: Sebastiaan van Stijn --- cli-plugins/plugin/plugin.go | 1 + cli/command/cli.go | 10 ++++++++-- cli/command/telemetry_options.go | 25 +++++++++++++++++++++++++ cmd/docker/docker.go | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 cli/command/telemetry_options.go diff --git a/cli-plugins/plugin/plugin.go b/cli-plugins/plugin/plugin.go index d4f38d74cd..8cce73d9ac 100644 --- a/cli-plugins/plugin/plugin.go +++ b/cli-plugins/plugin/plugin.go @@ -45,6 +45,7 @@ func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager if os.Getenv("DOCKER_CLI_PLUGIN_USE_DIAL_STDIO") != "" { opts = append(opts, withPluginClientConn(plugin.Name())) } + opts = append(opts, command.WithEnableGlobalMeterProvider(), command.WithEnableGlobalTracerProvider()) err = tcmd.Initialize(opts...) ogRunE := cmd.RunE if ogRunE == nil { diff --git a/cli/command/cli.go b/cli/command/cli.go index d8c3460efa..06478c9c77 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -92,6 +92,8 @@ type DockerCli struct { // this may be replaced by explicitly passing a context to functions that // need it. baseCtx context.Context + + enableGlobalMeter, enableGlobalTracer bool } // DefaultVersion returns api.defaultVersion. @@ -284,8 +286,12 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption) } // TODO(krissetto): pass ctx to the funcs instead of using this - cli.createGlobalMeterProvider(cli.baseCtx) - cli.createGlobalTracerProvider(cli.baseCtx) + if cli.enableGlobalMeter { + cli.createGlobalMeterProvider(cli.baseCtx) + } + if cli.enableGlobalTracer { + cli.createGlobalTracerProvider(cli.baseCtx) + } return nil } diff --git a/cli/command/telemetry_options.go b/cli/command/telemetry_options.go new file mode 100644 index 0000000000..1d7baaa51c --- /dev/null +++ b/cli/command/telemetry_options.go @@ -0,0 +1,25 @@ +package command + +// WithEnableGlobalMeterProvider configures the DockerCli to create a new +// MeterProvider from the initialized DockerCli struct, and set it as +// the global meter provider. +// +// WARNING: For internal use, don't depend on this. +func WithEnableGlobalMeterProvider() CLIOption { + return func(cli *DockerCli) error { + cli.enableGlobalMeter = true + return nil + } +} + +// WithEnableGlobalTracerProvider configures the DockerCli to create a new +// TracerProvider from the initialized DockerCli struct, and set it as +// the global tracer provider. +// +// WARNING: For internal use, don't depend on this. +func WithEnableGlobalTracerProvider() CLIOption { + return func(cli *DockerCli) error { + cli.enableGlobalTracer = true + return nil + } +} diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 6b8f4ab589..7825c7d354 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -354,7 +354,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error { return err } - if err := tcmd.Initialize(); err != nil { + if err := tcmd.Initialize(command.WithEnableGlobalMeterProvider(), command.WithEnableGlobalTracerProvider()); err != nil { return err }