MCPcopy Index your code
hub / github.com/docker/cli / InstrumentCobraCommands

Method InstrumentCobraCommands

cli/command/telemetry_utils.go:29–65  ·  view source on GitHub ↗

InstrumentCobraCommands wraps all cobra commands' RunE funcs to set a command duration metric using otel. Note: this should be the last func to wrap/modify the PersistentRunE/RunE funcs before command execution. can also be used for spans!

(ctx context.Context, cmd *cobra.Command)

Source from the content-addressed store, hash-verified

27//
28// can also be used for spans!
29func (cli *DockerCli) InstrumentCobraCommands(ctx context.Context, cmd *cobra.Command) {
30 // If PersistentPreRunE is nil, make it execute PersistentPreRun and return nil by default
31 ogPersistentPreRunE := cmd.PersistentPreRunE
32 if ogPersistentPreRunE == nil {
33 ogPersistentPreRun := cmd.PersistentPreRun
34 //nolint:unparam // necessary because error will always be nil here
35 ogPersistentPreRunE = func(cmd *cobra.Command, args []string) error {
36 ogPersistentPreRun(cmd, args)
37 return nil
38 }
39 cmd.PersistentPreRun = nil
40 }
41
42 // wrap RunE in PersistentPreRunE so that this operation gets executed on all children commands
43 cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
44 // If RunE is nil, make it execute Run and return nil by default
45 ogRunE := cmd.RunE
46 if ogRunE == nil {
47 ogRun := cmd.Run
48 //nolint:unparam // necessary because error will always be nil here
49 ogRunE = func(cmd *cobra.Command, args []string) error {
50 ogRun(cmd, args)
51 return nil
52 }
53 cmd.Run = nil
54 }
55 cmd.RunE = func(cmd *cobra.Command, args []string) error {
56 // start the timer as the first step of every cobra command
57 stopInstrumentation := cli.StartInstrumentation(cmd)
58 cmdErr := ogRunE(cmd, args)
59 stopInstrumentation(cmdErr)
60 return cmdErr
61 }
62
63 return ogPersistentPreRunE(cmd, args)
64 }
65}
66
67// StartInstrumentation instruments CLI commands with the individual metrics and spans configured.
68// It's the main command OTel utility, and new command-related metrics should be added to it.

Callers 1

runDockerFunction · 0.80

Calls 1

StartInstrumentationMethod · 0.95

Tested by

no test coverage detected