Merge pull request #5070 from laurazard/backport/5067-26.1

[26.1 backport] Centralize init of Meter/TracerProviders
This commit is contained in:
Laura Brehm 2024-05-14 16:52:23 +01:00 committed by GitHub
commit 004e2925d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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()
defer mp.Shutdown(ctx) if mp, ok := mp.(command.MeterProvider); ok {
otel.SetMeterProvider(mp) defer mp.Shutdown(ctx)
} 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