@@ -41,33 +41,27 @@ type StartOptions struct {
4141 Telemetry * telemetry.Client
4242}
4343
44- // telCtx carries telemetry context for emitting per-container lifecycle events.
45- type telCtx struct {
46- tel * telemetry.Client
47- authToken string
48- }
49-
50- func (t telCtx ) emitEmulatorStartError (ctx context.Context , c runtime.ContainerConfig , errorCode , errorMsg string ) {
51- if t .tel == nil {
44+ func emitEmulatorStartError (ctx context.Context , tel * telemetry.Client , c runtime.ContainerConfig , errorCode , errorMsg string ) {
45+ if tel == nil {
5246 return
5347 }
54- t . tel .Emit (ctx , "lstk_lifecycle" , telemetry .ToMap (telemetry.LifecycleEvent {
48+ tel .Emit (ctx , "lstk_lifecycle" , telemetry .ToMap (telemetry.LifecycleEvent {
5549 EventType : telemetry .LifecycleStartError ,
56- Environment : t . tel .GetEnvironment (t . authToken ),
50+ Environment : tel .GetEnvironment (),
5751 Emulator : c .EmulatorType ,
5852 Image : c .Image ,
5953 ErrorCode : errorCode ,
6054 ErrorMsg : errorMsg ,
6155 }))
6256}
6357
64- func ( t telCtx ) emitEmulatorStartSuccess (ctx context.Context , c runtime.ContainerConfig , containerID string , durationMS int64 , info * telemetry.LocalStackInfo ) {
65- if t . tel == nil {
58+ func emitEmulatorStartSuccess (ctx context.Context , tel * telemetry. Client , c runtime.ContainerConfig , containerID string , durationMS int64 , info * telemetry.LocalStackInfo ) {
59+ if tel == nil {
6660 return
6761 }
68- t . tel .Emit (ctx , "lstk_lifecycle" , telemetry .ToMap (telemetry.LifecycleEvent {
62+ tel .Emit (ctx , "lstk_lifecycle" , telemetry .ToMap (telemetry.LifecycleEvent {
6963 EventType : telemetry .LifecycleStartSuccess ,
70- Environment : t . tel .GetEnvironment (t . authToken ),
64+ Environment : tel .GetEnvironment (),
7165 Emulator : c .EmulatorType ,
7266 Image : c .Image ,
7367 ContainerID : containerID ,
@@ -94,14 +88,15 @@ func Start(ctx context.Context, rt runtime.Runtime, sink output.Sink, opts Start
9488 return err
9589 }
9690
91+ if opts .Telemetry != nil {
92+ opts .Telemetry .SetAuthToken (token )
93+ }
94+
9795 if hasDuplicateContainerTypes (opts .Containers ) {
9896 output .EmitWarning (sink , "Multiple emulators of the same type are defined in your config; this setup is not supported yet" )
9997 }
10098
101- tel := telCtx {
102- tel : opts .Telemetry ,
103- authToken : token ,
104- }
99+ tel := opts .Telemetry
105100
106101 containers := make ([]runtime.ContainerConfig , len (opts .Containers ))
107102 for i , c := range opts .Containers {
@@ -232,7 +227,7 @@ func emitPostStartPointers(sink output.Sink, resolvedHost, webAppURL string) {
232227 output .EmitSecondary (sink , tips [rand .IntN (len (tips ))])
233228}
234229
235- func pullImages (ctx context.Context , rt runtime.Runtime , sink output.Sink , tel telCtx , containers []runtime.ContainerConfig ) error {
230+ func pullImages (ctx context.Context , rt runtime.Runtime , sink output.Sink , tel * telemetry. Client , containers []runtime.ContainerConfig ) error {
236231 for _ , c := range containers {
237232 // Remove any existing stopped container with the same name
238233 if err := rt .Remove (ctx , c .Name ); err != nil && ! errdefs .IsNotFound (err ) {
@@ -253,7 +248,7 @@ func pullImages(ctx context.Context, rt runtime.Runtime, sink output.Sink, tel t
253248 Title : fmt .Sprintf ("Failed to pull %s" , c .Image ),
254249 Summary : err .Error (),
255250 })
256- tel . emitEmulatorStartError (ctx , c , telemetry .ErrCodeImagePullFailed , err .Error ())
251+ emitEmulatorStartError (ctx , tel , c , telemetry .ErrCodeImagePullFailed , err .Error ())
257252 return output .NewSilentError (fmt .Errorf ("failed to pull image %s: %w" , c .Image , err ))
258253 }
259254 output .EmitSpinnerStop (sink )
@@ -262,7 +257,7 @@ func pullImages(ctx context.Context, rt runtime.Runtime, sink output.Sink, tel t
262257 return nil
263258}
264259
265- func validateLicenses (ctx context.Context , rt runtime.Runtime , sink output.Sink , opts StartOptions , tel telCtx , containers []runtime.ContainerConfig , token string ) error {
260+ func validateLicenses (ctx context.Context , rt runtime.Runtime , sink output.Sink , opts StartOptions , tel * telemetry. Client , containers []runtime.ContainerConfig , token string ) error {
266261 for _ , c := range containers {
267262 if err := validateLicense (ctx , rt , sink , opts , tel , c , token ); err != nil {
268263 return err
@@ -271,32 +266,32 @@ func validateLicenses(ctx context.Context, rt runtime.Runtime, sink output.Sink,
271266 return nil
272267}
273268
274- func startContainers (ctx context.Context , rt runtime.Runtime , sink output.Sink , tel telCtx , containers []runtime.ContainerConfig ) error {
269+ func startContainers (ctx context.Context , rt runtime.Runtime , sink output.Sink , tel * telemetry. Client , containers []runtime.ContainerConfig ) error {
275270 for _ , c := range containers {
276271 startTime := time .Now ()
277272 output .EmitStatus (sink , "starting" , c .Name , "" )
278273 containerID , err := rt .Start (ctx , c )
279274 if err != nil {
280- tel . emitEmulatorStartError (ctx , c , telemetry .ErrCodeStartFailed , err .Error ())
275+ emitEmulatorStartError (ctx , tel , c , telemetry .ErrCodeStartFailed , err .Error ())
281276 return fmt .Errorf ("failed to start LocalStack: %w" , err )
282277 }
283278
284279 output .EmitStatus (sink , "waiting" , c .Name , "" )
285280 healthURL := fmt .Sprintf ("http://localhost:%s%s" , c .Port , c .HealthPath )
286281 if err := awaitStartup (ctx , rt , sink , containerID , "LocalStack" , healthURL ); err != nil {
287- tel . emitEmulatorStartError (ctx , c , telemetry .ErrCodeStartFailed , err .Error ())
282+ emitEmulatorStartError (ctx , tel , c , telemetry .ErrCodeStartFailed , err .Error ())
288283 return err
289284 }
290285
291286 output .EmitStatus (sink , "ready" , c .Name , fmt .Sprintf ("containerId: %s" , containerID [:12 ]))
292287
293288 lsInfo , _ := fetchLocalStackInfo (ctx , c .Port )
294- tel . emitEmulatorStartSuccess (ctx , c , containerID [:12 ], time .Since (startTime ).Milliseconds (), lsInfo )
289+ emitEmulatorStartSuccess (ctx , tel , c , containerID [:12 ], time .Since (startTime ).Milliseconds (), lsInfo )
295290 }
296291 return nil
297292}
298293
299- func selectContainersToStart (ctx context.Context , rt runtime.Runtime , sink output.Sink , tel telCtx , containers []runtime.ContainerConfig ) ([]runtime.ContainerConfig , error ) {
294+ func selectContainersToStart (ctx context.Context , rt runtime.Runtime , sink output.Sink , tel * telemetry. Client , containers []runtime.ContainerConfig ) ([]runtime.ContainerConfig , error ) {
300295 var filtered []runtime.ContainerConfig
301296 for _ , c := range containers {
302297 running , err := rt .IsRunning (ctx , c .Name )
@@ -309,7 +304,7 @@ func selectContainersToStart(ctx context.Context, rt runtime.Runtime, sink outpu
309304 }
310305 if err := ports .CheckAvailable (c .Port ); err != nil {
311306 emitPortInUseError (sink , c .Port )
312- tel . emitEmulatorStartError (ctx , c , telemetry .ErrCodePortConflict , err .Error ())
307+ emitEmulatorStartError (ctx , tel , c , telemetry .ErrCodePortConflict , err .Error ())
313308 return nil , output .NewSilentError (err )
314309 }
315310 filtered = append (filtered , c )
@@ -332,7 +327,7 @@ func emitPortInUseError(sink output.Sink, port string) {
332327 })
333328}
334329
335- func validateLicense (ctx context.Context , rt runtime.Runtime , sink output.Sink , opts StartOptions , tel telCtx , containerConfig runtime.ContainerConfig , token string ) error {
330+ func validateLicense (ctx context.Context , rt runtime.Runtime , sink output.Sink , opts StartOptions , tel * telemetry. Client , containerConfig runtime.ContainerConfig , token string ) error {
336331 version := containerConfig .Tag
337332 if version == "" || version == "latest" {
338333 actualVersion , err := rt .GetImageVersion (ctx , containerConfig .Image )
@@ -365,7 +360,7 @@ func validateLicense(ctx context.Context, rt runtime.Runtime, sink output.Sink,
365360 if errors .As (err , & licErr ) && licErr .Detail != "" {
366361 opts .Logger .Error ("license server response (HTTP %d): %s" , licErr .Status , licErr .Detail )
367362 }
368- tel . emitEmulatorStartError (ctx , containerConfig , telemetry .ErrCodeLicenseInvalid , err .Error ())
363+ emitEmulatorStartError (ctx , tel , containerConfig , telemetry .ErrCodeLicenseInvalid , err .Error ())
369364 return fmt .Errorf ("license validation failed for %s:%s: %w" , containerConfig .ProductName , version , err )
370365 }
371366
0 commit comments