Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tail logs in k6 #1599

Merged
merged 2 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ package cmd

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"

Expand All @@ -51,19 +54,37 @@ const (
)

//nolint:gochecknoglobals
var exitOnRunning = os.Getenv("K6_EXIT_ON_RUNNING") != ""
var (
exitOnRunning = os.Getenv("K6_EXIT_ON_RUNNING") != ""
showCloudLogs = true
)

//nolint:gochecknoglobals
var cloudCmd = &cobra.Command{
Use: "cloud",
Short: "Run a test on the cloud",
Long: `Run a test on the cloud.

This will execute the test on the Load Impact cloud service. Use "k6 login cloud" to authenticate.`,
This will execute the test on the k6 cloud service. Use "k6 login cloud" to authenticate.`,
Example: `
k6 cloud script.js`[1:],
Args: exactArgsWithMsg(1, "arg should either be \"-\", if reading script from stdin, or a path to a script file"),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: don't use the Global logger
logger := logrus.StandardLogger()
// we specifically first parse it and return an error if it has bad value and then check if
// we are going to set it ... so we always parse it instead of it breaking the command if
// the cli flag is removed
Comment on lines +76 to +77
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm that's a good point, I haven't considered it in my "fix #883" ideas dreams and it's probably going to slightly complicate matters... 😞

if showCloudLogsEnv, ok := os.LookupEnv("K6_SHOW_CLOUD_LOGS"); ok {
showCloudLogsValue, err := strconv.ParseBool(showCloudLogsEnv)
if err != nil {
return fmt.Errorf("parsing K6_SHOW_CLOUD_LOGS returned an error: %w", err)
}
if !cmd.Flags().Changed("show-logs") {
showCloudLogs = showCloudLogsValue
}

}
// TODO: disable in quiet mode?
_, _ = BannerColor.Fprintf(stdout, "\n%s\n\n", consts.Banner())

Expand All @@ -81,8 +102,6 @@ This will execute the test on the Load Impact cloud service. Use "k6 login cloud

filename := args[0]
filesystems := loader.CreateFilesystems()
// TODO: don't use the Global logger
logger := logrus.StandardLogger()
src, err := loader.ReadSource(logger, filename, pwd, filesystems, os.Stdin)
if err != nil {
return err
Expand Down Expand Up @@ -207,6 +226,16 @@ This will execute the test on the Load Impact cloud service. Use "k6 login cloud
pb.WithConstProgress(0, "Initializing the cloud test"),
)

progressCtx, progressCancel := context.WithCancel(context.Background())
progressBarWG := &sync.WaitGroup{}
progressBarWG.Add(1)
defer progressBarWG.Wait()
defer progressCancel()
go func() {
showProgress(progressCtx, conf, []*pb.ProgressBar{progressBar}, logger)
progressBarWG.Done()
}()

// The quiet option hides the progress bar and disallow aborting the test
if quiet {
return nil
Expand Down Expand Up @@ -247,6 +276,15 @@ This will execute the test on the Load Impact cloud service. Use "k6 login cloud
var progressErr error
ticker := time.NewTicker(time.Millisecond * 2000)
shouldExitLoop := false
if showCloudLogs {
na-- marked this conversation as resolved.
Show resolved Hide resolved
go func() {
logger.Debug("Connecting to cloud logs server...")
// TODO replace with another context
if err := cloudConfig.StreamLogsToLogger(context.Background(), logger, refID, 0); err != nil {
logger.WithError(err).Error("error while tailing cloud logs")
}
}()
}

runningLoop:
for {
Expand All @@ -257,7 +295,6 @@ This will execute the test on the Load Impact cloud service. Use "k6 login cloud
if (testProgress.RunStatus > lib.RunStatusRunning) || (exitOnRunning && testProgress.RunStatus == lib.RunStatusRunning) {
shouldExitLoop = true
}
printBar(progressBar)
} else {
logger.WithError(progressErr).Error("Test progress error")
}
Expand Down Expand Up @@ -305,6 +342,10 @@ func cloudCmdFlagSet() *pflag.FlagSet {
// K6_EXIT_ON_RUNNING=true won't affect the usage message
flags.Lookup("exit-on-running").DefValue = "false"

// read the comments above for explanation why this is done this way and what are the problems
flags.BoolVar(&showCloudLogs, "show-logs", showCloudLogs,
"enable showing of logs when a test is executed in the cloud")

return flags
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ a commandline interface for interacting with it.`,
progressBarWG := &sync.WaitGroup{}
progressBarWG.Add(1)
go func() {
showProgress(progressCtx, conf, execScheduler, logger)
pbs := []*pb.ProgressBar{execScheduler.GetInitProgressBar()}
for _, s := range execScheduler.GetExecutors() {
pbs = append(pbs, s.GetProgress())
}
showProgress(progressCtx, conf, pbs, logger)
progressBarWG.Done()
}()

Expand Down
10 changes: 2 additions & 8 deletions cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"

"github.com/loadimpact/k6/core/local"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/ui"
"github.com/loadimpact/k6/ui/pb"
Expand Down Expand Up @@ -242,17 +241,12 @@ func renderMultipleBars(
// nolint:funlen
func showProgress(
ctx context.Context, conf Config,
execScheduler *local.ExecutionScheduler, logger *logrus.Logger,
pbs []*pb.ProgressBar, logger *logrus.Logger,
) {
if quiet || conf.HTTPDebug.Valid && conf.HTTPDebug.String != "" {
if quiet {
return
}

pbs := []*pb.ProgressBar{execScheduler.GetInitProgressBar()}
for _, s := range execScheduler.GetExecutors() {
pbs = append(pbs, s.GetProgress())
}

var errTermGetSize bool
termWidth := defaultTermWidth
if stdoutTTY {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require (
github.com/pmezard/go-difflib v1.0.0
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 // indirect
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
github.com/sirupsen/logrus v1.1.2-0.20181101075517-7eeb7b7cbdeb
github.com/sirupsen/logrus v1.6.0
github.com/spf13/afero v1.1.1
github.com/spf13/cobra v0.0.4-0.20180629152535-a114f312e075
github.com/spf13/pflag v1.0.1
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/klauspost/compress v1.7.2 h1:liMOoeIvFpr9kEvalrZ7VVBA4wGf7zfOgwBjzz/5
github.com/klauspost/compress v1.7.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v1.3.1 h1:5JNjFYYQrZeKRJ0734q51WCEEn2huer72Dc7K+R/b6s=
github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kubernetes/helm v2.9.0+incompatible h1:X6Tl40RMiqT0GD8hT3+jFPgkZV1EB6vYsoJDX8YkY+c=
github.com/kubernetes/helm v2.9.0+incompatible/go.mod h1:3Nb8I82ptmDi7OvvBQK25X1bwxg+WMAkusUQXHxu8ag=
github.com/labstack/echo v3.2.6+incompatible h1:28U/uXFFKBIP+VQIqq641LuYhMS7Br9ZlwEXERaohCc=
Expand Down Expand Up @@ -123,15 +123,14 @@ github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 h1:nkcn14uNmFE
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516 h1:ofR1ZdrNSkiWcMsRrubK9tb2/SlZVWttAfqUjJi6QYc=
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
github.com/sirupsen/logrus v1.1.2-0.20181101075517-7eeb7b7cbdeb h1:Zs7k4rxIvfXFjav0ia3QqnauqGfG2hr8+y6NZYwq8iA=
github.com/sirupsen/logrus v1.1.2-0.20181101075517-7eeb7b7cbdeb/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/spf13/afero v1.1.1 h1:Lt3ihYMlE+lreX1GS4Qw4ZsNpYQLxIXKBTEOXm3nt6I=
github.com/spf13/afero v1.1.1/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cobra v0.0.4-0.20180629152535-a114f312e075 h1:bfSj+hHTrZKbMJHEldrx659CxsvTHbAznKTfb42l2M4=
github.com/spf13/cobra v0.0.4-0.20180629152535-a114f312e075/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.1 h1:aCvUg6QPl3ibpQUxyLkrEkCHtPqYJL4x9AuhqVqFis4=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tidwall/gjson v1.1.3 h1:u4mspaByxY+Qk4U1QYYVzGFI8qxN/3jtEV0ZDb2vRic=
Expand Down
Loading