Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metricbeat] Introduce Logger per metricset #11106

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, though I would have went with a shorter Log() name in effort to keep the code lines shorter like (ms.Log().Info(...)).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewkroh I like that, will open a follow up PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name updated here: #11126

}

// 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