Skip to content

Commit

Permalink
WIP Add Cleanup() hook for collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed May 19, 2020
1 parent a57db4c commit 4619f64
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ a commandline interface for interacting with it.`,
runCancel() // stop the test run, metric processing is cancelled below
lingerCancel()

for _, c := range engine.Collectors {
c.SetRunStatus(lib.RunStatusAbortedUser)
c.Cleanup()
}

// If we get a second signal, we immediately exit, so something like
// https://github.com/loadimpact/k6/issues/971 never happens again
sig = <-sigC
Expand Down
4 changes: 4 additions & 0 deletions lib/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ type Collector interface {
// You should do any lengthy setup here rather than in New.
Init() error

// Cleanup is run on premature engine interruption (e.g. Ctrl+C).
// It can be used to perform optional maintenance.
Cleanup()

// Run is called in a goroutine and starts the collector. Should commit samples to the backend
// at regular intervals and when the context is terminated.
Run(ctx context.Context)
Expand Down
5 changes: 5 additions & 0 deletions stats/cloud/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ func (c *Collector) Link() string {
return URLForResults(c.referenceID, c.config)
}

// Cleanup updates the test status in the backend
func (c *Collector) Cleanup() {
c.testFinished()
}

// Run is called in a goroutine and starts the collector. Should commit samples to the backend
// at regular intervals and when the context is terminated.
func (c *Collector) Run(ctx context.Context) {
Expand Down
8 changes: 6 additions & 2 deletions stats/csv/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ import (
"sync"
"time"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
)

// Collector saving output to csv implements the lib.Collector interface
Expand Down Expand Up @@ -118,6 +119,9 @@ func (c *Collector) Init() error {
return nil
}

// Cleanup does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Cleanup() {}

// SetRunStatus does nothing
func (c *Collector) SetRunStatus(status lib.RunStatus) {}

Expand Down
3 changes: 3 additions & 0 deletions stats/dummy/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ var _ lib.Collector = &Collector{}
// Init does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Init() error { return nil }

// Cleanup does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Cleanup() {}

// MakeConfig does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) MakeConfig() interface{} { return nil }

Expand Down
6 changes: 5 additions & 1 deletion stats/influxdb/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
"time"

client "github.com/influxdata/influxdb1-client/v2"
"github.com/sirupsen/logrus"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
"github.com/sirupsen/logrus"
)

// Verify that Collector implements lib.Collector
Expand Down Expand Up @@ -74,6 +75,9 @@ func (c *Collector) Init() error {
return nil
}

// Cleanup does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Cleanup() {}

func (c *Collector) Run(ctx context.Context) {
logrus.Debug("InfluxDB: Running!")
ticker := time.NewTicker(time.Duration(c.Config.PushInterval.Duration))
Expand Down
8 changes: 5 additions & 3 deletions stats/json/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ func New(fs afero.Fs, fname string) (*Collector, error) {
return c, nil
}

func (c *Collector) Init() error {
return nil
}
// Init does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Init() error { return nil }

// Cleanup does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Cleanup() {}

func (c *Collector) SetRunStatus(status lib.RunStatus) {}

Expand Down
3 changes: 3 additions & 0 deletions stats/kafka/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func New(conf Config) (*Collector, error) {
// Init does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Init() error { return nil }

// Cleanup does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Cleanup() {}

// Run just blocks until the context is done
func (c *Collector) Run(ctx context.Context) {
logrus.Debug("Kafka: Running!")
Expand Down
3 changes: 3 additions & 0 deletions stats/statsd/common/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (c *Collector) Link() string {
return c.Config.Addr.String
}

// Cleanup does nothing, it's only included to satisfy the lib.Collector interface
func (c *Collector) Cleanup() {}

// Run the collector
func (c *Collector) Run(ctx context.Context) {
c.logger.Debugf("%s: Running!", c.Type)
Expand Down

0 comments on commit 4619f64

Please sign in to comment.