@@ -49,7 +49,10 @@ import (
49
49
"go.k6.io/k6/js"
50
50
"go.k6.io/k6/lib"
51
51
"go.k6.io/k6/lib/consts"
52
+ "go.k6.io/k6/lib/metrics"
52
53
"go.k6.io/k6/loader"
54
+ "go.k6.io/k6/output"
55
+ "go.k6.io/k6/stats"
53
56
"go.k6.io/k6/ui/pb"
54
57
)
55
58
@@ -153,10 +156,6 @@ a commandline interface for interacting with it.`,
153
156
// can start winding down its metrics processing.
154
157
globalCtx , globalCancel := context .WithCancel (ctx )
155
158
defer globalCancel ()
156
- lingerCtx , lingerCancel := context .WithCancel (globalCtx )
157
- defer lingerCancel ()
158
- runCtx , runCancel := context .WithCancel (lingerCtx )
159
- defer runCancel ()
160
159
161
160
// Create a local execution scheduler wrapping the runner.
162
161
logger .Debug ("Initializing the execution scheduler..." )
@@ -197,6 +196,16 @@ a commandline interface for interacting with it.`,
197
196
return err
198
197
}
199
198
199
+ registry := stats .NewRegistry (engine .Samples )
200
+ builtInMetrics := metrics .RegisterBuiltinMetrics (registry )
201
+ globalCtx = stats .WithRegistry (globalCtx , registry )
202
+ globalCtx = metrics .WithBuiltinMetrics (globalCtx , builtInMetrics )
203
+
204
+ lingerCtx , lingerCancel := context .WithCancel (globalCtx )
205
+ defer lingerCancel ()
206
+ runCtx , runCancel := context .WithCancel (lingerCtx )
207
+ defer runCancel ()
208
+
200
209
// Spin up the REST API server, if not disabled.
201
210
if address != "" {
202
211
initBar .Modify (pb .WithConstProgress (0 , "Init API server" ))
@@ -216,11 +225,11 @@ a commandline interface for interacting with it.`,
216
225
217
226
// We do this here so we can get any output URLs below.
218
227
initBar .Modify (pb .WithConstProgress (0 , "Starting outputs" ))
219
- err = engine . StartOutputs ()
228
+ err = StartOutputs (logger , outputs , engine , builtInMetrics )
220
229
if err != nil {
221
230
return err
222
231
}
223
- defer engine . StopOutputs ()
232
+ defer StopOutputs (logger , outputs )
224
233
225
234
printExecutionDescription (
226
235
"local" , filename , "" , conf , execScheduler .GetState ().ExecutionTuple ,
@@ -442,3 +451,53 @@ func handleSummaryResult(fs afero.Fs, stdOut, stdErr io.Writer, result map[strin
442
451
443
452
return consolidateErrorMessage (errs , "Could not save some summary information:" )
444
453
}
454
+
455
+ // StartOutputs spins up all configured outputs, giving the thresholds to any
456
+ // that can accept them. And if some output fails, stop the already started
457
+ // ones. This may take some time, since some outputs make initial network
458
+ // requests to set up whatever remote services are going to listen to them.
459
+ func StartOutputs (
460
+ logger logrus.FieldLogger ,
461
+ outputs []output.Output ,
462
+ engine * core.Engine ,
463
+ builtinMetrics * metrics.BuiltInMetrics ,
464
+ ) error {
465
+ logger .Debugf ("Starting %d outputs..." , len (outputs ))
466
+ for i , out := range outputs {
467
+ if thresholdOut , ok := out .(output.WithThresholds ); ok {
468
+ thresholdOut .SetThresholds (engine .Options .Thresholds )
469
+ }
470
+
471
+ if stopOut , ok := out .(output.WithTestRunStop ); ok {
472
+ stopOut .SetTestRunStopCallback (
473
+ func (err error ) {
474
+ logger .WithError (err ).Error ("Received error to stop from output" )
475
+ engine .Stop ()
476
+ })
477
+ }
478
+
479
+ if builtinMetricOut , ok := out .(output.WithBuiltinMetrics ); ok {
480
+ builtinMetricOut .SetBuiltinMetrics (builtinMetrics )
481
+ }
482
+
483
+ if err := out .Start (); err != nil {
484
+ stopOutputs (logger , outputs , i )
485
+ return err
486
+ }
487
+ }
488
+ return nil
489
+ }
490
+
491
+ // StopOutputs stops all configured outputs.
492
+ func StopOutputs (logger logrus.FieldLogger , outputs []output.Output ) {
493
+ stopOutputs (logger , outputs , len (outputs ))
494
+ }
495
+
496
+ func stopOutputs (logger logrus.FieldLogger , outputs []output.Output , upToID int ) {
497
+ logger .Debugf ("Stopping %d outputs..." , upToID )
498
+ for i := 0 ; i < upToID ; i ++ {
499
+ if err := outputs [i ].Stop (); err != nil {
500
+ logger .WithError (err ).Errorf ("Stopping output %d failed" , i )
501
+ }
502
+ }
503
+ }
0 commit comments