Skip to content

Commit 947effd

Browse files
committed
cli: make initializing the global meter- and tracing providers optional
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 50acbb0 commit 947effd

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

cli-plugins/plugin/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func RunPlugin(dockerCli *command.DockerCli, plugin *cobra.Command, meta manager
4545
if os.Getenv("DOCKER_CLI_PLUGIN_USE_DIAL_STDIO") != "" {
4646
opts = append(opts, withPluginClientConn(plugin.Name()))
4747
}
48+
opts = append(opts, command.WithEnableGlobalMeterProvider(), command.WithEnableGlobalTracerProvider())
4849
err = tcmd.Initialize(opts...)
4950
ogRunE := cmd.RunE
5051
if ogRunE == nil {

cli/cobra.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func (tcmd *TopLevelCommand) HandleGlobalFlags() (*cobra.Command, []string, erro
166166
// arguments are in `flags.Args()`.
167167
if err := flags.Parse(tcmd.args); err != nil {
168168
// Our FlagErrorFunc uses the cli, make sure it is initialized
169+
// TODO(thaJeztah): does this need OTEL global tracer and meter to be configured?
169170
if err := tcmd.Initialize(); err != nil {
170171
return nil, nil, err
171172
}

cli/command/cli.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ type DockerCli struct {
9292
// this may be replaced by explicitly passing a context to functions that
9393
// need it.
9494
baseCtx context.Context
95+
96+
enableGlobalMeter, enableGlobalTracer bool
9597
}
9698

9799
// DefaultVersion returns api.defaultVersion.
@@ -284,8 +286,12 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption)
284286
}
285287

286288
// TODO(krissetto): pass ctx to the funcs instead of using this
287-
cli.createGlobalMeterProvider(cli.baseCtx)
288-
cli.createGlobalTracerProvider(cli.baseCtx)
289+
if cli.enableGlobalMeter {
290+
cli.createGlobalMeterProvider(cli.baseCtx)
291+
}
292+
if cli.enableGlobalTracer {
293+
cli.createGlobalTracerProvider(cli.baseCtx)
294+
}
289295

290296
return nil
291297
}

cli/command/telemetry_options.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package command
2+
3+
// WithEnableGlobalMeterProvider configures the DockerCli to create a new
4+
// MeterProvider from the initialized DockerCli struct, and set it as
5+
// the global meter provider.
6+
//
7+
// WARNING: For internal use, don't depend on this.
8+
func WithEnableGlobalMeterProvider() CLIOption {
9+
return func(cli *DockerCli) error {
10+
cli.enableGlobalMeter = true
11+
return nil
12+
}
13+
}
14+
15+
// WithEnableGlobalTracerProvider configures the DockerCli to create a new
16+
// TracerProvider from the initialized DockerCli struct, and set it as
17+
// the global tracer provider.
18+
//
19+
// WARNING: For internal use, don't depend on this.
20+
func WithEnableGlobalTracerProvider() CLIOption {
21+
return func(cli *DockerCli) error {
22+
cli.enableGlobalTracer = true
23+
return nil
24+
}
25+
}

cmd/docker/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
354354
return err
355355
}
356356

357-
if err := tcmd.Initialize(); err != nil {
357+
if err := tcmd.Initialize(command.WithEnableGlobalMeterProvider(), command.WithEnableGlobalTracerProvider()); err != nil {
358358
return err
359359
}
360360

0 commit comments

Comments
 (0)