Skip to content

Commit

Permalink
Refactoring: use logger from base metricset (#11754)
Browse files Browse the repository at this point in the history
With #11106, the base `Metricset` struct started defining a `Logger()` method which returned a `*logp.Logger` for metricsets to use. With this change, each metricset no longer needs to define its own logger.

This PR updates Elastic Stack metricsets to use the base `Metricset`'s `Logger()` instead of defining their own.
  • Loading branch information
ycombinator authored Apr 11, 2019
1 parent ccfbda3 commit 6d07f89
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 67 deletions.
14 changes: 7 additions & 7 deletions metricbeat/module/elasticsearch/ccr/ccr.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.GetServiceURI())
if err != nil {
err = errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch ccr stats from a non-master node")
m.Logger().Debug("trying to fetch ccr stats from a non-master node")
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.GetServiceURI())
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -81,22 +81,22 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
ccrUnavailableMessage, err := m.checkCCRAvailability(info.Version.Number)
if err != nil {
err = errors.Wrap(err, "error determining if CCR is available")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

if ccrUnavailableMessage != "" {
if time.Since(m.lastCCRLicenseMessageTimestamp) > 1*time.Minute {
err := fmt.Errorf(ccrUnavailableMessage)
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
m.lastCCRLicenseMessageTimestamp = time.Now()
}
return
}

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -107,7 +107,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
}

if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.HostData().SanitizedURI+clusterStatsPath)
if err != nil {
err := errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch cluster stats from a non-master node")
m.Logger().Debug("trying to fetch cluster stats from a non-master node")
return
}

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -84,6 +84,6 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
}

if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
}
}
10 changes: 5 additions & 5 deletions metricbeat/module/elasticsearch/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,26 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.HostData().SanitizedURI+statsPath)
if err != nil {
err = errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch index stats from a non-master node")
m.Logger().Debug("trying to fetch index stats from a non-master node")
return
}

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.HostData().SanitizedURI)
if err != nil {
err = errors.Wrap(err, "failed to get info from Elasticsearch")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -90,7 +90,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
}

if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
}
10 changes: 5 additions & 5 deletions metricbeat/module/elasticsearch/index_recovery/index_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.GetServiceURI())
if err != nil {
err = errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch index recovery stats from a non-master node")
m.Logger().Debug("trying to fetch index recovery stats from a non-master node")
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.GetServiceURI())
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -100,7 +100,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
}

if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
}
10 changes: 5 additions & 5 deletions metricbeat/module/elasticsearch/index_summary/index_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,26 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.HostData().SanitizedURI+statsPath)
if err != nil {
err = errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch index summary stats from a non-master node")
m.Logger().Debug("trying to fetch index summary stats from a non-master node")
return
}

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.HostData().SanitizedURI+statsPath)
if err != nil {
err = errors.Wrap(err, "failed to get info from Elasticsearch")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -96,7 +96,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
}

if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
}
3 changes: 0 additions & 3 deletions metricbeat/module/elasticsearch/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package elasticsearch

import (
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
Expand All @@ -44,7 +43,6 @@ type MetricSet struct {
servicePath string
*helper.HTTP
XPack bool
Log *logp.Logger
}

// NewMetricSet creates an metric set that can be used to build other metric
Expand All @@ -69,7 +67,6 @@ func NewMetricSet(base mb.BaseMetricSet, servicePath string) (*MetricSet, error)
servicePath,
http,
config.XPack,
logp.NewLogger(ModuleName),
}

ms.SetServiceURI(servicePath)
Expand Down
10 changes: 5 additions & 5 deletions metricbeat/module/elasticsearch/ml_job/ml_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.GetServiceURI())
if err != nil {
err = errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch machine learning job stats from a non-master node")
m.Logger().Debug("trying to fetch machine learning job stats from a non-master node")
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.GetServiceURI())
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -83,7 +83,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -94,7 +94,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
}

if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
}
6 changes: 3 additions & 3 deletions metricbeat/module/elasticsearch/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
func (m *MetricSet) Fetch(r mb.ReporterV2) {
content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.HostData().SanitizedURI+nodeStatsPath)
if err != nil {
err = errors.Wrap(err, "failed to get info from Elasticsearch")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

err = eventsMapping(r, *info, content)
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}
}
8 changes: 4 additions & 4 deletions metricbeat/module/elasticsearch/node_stats/node_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,26 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
func (m *MetricSet) Fetch(r mb.ReporterV2) {
content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.GetServiceURI())
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

if m.XPack {
err = eventsMappingXPack(r, m, *info, content)
if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
} else {
err = eventsMapping(r, *info, content)
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}
}
Expand Down
10 changes: 5 additions & 5 deletions metricbeat/module/elasticsearch/pending_tasks/pending_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,31 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.GetServiceURI())
if err != nil {
err := errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch pending tasks from a non-master node")
m.Logger().Debug("trying to fetch pending tasks from a non-master node")
return
}

info, err := elasticsearch.GetInfo(m.HTTP, m.GetServiceURI())
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

err = eventsMapping(r, *info, content)
if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
}
8 changes: 4 additions & 4 deletions metricbeat/module/elasticsearch/shard/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
isMaster, err := elasticsearch.IsMaster(m.HTTP, m.HostData().SanitizedURI+statePath)
if err != nil {
err := errors.Wrap(err, "error determining if connected Elasticsearch node is master")
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

// Not master, no event sent
if !isMaster {
m.Log.Debug("trying to fetch shard stats from a non-master node")
m.Logger().Debug("trying to fetch shard stats from a non-master node")
return
}

content, err := m.HTTP.FetchContent()
if err != nil {
elastic.ReportAndLogError(err, r, m.Log)
elastic.ReportAndLogError(err, r, m.Logger())
return
}

Expand All @@ -80,7 +80,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) {
}

if err != nil {
m.Log.Error(err)
m.Logger().Error(err)
return
}
}
Loading

0 comments on commit 6d07f89

Please sign in to comment.