Skip to content

Commit 6691085

Browse files
krissettolaurazard
authored andcommitted
Use funcs on DockerCli to return Meter/TracerProviders, not initialize them. Initialize them during DockerCli struct init
Signed-off-by: Christopher Petito <[email protected]> (cherry picked from commit 02537ea)
1 parent 60f2d38 commit 6691085

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

cli/command/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption)
273273
return ResolveDefaultContext(cli.options, cli.contextStoreConfig)
274274
},
275275
}
276+
277+
// TODO(krissetto): pass ctx to the funcs instead of using this
278+
cli.createGlobalMeterProvider(cli.baseCtx)
279+
cli.createGlobalTracerProvider(cli.baseCtx)
280+
276281
return nil
277282
}
278283

cli/command/telemetry.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,25 @@ type TelemetryClient interface {
4141
// each time this function is invoked.
4242
Resource() *resource.Resource
4343

44-
// TracerProvider returns a TracerProvider. This TracerProvider will be configured
45-
// with the default tracing components for a CLI program along with any options given
46-
// for the SDK.
47-
TracerProvider(ctx context.Context, opts ...sdktrace.TracerProviderOption) TracerProvider
44+
// TracerProvider returns the currently initialized TracerProvider. This TracerProvider will be configured
45+
// with the default tracing components for a CLI program
46+
TracerProvider() trace.TracerProvider
4847

49-
// MeterProvider returns a MeterProvider. This MeterProvider will be configured
50-
// with the default metric components for a CLI program along with any options given
51-
// for the SDK.
52-
MeterProvider(ctx context.Context, opts ...sdkmetric.Option) MeterProvider
48+
// MeterProvider returns the currently initialized MeterProvider. This MeterProvider will be configured
49+
// with the default metric components for a CLI program
50+
MeterProvider() metric.MeterProvider
5351
}
5452

5553
func (cli *DockerCli) Resource() *resource.Resource {
5654
return cli.res.Get()
5755
}
5856

59-
func (cli *DockerCli) TracerProvider(ctx context.Context, opts ...sdktrace.TracerProviderOption) TracerProvider {
60-
allOpts := make([]sdktrace.TracerProviderOption, 0, len(opts)+2)
61-
allOpts = append(allOpts, sdktrace.WithResource(cli.Resource()))
62-
allOpts = append(allOpts, dockerSpanExporter(ctx, cli)...)
63-
allOpts = append(allOpts, opts...)
64-
return sdktrace.NewTracerProvider(allOpts...)
57+
func (cli *DockerCli) TracerProvider() trace.TracerProvider {
58+
return otel.GetTracerProvider()
6559
}
6660

67-
func (cli *DockerCli) MeterProvider(ctx context.Context, opts ...sdkmetric.Option) MeterProvider {
68-
allOpts := make([]sdkmetric.Option, 0, len(opts)+2)
69-
allOpts = append(allOpts, sdkmetric.WithResource(cli.Resource()))
70-
allOpts = append(allOpts, dockerMetricExporter(ctx, cli)...)
71-
allOpts = append(allOpts, opts...)
72-
return sdkmetric.NewMeterProvider(allOpts...)
61+
func (cli *DockerCli) MeterProvider() metric.MeterProvider {
62+
return otel.GetMeterProvider()
7363
}
7464

7565
// WithResourceOptions configures additional options for the default resource. The default
@@ -122,6 +112,28 @@ func (r *telemetryResource) init() {
122112
r.opts = nil
123113
}
124114

115+
// createGlobalMeterProvider creates a new MeterProvider from the initialized DockerCli struct
116+
// with the given options and sets it as the global meter provider
117+
func (cli *DockerCli) createGlobalMeterProvider(ctx context.Context, opts ...sdkmetric.Option) {
118+
allOpts := make([]sdkmetric.Option, 0, len(opts)+2)
119+
allOpts = append(allOpts, sdkmetric.WithResource(cli.Resource()))
120+
allOpts = append(allOpts, dockerMetricExporter(ctx, cli)...)
121+
allOpts = append(allOpts, opts...)
122+
mp := sdkmetric.NewMeterProvider(allOpts...)
123+
otel.SetMeterProvider(mp)
124+
}
125+
126+
// createGlobalTracerProvider creates a new TracerProvider from the initialized DockerCli struct
127+
// with the given options and sets it as the global tracer provider
128+
func (cli *DockerCli) createGlobalTracerProvider(ctx context.Context, opts ...sdktrace.TracerProviderOption) {
129+
allOpts := make([]sdktrace.TracerProviderOption, 0, len(opts)+2)
130+
allOpts = append(allOpts, sdktrace.WithResource(cli.Resource()))
131+
allOpts = append(allOpts, dockerSpanExporter(ctx, cli)...)
132+
allOpts = append(allOpts, opts...)
133+
tp := sdktrace.NewTracerProvider(allOpts...)
134+
otel.SetTracerProvider(tp)
135+
}
136+
125137
func defaultResourceOptions() []resource.Option {
126138
return []resource.Option{
127139
resource.WithDetectors(serviceNameDetector{}),

cmd/docker/docker.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,13 @@ func runDocker(ctx context.Context, dockerCli *command.DockerCli) error {
307307
return err
308308
}
309309

310-
mp := dockerCli.MeterProvider(ctx)
311-
defer mp.Shutdown(ctx)
312-
otel.SetMeterProvider(mp)
310+
mp := dockerCli.MeterProvider()
311+
if mp, ok := mp.(command.MeterProvider); ok {
312+
defer mp.Shutdown(ctx)
313+
} else {
314+
fmt.Fprint(dockerCli.Err(), "Warning: Unexpected OTEL error, metrics may not be flushed")
315+
}
316+
313317
dockerCli.InstrumentCobraCommands(cmd, mp)
314318

315319
var envs []string

0 commit comments

Comments
 (0)