Skip to content

Commit

Permalink
add debugg logs (#104)
Browse files Browse the repository at this point in the history
add debugg logs


Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin authored Feb 14, 2025
1 parent ae0bb16 commit ab668f5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
37 changes: 36 additions & 1 deletion analyze.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package k6exec

import (
"log/slog"
"os"
"strings"

"github.com/grafana/k6deps"
)

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 {
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions cmd/state_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"github.com/stretchr/testify/require"
)

func Test_interal_state(t *testing.T) { //nolint:tparallel
t.Parallel()
func Test_interal_state(t *testing.T) {
t.Setenv("K6_BUILD_SERVICE_URL", "")
t.Setenv("K6_CLOUD_TOKEN", "")

env, err := testutils.NewTestEnv(testutils.TestEnvConfig{
WorkDir: t.TempDir(),
Expand Down
6 changes: 6 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k6exec

import (
"context"
"log/slog"
"os/exec"
)

Expand All @@ -17,11 +18,16 @@ func Command(ctx context.Context, args []string, opts *Options) (*exec.Cmd, func
return nil, nil, err
}

slog.Info("fetching 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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/grafana/clireadme v0.1.0
github.com/grafana/k6build v0.5.4
github.com/grafana/k6deps v0.2.1
github.com/grafana/k6provider v0.1.9
github.com/samber/slog-logrus/v2 v2.5.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
Expand All @@ -30,7 +31,6 @@ require (
github.com/evanw/esbuild v0.24.2 // indirect
github.com/grafana/k6foundry v0.3.1 // indirect
github.com/grafana/k6pack v0.2.4 // indirect
github.com/grafana/k6provider v0.1.7
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/samber/lo v1.47.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/grafana/k6foundry v0.3.1 h1:nv4BqlJfNXrVMk7ps7mlGiPgegR73ogTvisn1y0bY
github.com/grafana/k6foundry v0.3.1/go.mod h1:4Hw0ll6ZsKN8f3cgp7I4N6EkhXafZ6CBC6fDJWkW7/Q=
github.com/grafana/k6pack v0.2.4 h1:JzbaO/NnLBaM2Shbn59WynaGAYL+jMvnjsoj/VTr3es=
github.com/grafana/k6pack v0.2.4/go.mod h1:JTG8lQRU4U4WNKkznSL6zYokviiFVIp1I9W7z7NmrLA=
github.com/grafana/k6provider v0.1.7 h1:vBHzm80u7vAa2xIocxtq/rK4fy1d0kaVR9arrGNc1n0=
github.com/grafana/k6provider v0.1.7/go.mod h1:Uvqmgg/16Dc4vm70EwRxx2teA9NMZtSPm1v9qnobtbY=
github.com/grafana/k6provider v0.1.9 h1:N9yoauRIb4JtDPBB+z3mrIdFHvo5h7S6UA9pfKdjVZg=
github.com/grafana/k6provider v0.1.9/go.mod h1:voN3IOPdqODBp7RGGawYeHNHTxdwYHb6LW/9dYtBtHw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
Expand Down
14 changes: 14 additions & 0 deletions provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package k6exec

import (
"context"
"log/slog"
"strings"

"github.com/grafana/k6deps"
"github.com/grafana/k6provider"
Expand All @@ -20,10 +22,22 @@ func provision(ctx context.Context, deps k6deps.Dependencies, opts *Options) (st
return "", err
}

slog.Debug("fetching binary", "build service URL: ", opts.BuildServiceURL)

binary, err := provider.GetBinary(ctx, deps)
if err != nil {
return "", err
}

// Cut the query string from the download URL to reduce noise in the logs
downloadURL, _, _ := strings.Cut(binary.DownloadURL, "?")
slog.Debug("binary fetched",
"Path: ", binary.Path,
"dependencies", deps.String(),
"checksum", binary.Checksum,
"cached", binary.Cached,
"download URL", downloadURL,
)

return binary.Path, nil
}

0 comments on commit ab668f5

Please sign in to comment.