MCPcopy
hub / github.com/docker/compose / traceClientFromDockerContext

Function traceClientFromDockerContext

internal/tracing/docker_context.go:37–77  ·  view source on GitHub ↗

traceClientFromDockerContext creates a gRPC OTLP client based on metadata from the active Docker CLI context.

(dockerCli command.Cli, otelEnv envMap)

Source from the content-addressed store, hash-verified

35// traceClientFromDockerContext creates a gRPC OTLP client based on metadata
36// from the active Docker CLI context.
37func traceClientFromDockerContext(dockerCli command.Cli, otelEnv envMap) (otlptrace.Client, error) {
38 // attempt to extract an OTEL config from the Docker context to enable
39 // automatic integration with Docker Desktop;
40 cfg, err := ConfigFromDockerContext(dockerCli.ContextStore(), dockerCli.CurrentContext())
41 if err != nil {
42 return nil, fmt.Errorf("loading otel config from docker context metadata: %w", err)
43 }
44
45 if cfg.Endpoint == "" {
46 return nil, nil
47 }
48
49 // HACK: unfortunately _all_ public OTEL initialization functions
50 // implicitly read from the OS env, so temporarily unset them all and
51 // restore afterwards
52 defer func() {
53 for k, v := range otelEnv {
54 if err := os.Setenv(k, v); err != nil {
55 panic(fmt.Errorf("restoring env for %q: %w", k, err))
56 }
57 }
58 }()
59 for k := range otelEnv {
60 if err := os.Unsetenv(k); err != nil {
61 return nil, fmt.Errorf("stashing env for %q: %w", k, err)
62 }
63 }
64
65 conn, err := grpc.NewClient(cfg.Endpoint,
66 grpc.WithContextDialer(memnet.DialEndpoint),
67 // this dial is restricted to using a local Unix socket / named pipe,
68 // so there is no need for TLS
69 grpc.WithTransportCredentials(insecure.NewCredentials()),
70 )
71 if err != nil {
72 return nil, fmt.Errorf("initializing otel connection from docker context metadata: %w", err)
73 }
74
75 client := otlptracegrpc.NewClient(otlptracegrpc.WithGRPCConn(conn))
76 return client, nil
77}
78
79// ConfigFromDockerContext inspects extra metadata included as part of the
80// specified Docker context to try and extract a valid OTLP client configuration.

Callers 1

InitProviderFunction · 0.85

Calls 3

ConfigFromDockerContextFunction · 0.85
CurrentContextMethod · 0.65
ContextStoreMethod · 0.45

Tested by

no test coverage detected