Skip to content

Commit

Permalink
[Metricbeat] Introduce Logger per metricset (#11106)
Browse files Browse the repository at this point in the history
Currently each Metricset initialises its own logger. The selector has to be set manually. This PR moves this code to the initialisation of the Metricset, meaning each metricset has its own logger. This should reduce the code needed in each Metricset and removes errors like putting a wrong or inconsistent selectors.

It's different from before where there was a global logger for all instances of the same metricset and now each metricset instance has it's own logger. This should not make a difference.

As an example implementation the `system.fsstat` metricset was adjusted.
  • Loading branch information
ruflin authored Mar 6, 2019
1 parent cd49078 commit 30f809b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ The list below covers the major changes between 7.0.0-beta1 and master only.
- Move host name addition to a processor. {pull}10728[10728]
- The `beat.Event` accessor methods now support `@metadata` keys. {pull}10761[10761]
- Assertion for documented fields in tests fails if any of the fields in the tested event is documented as an alias. {pull}10921[10921]
- Support for Logger in the Metricset base instance. {pull}11106[11106]
2 changes: 2 additions & 0 deletions metricbeat/mb/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/monitoring"
)

Expand Down Expand Up @@ -194,6 +195,7 @@ func newBaseMetricSets(r *Register, m Module) ([]BaseMetricSet, error) {
module: m,
host: host,
metrics: metrics,
logger: logp.NewLogger(m.Name() + "." + name),
})
}
}
Expand Down
8 changes: 8 additions & 0 deletions metricbeat/mb/mb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/monitoring"
)

Expand Down Expand Up @@ -106,6 +107,7 @@ type MetricSet interface {
HostData() HostData // HostData returns the parsed host data.
Registration() MetricSetRegistration // Params used in registration.
Metrics() *monitoring.Registry // MetricSet specific metrics
Logger() *logp.Logger // MetricSet specific logger
}

// Closer is an optional interface that a MetricSet can implement in order to
Expand Down Expand Up @@ -241,6 +243,7 @@ type BaseMetricSet struct {
hostData HostData
registration MetricSetRegistration
metrics *monitoring.Registry
logger *logp.Logger
}

func (b *BaseMetricSet) String() string {
Expand All @@ -264,6 +267,11 @@ func (b *BaseMetricSet) Metrics() *monitoring.Registry {
return b.metrics
}

// Logger returns the logger.
func (b *BaseMetricSet) Logger() *logp.Logger {
return b.logger
}

// Name returns the name of the MetricSet. It should not include the name of
// the module.
func (b *BaseMetricSet) Name() string {
Expand Down
9 changes: 3 additions & 6 deletions metricbeat/module/system/fsstat/fsstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ import (
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/elastic/beats/metricbeat/module/system/filesystem"

"github.com/pkg/errors"
)

var debugf = logp.MakeDebug("system-fsstat")

func init() {
mb.Registry.MustAddMetricSet("system", "fsstat", New,
mb.WithHostParser(parse.EmptyHostParser),
Expand All @@ -56,7 +53,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
config.IgnoreTypes = filesystem.DefaultIgnoredTypes()
}
if len(config.IgnoreTypes) > 0 {
logp.Info("Ignoring filesystem types: %s", strings.Join(config.IgnoreTypes, ", "))
base.Logger().Info("Ignoring filesystem types: %s", strings.Join(config.IgnoreTypes, ", "))
}

return &MetricSet{
Expand Down Expand Up @@ -84,10 +81,10 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
for _, fs := range fss {
stat, err := filesystem.GetFileSystemStat(fs)
if err != nil {
debugf("error fetching filesystem stats for '%s': %v", fs.DirName, err)
m.Logger().Debug("error fetching filesystem stats for '%s': %v", fs.DirName, err)
continue
}
logp.Debug("fsstat", "filesystem: %s total=%d, used=%d, free=%d", stat.Mount, stat.Total, stat.Used, stat.Free)
m.Logger().Debug("filesystem: %s total=%d, used=%d, free=%d", stat.Mount, stat.Total, stat.Used, stat.Free)

totalFiles += stat.Files
totalSize += stat.Total
Expand Down

0 comments on commit 30f809b

Please # to comment.