Skip to content

Commit

Permalink
cli: make initializing the global meter- and tracing providers optional
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jun 24, 2024
1 parent 50acbb0 commit 13addd4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions cli-plugins/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (tcmd *TopLevelCommand) HandleGlobalFlags() (*cobra.Command, []string, erro
// arguments are in `flags.Args()`.
if err := flags.Parse(tcmd.args); err != nil {
// Our FlagErrorFunc uses the cli, make sure it is initialized
// TODO(thaJeztah): does this need OTEL global tracer and meter to be configured?
if err := tcmd.Initialize(); err != nil {
return nil, nil, err
}
Expand Down
10 changes: 8 additions & 2 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions cli/command/telemetry_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package command

// WithEnableGlobalMeterProvider configures the DockerCli to create a new
// MeterProvider from the initialized DockerCli struct, and set it as
// the global meter provider.
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.
func WithEnableGlobalTracerProvider() CLIOption {
return func(cli *DockerCli) error {
cli.enableGlobalTracer = true
return nil
}
}
2 changes: 1 addition & 1 deletion cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 13addd4

Please sign in to comment.