@@ -40,7 +40,7 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.C
4040 if err != nil {
4141 return err
4242 }
43- return runStart (cmd .Context (), cmd . Flags (), rt , cfg , tel , logger )
43+ return startEmulator (cmd .Context (), rt , cfg , tel , logger )
4444 },
4545 }
4646
@@ -63,15 +63,15 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.C
6363 newStopCmd (cfg , tel ),
6464 newRestartCmd (cfg , tel , logger ),
6565 newLoginCmd (cfg , tel , logger ),
66- newLogoutCmd (cfg , tel , logger ),
67- newStatusCmd (cfg , tel ),
68- newLogsCmd (cfg , tel ),
69- newSetupCmd (cfg , tel ),
70- newConfigCmd (cfg , tel ),
71- newVolumeCmd (cfg , tel ),
72- newUpdateCmd (cfg , tel ),
66+ newLogoutCmd (cfg , logger ),
67+ newStatusCmd (cfg ),
68+ newLogsCmd (cfg ),
69+ newSetupCmd (cfg ),
70+ newConfigCmd (cfg ),
71+ newVolumeCmd (cfg ),
72+ newUpdateCmd (cfg ),
7373 newDocsCmd (),
74- newAWSCmd (cfg , tel ),
74+ newAWSCmd (cfg ),
7575 )
7676
7777 return root
@@ -118,6 +118,7 @@ func Execute(ctx context.Context) error {
118118 root := NewRootCmd (cfg , tel , logger )
119119 root .SilenceErrors = true
120120 root .SilenceUsage = true
121+ instrumentCommands (root , tel )
121122 if cfg .TracesEnabled {
122123 wrapCommandsWithTracing (root )
123124 }
@@ -192,48 +193,34 @@ func startEmulator(ctx context.Context, rt runtime.Runtime, cfg *env.Env, tel *t
192193 return container .Start (ctx , rt , sink , opts , false )
193194}
194195
195- func runStart (ctx context.Context , cmdFlags * pflag.FlagSet , rt runtime.Runtime , cfg * env.Env , tel * telemetry.Client , logger log.Logger ) error {
196- startTime := time .Now ()
197-
198- var flags []string
199- cmdFlags .Visit (func (f * pflag.Flag ) {
200- flags = append (flags , "--" + f .Name )
201- })
202-
203- runErr := startEmulator (ctx , rt , cfg , tel , logger )
204-
205- exitCode := 0
206- errorMsg := ""
207- if runErr != nil {
208- exitCode = 1
209- errorMsg = runErr .Error ()
210- }
211- tel .EmitCommand (ctx , "start" , flags , time .Since (startTime ).Milliseconds (), exitCode , errorMsg )
212-
213- return runErr
214- }
215-
216- // wraps a RunE function so that an lstk_command event is emitted after every invocation
217- // used for commands that do not emit lstk_lifecycle events (i.e. status, logs, config path, etc)
218- func commandWithTelemetry (name string , tel * telemetry.Client , fn func (* cobra.Command , []string ) error ) func (* cobra.Command , []string ) error {
219- return func (cmd * cobra.Command , args []string ) error {
220- startTime := time .Now ()
221- runErr := fn (cmd , args )
196+ // instrumentCommands walks the Cobra command tree and wraps every RunE with telemetry emission.
197+ func instrumentCommands (cmd * cobra.Command , tel * telemetry.Client ) {
198+ if cmd .RunE != nil {
199+ original := cmd .RunE
200+ cmd .RunE = func (c * cobra.Command , args []string ) error {
201+ startTime := time .Now ()
202+ runErr := original (c , args )
203+
204+ var flags []string
205+ c .Flags ().Visit (func (f * pflag.Flag ) {
206+ flags = append (flags , "--" + f .Name )
207+ })
208+
209+ exitCode := 0
210+ errorMsg := ""
211+ if runErr != nil {
212+ exitCode = 1
213+ errorMsg = runErr .Error ()
214+ }
222215
223- var flags []string
224- cmd .Flags ().Visit (func (f * pflag.Flag ) {
225- flags = append (flags , "--" + f .Name )
226- })
216+ commandName := strings .TrimPrefix (c .CommandPath (), c .Root ().Name ()+ " " )
217+ tel .EmitCommand (c .Context (), commandName , flags , time .Since (startTime ).Milliseconds (), exitCode , errorMsg )
227218
228- exitCode := 0
229- errorMsg := ""
230- if runErr != nil {
231- exitCode = 1
232- errorMsg = runErr .Error ()
219+ return runErr
233220 }
234- tel . EmitCommand ( cmd . Context (), name , flags , time . Since ( startTime ). Milliseconds (), exitCode , errorMsg )
235-
236- return runErr
221+ }
222+ for _ , child := range cmd . Commands () {
223+ instrumentCommands ( child , tel )
237224 }
238225}
239226
0 commit comments