From 96df1c296b22ebdff5da9f6e0a7276ae05239805 Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Wed, 12 Feb 2025 21:07:52 +0100 Subject: [PATCH] add debugg logs Signed-off-by: Pablo Chacin --- analyze.go | 37 ++++++++++++++++++++++++++++++++++++- cmd/state.go | 1 + command.go | 6 ++++++ provision.go | 5 +++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/analyze.go b/analyze.go index fb22b3e..01a6c5c 100644 --- a/analyze.go +++ b/analyze.go @@ -1,6 +1,7 @@ package k6exec import ( + "log/slog" "os" "strings" @@ -8,7 +9,18 @@ import ( ) func analyze(args []string, opts *Options) (k6deps.Dependencies, error) { - return k6deps.Analyze(newDepsOptions(args, opts)) + depsOpts := newDepsOptions(args, opts) + + // we call Analyze before logging because it will return the name of the manifest, in any + deps, err := k6deps.Analyze(depsOpts) + + slog.Debug("analyzing sources", depsOptsAttrs(depsOpts)...) + + if err == nil && len(deps) > 0 { + slog.Debug("found dependencies", "deps", deps.String()) + } + + return deps, err } func newDepsOptions(args []string, opts *Options) *k6deps.Options { @@ -58,3 +70,26 @@ func scriptArg(args []string) (string, bool) { return last, true } + +func depsOptsAttrs(opts *k6deps.Options) []any { + attrs := []any{} + + if opts.Manifest.Name != "" { + attrs = append(attrs, "Manifest", opts.Manifest.Name) + } + + if opts.Archive.Name != "" { + attrs = append(attrs, "Archive", opts.Archive.Name) + } + + // ignore script if archive is present + if opts.Archive.Name == "" && opts.Script.Name != "" { + attrs = append(attrs, "Script", opts.Script.Name) + } + + if opts.Env.Name != "" { + attrs = append(attrs, "Env", opts.Env.Name) + } + + return attrs +} diff --git a/cmd/state.go b/cmd/state.go index dca6e24..b58ad5f 100644 --- a/cmd/state.go +++ b/cmd/state.go @@ -142,6 +142,7 @@ func (s *state) runE(_ *cobra.Command, _ []string) error { } }() + slog.Debug("running", "k6 binary", s.cmd.Path, "args", s.cmd.Args[1:]) err = s.cmd.Run() return err diff --git a/command.go b/command.go index 4b6a0e0..c22711b 100644 --- a/command.go +++ b/command.go @@ -2,6 +2,7 @@ package k6exec import ( "context" + "log/slog" "os/exec" ) @@ -17,11 +18,16 @@ func Command(ctx context.Context, args []string, opts *Options) (*exec.Cmd, func return nil, nil, err } + slog.Info("provisioning k6 binary") + exe, err := provision(ctx, deps, opts) if err != nil { return nil, nil, err } + // FIXME: can we leak sensitive information in arguments here? (pablochacin) + slog.Debug("running k6", "path", exe, "args", args) + cmd := exec.CommandContext(ctx, exe, args...) //nolint:gosec // TODO: once k6provider implements the cleanup of binary return the proper cleanup function (pablochacin) diff --git a/provision.go b/provision.go index 49afda6..352b261 100644 --- a/provision.go +++ b/provision.go @@ -2,6 +2,7 @@ package k6exec import ( "context" + "log/slog" "github.com/grafana/k6deps" "github.com/grafana/k6provider" @@ -20,10 +21,14 @@ func provision(ctx context.Context, deps k6deps.Dependencies, opts *Options) (st return "", err } + slog.Debug("downloading binary", "build service URL: ", opts.BuildServiceURL) + binary, err := provider.GetBinary(ctx, deps) if err != nil { return "", err } + slog.Debug("binary downloaded", "Path: ", binary.Path, "dependencies", deps.String()) + return binary.Path, nil }