Skip to content

Commit

Permalink
Add debug logging for most commands (grafana#188)
Browse files Browse the repository at this point in the history
* Add debug logging for most commands
While debugging a failing apply, I thought some debug logging would be helpful
This is very verbose for normal usecases so I also added a `--log-level` option to all commands to toggle the debug logging

* Add missing formatting

* Move down the logging initialize to workflow.go
  • Loading branch information
julienduchesne authored Oct 28, 2021
1 parent 98137a8 commit 46e33f4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 39 deletions.
3 changes: 1 addition & 2 deletions cmd/grr/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"log"
log "github.com/sirupsen/logrus"

"github.com/go-clix/cli"
"github.com/grafana/grizzly/pkg/grafana"
Expand All @@ -13,7 +13,6 @@ import (
var Version = "dev"

func main() {
log.SetFlags(log.Ltime)

rootCmd := &cli.Command{
Use: "grr",
Expand Down
64 changes: 40 additions & 24 deletions cmd/grr/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/go-clix/cli"
"github.com/grafana/grizzly/pkg/grizzly"
"github.com/grafana/grizzly/pkg/grizzly/notifier"
"github.com/spf13/pflag"
log "github.com/sirupsen/logrus"
)

func getCmd() *cli.Command {
Expand All @@ -17,11 +17,13 @@ func getCmd() *cli.Command {
Short: "retrieve resource",
Args: cli.ArgsExact(1),
}
var opts grizzly.LoggingOpts

cmd.Run = func(cmd *cli.Command, args []string) error {
uid := args[0]
return grizzly.Get(uid)
}
return cmd
return initialiseLogging(cmd, &opts)
}

func listCmd() *cli.Command {
Expand All @@ -31,7 +33,6 @@ func listCmd() *cli.Command {
Args: cli.ArgsRange(0, 1),
}
var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())
var isRemote bool
cmd.Flags().BoolVarP(&isRemote, "remote", "r", false, "list remote resources")

Expand All @@ -54,7 +55,7 @@ func listCmd() *cli.Command {

return grizzly.List(resources)
}
return cmd
return initialiseCmd(cmd, &opts)
}

func pullCmd() *cli.Command {
Expand All @@ -64,11 +65,11 @@ func pullCmd() *cli.Command {
Args: cli.ArgsExact(1),
}
var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())

cmd.Run = func(cmd *cli.Command, args []string) error {
return grizzly.Pull(args[0], opts)
}
return cmd
return initialiseCmd(cmd, &opts)
}
func showCmd() *cli.Command {
cmd := &cli.Command{
Expand All @@ -77,15 +78,15 @@ func showCmd() *cli.Command {
Args: cli.ArgsExact(1),
}
var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())

cmd.Run = func(cmd *cli.Command, args []string) error {
resources, err := grizzly.Parse(args[0], opts)
if err != nil {
return err
}
return grizzly.Show(resources)
}
return cmd
return initialiseCmd(cmd, &opts)
}

func diffCmd() *cli.Command {
Expand All @@ -95,15 +96,15 @@ func diffCmd() *cli.Command {
Args: cli.ArgsExact(1),
}
var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())

cmd.Run = func(cmd *cli.Command, args []string) error {
resources, err := grizzly.Parse(args[0], opts)
if err != nil {
return err
}
return grizzly.Diff(resources)
}
return cmd
return initialiseCmd(cmd, &opts)
}

func applyCmd() *cli.Command {
Expand All @@ -113,15 +114,15 @@ func applyCmd() *cli.Command {
Args: cli.ArgsExact(1),
}
var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())

cmd.Run = func(cmd *cli.Command, args []string) error {
resources, err := grizzly.Parse(args[0], opts)
if err != nil {
return err
}
return grizzly.Apply(resources)
}
return cmd
return initialiseCmd(cmd, &opts)
}

type jsonnetWatchParser struct {
Expand All @@ -143,9 +144,7 @@ func watchCmd() *cli.Command {
Short: "watch dir recursively for file changes and apply selected resource path",
Args: cli.ArgsExact(2),
}

var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())

