Skip to content

Commit

Permalink
feat: --debug CLI argument should enable debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrinds committed Jul 10, 2024
1 parent 74d2756 commit f9d62e0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 33 deletions.
9 changes: 2 additions & 7 deletions cmd/exporters/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"github.com/netapp/harvest/v2/cmd/poller/exporter"
"github.com/netapp/harvest/v2/pkg/color"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/requests"
Expand Down Expand Up @@ -165,12 +164,8 @@ func (e *InfluxDB) Export(data *matrix.Matrix) (exporter.Stats, error) {
if err = e.Metadata.LazyAddValueInt64("time", "render", time.Since(s).Microseconds()); err != nil {
e.Logger.Error().Err(err).Msg("metadata render time")
}
// in debug mode, don't actually export but write to log
if e.Options.Debug {
e.Logger.Debug().Msg("simulating export since in debug mode")
for _, m := range metrics {
e.Logger.Debug().Msgf("M= [%s%s%s]", color.Blue, m, color.End)
}
// in test mode, don't emit metrics
if e.Options.IsTest {
return stats, nil
// otherwise, to the actual export: send to the DB
} else if err = e.Emit(metrics); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/exporters/influxdb/influxdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func setupInfluxDB(t *testing.T, exporterName string) *InfluxDB {
opts := options.New()
opts.Debug = true
opts.IsTest = true

_, err := conf.LoadHarvestConfig("../../tools/doctor/testdata/testConfig.yml")
if err != nil {
Expand Down
14 changes: 0 additions & 14 deletions cmd/exporters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ func (p *Prometheus) Init() error {
p.globalPrefix = globalPrefix
}

if p.Options.Debug {
p.Logger.Debug().Msg("initialized without HTTP server since in debug mode")
return nil
}

// add HELP and TYPE tags to exported metrics if requested
if p.Params.ShouldAddMetaTags != nil && *p.Params.ShouldAddMetaTags {
p.addMetaTags = true
Expand Down Expand Up @@ -225,15 +220,6 @@ func (p *Prometheus) Export(data *matrix.Matrix) (exporter.Stats, error) {
// fix render time for metadata
d := time.Since(start)

// simulate export in debug mode
if p.Options.Debug {
p.Logger.Debug().Msg("no export since in debug mode")
for _, m := range metrics {
p.Logger.Debug().Msgf("M= %s", string(m))
}
return stats, nil
}

// store metrics in cache
key := data.UUID + "." + data.Object + "." + data.Identifier

Expand Down
12 changes: 9 additions & 3 deletions cmd/harvest/harvest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ NetApp Harvest : the swiss-army-knife for datacenter monitoring
Authors:
Georg Mey & Vachagan Gratian
Chris Grindstaff
Georg Mey
Hardik Leuva
Rahul Gupta
Vachagan Gratian
Contact:
Expand Down Expand Up @@ -538,7 +542,7 @@ Feedback
"debug",
"d",
false,
"debug mode collects data, but no HTTP daemons and no writes to DBs",
"enable debug logging (same as -loglevel 1). If both debug and loglevel are specified, loglevel wins",
)
startCmd.PersistentFlags().BoolVarP(
&opts.verbose,
Expand All @@ -559,7 +563,7 @@ Feedback
"foreground",
"f",
false,
"start poller in foreground (only one poller, implies debug mode)",
"start single poller in foreground",
)
startCmd.PersistentFlags().BoolVar(
&opts.daemon,
Expand Down Expand Up @@ -607,6 +611,8 @@ Feedback
"only start these objects (overrides collector config)",
)
_ = startCmd.PersistentFlags().MarkHidden("logtofile")
_ = startCmd.PersistentFlags().MarkHidden("verbose")
_ = startCmd.PersistentFlags().MarkHidden("trace")
}

// The management commands: start|status|stop|restart|kill
Expand Down
9 changes: 6 additions & 3 deletions cmd/poller/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type Options struct {
Poller string // name of the Poller
Daemon bool // if true, Poller is started as daemon
Debug bool // if true, Poller is started in debug mode, which means data will not be exported
Debug bool // if true, Poller is started with debug logging (same as -loglevel 1)
PromPort int // HTTP port that is assigned to Poller and can be used by the Prometheus exporter
Config string // filepath of Harvest config (defaults to "harvest.yml") can be relative or absolute path
HomePath string // path to harvest home (usually "/opt/harvest")
Expand Down Expand Up @@ -67,12 +67,10 @@ func (o *Options) MarshalZerologObject(e *zerolog.Event) {
e.Str("config", o.Config)
e.Str("confPath", o.ConfPath)
e.Bool("daemon", o.Daemon)
e.Bool("debug", o.Debug)
e.Int("profiling", o.Profiling)
e.Int("promPort", o.PromPort)
e.Str("homePath", o.HomePath)
e.Str("logPath", o.LogPath)
e.Str("logPath", o.LogPath)
e.Str("hostname", o.Hostname)
e.Bool("asup", o.Asup)
}
Expand All @@ -86,6 +84,11 @@ func (o *Options) SetDefaults() *Options {
o.LogPath = conf.GetHarvestLogPath()
o.SetConfPath(o.ConfPath)

// If both debug and loglevel are set, loglevel wins
if o.Debug && o.LogLevel == 2 {
o.LogLevel = 1
}

return o
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ func init() {

var flags = pollerCmd.Flags()
flags.StringVarP(&opts.Poller, "poller", "p", "", "Poller name as defined in config")
flags.BoolVarP(&opts.Debug, "debug", "d", false, "Debug mode, no data will be exported")
flags.BoolVarP(&opts.Debug, "debug", "d", false, "Enable debug logging (same as -loglevel 1). If both debug and loglevel are specified, loglevel wins")
flags.BoolVar(&opts.Daemon, "daemon", false, "Start as daemon")
flags.IntVarP(&opts.LogLevel, "loglevel", "l", 2, "Logging level (0=trace, 1=debug, 2=info, 3=warning, 4=error, 5=critical)")
flags.BoolVar(&opts.LogToFile, "logtofile", false, "When running in the foreground, log to file instead of stdout")
Expand Down
17 changes: 13 additions & 4 deletions docs/help/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,23 @@ harvest.yml

## How do I start Harvest in debug mode?

Use the `--debug` flag when starting a poller. In debug mode, the poller will only collect metrics, but not write to databases. Another useful flag is `--foreground`, in which case all log messages are written to the terminal. Note that you can only start one poller in foreground mode.

Finally, you can use `--loglevel=1` or `--verbose`, if you want to see a lot of log messages. For even more, you can use `--loglevel=0` or `--trace`.
Use the `--debug` flag when starting a poller to enable debug logging (`--debug` is shorthand for `--loglevel 1`).
Another useful flag is `--foreground`, which causes all log messages to be written to the terminal.
Note that you can only start one poller in foreground mode.

The amount of logged information can be controlled with the `--loglevel` flag followed by an integer value.
The integer values are as follows:
- 0: Trace
- 1: Debug
- 2: Info (default)
- 3: Warning
- 4: Error
- 5: Critical

Examples:

```
bin/harvest start $POLLER_NAME --foreground --debug --loglevel=0
bin/harvest start $POLLER_NAME --foreground --debug
or
bin/harvest start $POLLER_NAME --loglevel=1 --collectors Zapi --objects Qtree
```
Expand Down

0 comments on commit f9d62e0

Please sign in to comment.