Skip to content

Commit

Permalink
Additional logging + improvements
Browse files Browse the repository at this point in the history
Follow-up to #766
- Shift linting name logging to debug logging. Instead of sending everything to stdout, we can now have a cleaner "report"
- Missed some `log.Printf`(alias for Debug) in helm code, shifted that to info logs
- Added some caching logs, whether or not it's a cache hit + time elasped to calculate hash
- Changed `time` for `duration_ms` in env loading log. This conflicts with the log time
  • Loading branch information
julienduchesne committed Sep 30, 2022
1 parent cc21323 commit 3343606
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
9 changes: 7 additions & 2 deletions cmd/tk/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ func lintCmd() *cli.Command {

exclude := cmd.Flags().StringSliceP("exclude", "e", []string{"**/.*", ".*", "**/vendor/**", "vendor/**"}, "globs to exclude")
parallelism := cmd.Flags().IntP("parallelism", "n", 4, "amount of workers")
verbose := cmd.Flags().BoolP("verbose", "v", false, "print each checked file")

// shifted to debug logs
cmd.Flags().BoolP("verbose", "v", false, "print each checked file")
if err := cmd.Flags().MarkDeprecated("verbose", "logs are sent to debug now, this is unused"); err != nil {
panic(err)
}

cmd.Run = func(cmd *cli.Command, args []string) error {
globs := make([]glob.Glob, len(*exclude))
Expand All @@ -39,7 +44,7 @@ func lintCmd() *cli.Command {
globs[i] = g
}

return jsonnet.Lint(args, &jsonnet.LintOpts{Excludes: globs, PrintNames: *verbose, Parallelism: *parallelism})
return jsonnet.Lint(args, &jsonnet.LintOpts{Excludes: globs, Parallelism: *parallelism})
}

return cmd
Expand Down
14 changes: 7 additions & 7 deletions pkg/helm/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c Charts) Vendor(prune bool) error {
}

if chartYAML.Version.String() == r.Version.String() {
log.Info().Msgf(" %s exists", r)
log.Info().Msgf("%s exists", r)
continue
} else {
log.Info().Msgf("Removing %s", r)
Expand Down Expand Up @@ -174,7 +174,7 @@ func (c Charts) Vendor(prune bool) error {
return err
}

log.Info().Msgf(" %s@%s downloaded", r.Chart, r.Version.String())
log.Info().Msgf("%s@%s downloaded", r.Chart, r.Version.String())
}

if prune {
Expand Down Expand Up @@ -202,7 +202,7 @@ func (c Charts) Vendor(prune bool) error {
// Add adds every Chart in reqs to the Manifest after validation, and runs
// Vendor afterwards
func (c *Charts) Add(reqs []string) error {
log.Printf("Adding %v Charts ...", len(reqs))
log.Info().Msgf("Adding %v Charts ...", len(reqs))

// parse new charts, append in memory
requirements := c.Manifest.Requires
Expand All @@ -219,7 +219,7 @@ func (c *Charts) Add(reqs []string) error {
}

requirements = append(requirements, *r)
log.Info().Msgf(" OK: %s", s)
log.Info().Msgf("OK: %s", s)
}

if err := requirements.Validate(); err != nil {
Expand All @@ -239,7 +239,7 @@ func (c *Charts) Add(reqs []string) error {
}

// worked fine? vendor it
log.Printf("Added %v Charts to helmfile.yaml. Vendoring ...", added)
log.Info().Msgf("Added %v Charts to helmfile.yaml. Vendoring ...", added)
return c.Vendor(false)
}

Expand All @@ -258,7 +258,7 @@ func (c *Charts) AddRepos(repos ...Repo) error {

c.Manifest.Repositories = append(c.Manifest.Repositories, r)
added++
log.Info().Msgf(" OK: %s", r.Name)
log.Info().Msgf("OK: %s", r.Name)
}

// write out
Expand Down Expand Up @@ -346,5 +346,5 @@ func parseReqName(s string) string {
}

func skip(s string, err error) {
log.Printf(" Skipping %s: %s.", s, err)
log.Info().Msgf("Skipping %s: %s.", s, err)
}
6 changes: 6 additions & 0 deletions pkg/jsonnet/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package jsonnet
import (
"os"
"regexp"
"time"

jsonnet "github.com/google/go-jsonnet"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"

"github.com/grafana/tanka/pkg/jsonnet/jpath"
"github.com/grafana/tanka/pkg/jsonnet/native"
Expand Down Expand Up @@ -141,14 +143,18 @@ func evaluateSnippet(evalFunc evalFunc, path, data string, opts Opts) (string, e

var hash string
if cache != nil {
startTime := time.Now()
if hash, err = getSnippetHash(vm, path, data); err != nil {
return "", err
}
cacheLog := log.Debug().Str("path", path).Str("hash", hash).Dur("duration_ms", time.Since(startTime))
if v, err := cache.Get(hash); err != nil {
return "", err
} else if v != "" {
cacheLog.Bool("cache_hit", true).Msg("computed snippet hash")
return v, nil
}
cacheLog.Bool("cache_hit", false).Msg("computed snippet hash")
}

content, err := evalFunc(vm)
Expand Down
11 changes: 5 additions & 6 deletions pkg/jsonnet/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/gobwas/glob"
"github.com/google/go-jsonnet/linter"
"github.com/grafana/tanka/pkg/jsonnet/jpath"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

// LintOpts modifies the behaviour of Lint
Expand All @@ -18,9 +20,6 @@ type LintOpts struct {
// files
Excludes []glob.Glob

// PrintNames causes all filenames to be printed
PrintNames bool

// Parallelism determines the number of workers that will process files
Parallelism int
}
Expand Down Expand Up @@ -54,9 +53,8 @@ func Lint(fds []string, opts *LintOpts) error {
continue
}

if opts.PrintNames {
fmt.Printf("Linting %s...\n", file)
}
log.Debug().Str("file", file).Msg("linting file")
startTime := time.Now()

vm := MakeVM(Opts{})
jpaths, _, _, err := jpath.Resolve(file, true)
Expand All @@ -71,6 +69,7 @@ func Lint(fds []string, opts *LintOpts) error {
content, _ := os.ReadFile(file)
failed := linter.LintSnippet(vm, buf, []linter.Snippet{{FileName: file, Code: string(content)}})
resultCh <- result{failed: failed, output: buf.String()}
log.Debug().Str("file", file).Dur("duration_ms", time.Since(startTime)).Msg("linted file")
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/tanka/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ func parallelWorker(jobsCh <-chan parallelJob, outCh chan parallelOut) {
}
outCh <- parallelOut{env: env, err: err}

log.Debug().Str("name", job.opts.Name).Str("path", job.path).Dur("time", time.Since(startTime)).Msg("Finished loading environment")
log.Debug().Str("name", job.opts.Name).Str("path", job.path).Dur("duration_ms", time.Since(startTime)).Msg("Finished loading environment")
}
}

0 comments on commit 3343606

Please sign in to comment.