cmd.Run = func(cmd *cli.Command, args []string) error {
parser := &jsonnetWatchParser{
Expand All @@ -157,7 +156,7 @@ func watchCmd() *cli.Command {

return grizzly.Watch(watchDir, parser)
}
return cmd
return initialiseCmd(cmd, &opts)
}

func previewCmd() *cli.Command {
Expand All @@ -167,7 +166,6 @@ func previewCmd() *cli.Command {
Args: cli.ArgsExact(1),
}
var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())
expires := cmd.Flags().IntP("expires", "e", 0, "when the preview should expire. Default 0 (never)")

cmd.Run = func(cmd *cli.Command, args []string) error {
Expand All @@ -182,7 +180,7 @@ func previewCmd() *cli.Command {

return grizzly.Preview(resources, previewOpts)
}
return cmd
return initialiseCmd(cmd, &opts)
}

func exportCmd() *cli.Command {
Expand All @@ -192,7 +190,7 @@ func exportCmd() *cli.Command {
Args: cli.ArgsExact(2),
}
var opts grizzly.Opts
defaultGrizzlyFlags(&opts, cmd.Flags())

cmd.Run = func(cmd *cli.Command, args []string) error {
dashboardDir := args[1]
resources, err := grizzly.Parse(args[0], opts)
Expand All @@ -201,7 +199,7 @@ func exportCmd() *cli.Command {
}
return grizzly.Export(dashboardDir, resources)
}
return cmd
return initialiseCmd(cmd, &opts)
}

func providersCmd() *cli.Command {
Expand All @@ -210,6 +208,8 @@ func providersCmd() *cli.Command {
Short: "Lists all providers registered with Grizzly",
Args: cli.ArgsExact(0),
}
var opts grizzly.LoggingOpts

cmd.Run = func(cmd *cli.Command, args []string) error {
f := "%s\t%s\n"
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
Expand All @@ -222,13 +222,29 @@ func providersCmd() *cli.Command {
}
return w.Flush()
}
return cmd
return initialiseLogging(cmd, &opts)
}

func defaultGrizzlyFlags(opts *grizzly.Opts, fs *pflag.FlagSet) {
fs.BoolVarP(&opts.Directory, "directory", "d", false, "treat resource path as a directory")
fs.StringSliceVarP(&opts.Targets, "target", "t", nil, "resources to target")
fs.StringSliceVarP(&opts.JsonnetPaths, "jpath", "J", getDefaultJsonnetFolders(), "Specify an additional library search dir (right-most wins)")
func initialiseCmd(cmd *cli.Command, opts *grizzly.Opts) *cli.Command {
cmd.Flags().BoolVarP(&opts.Directory, "directory", "d", false, "treat resource path as a directory")
cmd.Flags().StringSliceVarP(&opts.Targets, "target", "t", nil, "resources to target")
cmd.Flags().StringSliceVarP(&opts.JsonnetPaths, "jpath", "J", getDefaultJsonnetFolders(), "Specify an additional library search dir (right-most wins)")
return initialiseLogging(cmd, &opts.LoggingOpts)
}

func initialiseLogging(cmd *cli.Command, loggingOpts *grizzly.LoggingOpts) *cli.Command {
cmd.Flags().StringVarP(&loggingOpts.LogLevel, "log-level", "l", log.InfoLevel.String(), "info, debug, warning, error")
cmdRun := cmd.Run
cmd.Run = func(cmd *cli.Command, args []string) error {
logLevel, err := log.ParseLevel(loggingOpts.LogLevel)
if err != nil {
return err
}
log.SetLevel(logLevel)
return cmdRun(cmd, args)
}

return cmd
}

func getDefaultJsonnetFolders() []string {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/grafana/tanka v0.14.0
github.com/pmezard/go-difflib v1.0.0
github.com/rivo/tview v0.0.0-20200818120338-53d50e499bf9
github.com/sirupsen/logrus v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
Expand Down
10 changes: 8 additions & 2 deletions pkg/grizzly/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package grizzly

// Opts contains options for all Grizzly commands
// LoggingOpts contains logging options (used in all commands)
type LoggingOpts struct {
LogLevel string
}

// Opts contains options for most Grizzly commands
type Opts struct {
LoggingOpts
Directory bool
JsonnetPaths []string
Targets []string
}

// PreviewOpts Options to Configure a Preview
// PreviewOpts contains options to configure a preview
type PreviewOpts struct {
ExpiresSeconds int
}
4 changes: 2 additions & 2 deletions pkg/grizzly/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
_ "embed"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sort"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/grafana/tanka/pkg/jsonnet/native"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
"github.com/grafana/tanka/pkg/process"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -134,7 +134,7 @@ func ParseJsonnet(jsonnetFile string, opts Opts) (Resources, error) {
for _, m := range extracted {
handler, err := Registry.GetHandler(m.Kind())
if err != nil {
log.Println("Error getting handler", err)
log.Error("Error getting handler: ", err)
continue
}
parsedResources, err := handler.Parse(m)
Expand Down
Loading

0 comments on commit 46e33f4

Please sign in to comment.