Setup should be called as part of the command's PersistentPreRunE as soon as possible after initializing the dockerCli. It initializes the tracer for the CLI using both auto-detection from the Docker context metadata as well as standard OTEL_ env vars, creates a root span for the command, and wraps
(cmd *cobra.Command, dockerCli command.Cli, args []string)
| 46 | // command invocation to ensure the span is properly finalized and |
| 47 | // exported before exit. |
| 48 | func Setup(cmd *cobra.Command, dockerCli command.Cli, args []string) error { |
| 49 | tracingShutdown, err := tracing.InitTracing(dockerCli) |
| 50 | if err != nil { |
| 51 | return fmt.Errorf("initializing tracing: %w", err) |
| 52 | } |
| 53 | |
| 54 | ctx := cmd.Context() |
| 55 | ctx, cmdSpan := otel.Tracer("").Start( |
| 56 | ctx, |
| 57 | "cli/"+strings.Join(commandName(cmd), "-"), |
| 58 | ) |
| 59 | cmdSpan.SetAttributes( |
| 60 | attribute.StringSlice("cli.flags", getFlags(cmd.Flags())), |
| 61 | attribute.Bool("cli.isatty", dockerCli.In().IsTerminal()), |
| 62 | ) |
| 63 | |
| 64 | cmd.SetContext(ctx) |
| 65 | wrapRunE(cmd, cmdSpan, tracingShutdown) |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // wrapRunE injects a wrapper function around the command's actual RunE (or Run) |
| 70 | // method. This is necessary to capture the command result for reporting as well |
no test coverage detected