Use funcs on DockerCli to return Meter/TracerProviders, not initialize them. Initialize them during DockerCli struct init

Signed-off-by: Christopher Petito <chrisjpetito@gmail.com>
(cherry picked from commit 02537eac59)
This commit is contained in:
Christopher Petito 2024-05-14 15:23:49 +00:00 committed by Laura Brehm
parent 60f2d38d53
commit 6691085012
No known key found for this signature in database
GPG Key ID: CFBF847B4A313468
3 changed files with 44 additions and 23 deletions

View File

@ -273,6 +273,11 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption)
return ResolveDefaultContext(cli.options, cli.contextStoreConfig) return ResolveDefaultContext(cli.options, cli.contextStoreConfig)
}, },
} }
// TODO(krissetto): pass ctx to the funcs instead of using this
cli.createGlobalMeterProvider(cli.baseCtx)
cli.createGlobalTracerProvider(cli.baseCtx)
return nil return nil
} }

View File

@ -41,35 +41,25 @@ type TelemetryClient interface {
// each time this function is invoked. // each time this function is invoked.
Resource() *resource.Resource Resource() *resource.Resource
// TracerProvider returns a TracerProvider. This TracerProvider will be configured // TracerProvider returns the currently initialized TracerProvider. This TracerProvider will be configured
// with the default tracing components for a CLI program along with any options given // with the default tracing components for a CLI program
// for the SDK. TracerProvider() trace.TracerProvider
TracerProvider(ctx context.Context, opts ...sdktrace.TracerProviderOption) TracerProvider
// MeterProvider returns a MeterProvider. This MeterProvider will be configured // MeterProvider returns the currently initialized MeterProvider. This MeterProvider will be configured
// with the default metric components for a CLI program along with any options given // with the default metric components for a CLI program
// for the SDK. MeterProvider() metric.MeterProvider
MeterProvider(ctx context.Context, opts ...sdkmetric.Option) MeterProvider
} }
func (cli *DockerCli) Resource() *resource.Resource { func (cli *DockerCli) Resource() *resource.Resource {
return cli.res.Get() return cli.res.Get()
} }
func (cli *DockerCli) TracerProvider(ctx context.Context, opts ...sdktrace.TracerProviderOption) TracerProvider { func (cli *DockerCli) TracerProvider() trace.TracerProvider {
allOpts := make([]sdktrace.TracerProviderOption, 0, len(opts)+2) return otel.GetTracerProvider()
allOpts = append(allOpts, sdktrace.WithResource(cli.Resource()))
allOpts = append(allOpts, dockerSpanExporter(ctx, cli)...)
allOpts = append(allOpts, opts...)
return sdktrace.NewTracerProvider(allOpts...)
} }
func (cli *DockerCli) MeterProvider(ctx context.Context, opts ...sdkmetric.Option) MeterProvider { func (cli *DockerCli) MeterProvider() metric.MeterProvider {
allOpts := make([]sdkmetric.Option, 0, len(opts)+2) return otel.GetMeterProvider()
allOpts = append(allOpts, sdkmetric.WithResource(cli.Resource()))
allOpts = append(allOpts, dockerMetricExporter(ctx, cli)...)
allOpts = append(allOpts, opts...)
return sdkmetric.NewMeterProvider(allOpts...)
} }
// WithResourceOptions configures additional options for the default resource. The default // WithResourceOptions configures additional options for the default resource. The default
@ -122,6 +112,28 @@ func (r *telemetryResource) init() {
r.opts = nil r.opts = nil
} }
// createGlobalMeterProvider creates a new MeterProvider from the initialized DockerCli struct
// with the given options and sets it as the global meter provider
func (cli *DockerCli) createGlobalMeterProvider(ctx context.Context, opts ...sdkmetric.Option) {
allOpts := make([]sdkmetric.Option, 0, len(opts)+2)
allOpts = append(allOpts, sdkmetric.WithResource(cli.Resource()))
allOpts = append(allOpts, dockerMetricExporter(ctx, cli)...)
allOpts = append(allOpts, opts...)
mp := sdkmetric.NewMeterProvider(allOpts...)
otel.SetMeterProvider(mp)
}
// createGlobalTracerProvider creates a new TracerProvider from the initialized DockerCli struct
// with the given options and sets it as the global tracer provider
func (cli *DockerCli) createGlobalTracerProvider(ctx context.Context, opts ...sdktrace.TracerProviderOption) {
allOpts := make([]sdktrace.TracerProviderOption, 0, len(opts)+2)
allOpts = append(allOpts, sdktrace.WithResource(cli.Resource()))
allOpts = append(allOpts, dockerSpanExporter(ctx, cli)...)
allOpts = append(allOpts, opts...)
tp := sdktrace.NewTracerProvider(allOpts...)
otel.SetTracerProvider(tp)
}
func defaultResourceOptions() []resource.Option { func defaultResourceOptions() []resource.Option {
return []resource.Option{ return []resource.Option{
resource.WithDetectors(serviceNameDetector{}), resource.WithDetectors(serviceNameDetector{}),

View File

@ -307,9 +307,13 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
return err return err
} }
mp := dockerCli.MeterProvider(ctx) mp := dockerCli.MeterProvider()
if mp, ok := mp.(command.MeterProvider); ok {
defer mp.Shutdown(ctx) defer mp.Shutdown(ctx)
otel.SetMeterProvider(mp) } else {
fmt.Fprint(dockerCli.Err(), "Warning: Unexpected OTEL error, metrics may not be flushed")
}
dockerCli.InstrumentCobraCommands(cmd, mp) dockerCli.InstrumentCobraCommands(cmd, mp)
var envs []string var envs []string