From bacfb4412dfc149c1b3c2864240268458627dc5e Mon Sep 17 00:00:00 2001 From: berfinsari Date: Sun, 24 Mar 2019 06:45:29 +0300 Subject: [PATCH] migrate memcached module to ReporterV2 error --- metricbeat/module/memcached/stats/stats.go | 16 +++++----------- .../memcached/stats/stats_integration_test.go | 10 +++++----- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/metricbeat/module/memcached/stats/stats.go b/metricbeat/module/memcached/stats/stats.go index 398fc890e351..be66dfc3e68f 100644 --- a/metricbeat/module/memcached/stats/stats.go +++ b/metricbeat/module/memcached/stats/stats.go @@ -22,13 +22,11 @@ import ( "net" "strings" - "github.com/elastic/beats/libbeat/logp" + "github.com/pkg/errors" "github.com/elastic/beats/metricbeat/mb" ) -var logger = logp.NewLogger("memcached.stats") - func init() { mb.Registry.MustAddMetricSet("memcached", "stats", New, mb.DefaultMetricSet(), @@ -48,20 +46,16 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // Fetch methods implements the data gathering and data conversion to the right // format. It publishes the event which is then forwarded to the output. In case // of an error set the Error field of mb.Event or simply call report.Error(). -func (m *MetricSet) Fetch(reporter mb.ReporterV2) { +func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { conn, err := net.DialTimeout("tcp", m.Host(), m.Module().Config().Timeout) if err != nil { - logger.Error(err) - reporter.Error(err) - return + return errors.Wrap(err, "error in fetch") } defer conn.Close() _, err = conn.Write([]byte("stats\n")) if err != nil { - logger.Error(err) - reporter.Error(err) - return + return errors.Wrap(err, "error in connection") } scanner := bufio.NewScanner(conn) @@ -85,5 +79,5 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) { reporter.Event(mb.Event{MetricSetFields: event}) - return + return nil } diff --git a/metricbeat/module/memcached/stats/stats_integration_test.go b/metricbeat/module/memcached/stats/stats_integration_test.go index 40a0a1e6ffc5..9ac77eb4eef8 100644 --- a/metricbeat/module/memcached/stats/stats_integration_test.go +++ b/metricbeat/module/memcached/stats/stats_integration_test.go @@ -32,14 +32,14 @@ import ( func TestData(t *testing.T) { compose.EnsureUp(t, "memcached") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) - events, errs := mbtest.ReportingFetchV2(f) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) } assert.NotEmpty(t, events) - if err := mbtest.WriteEventsReporterV2(f, t, ""); err != nil { + if err := mbtest.WriteEventsReporterV2Error(f, t, ""); err != nil { t.Fatal("write", err) } } @@ -47,8 +47,8 @@ func TestData(t *testing.T) { func TestFetch(t *testing.T) { compose.EnsureUp(t, "memcached") - f := mbtest.NewReportingMetricSetV2(t, getConfig()) - events, errs := mbtest.ReportingFetchV2(f) + f := mbtest.NewReportingMetricSetV2Error(t, getConfig()) + events, errs := mbtest.ReportingFetchV2Error(f) if len(errs) > 0 { t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) }