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] migrate memcached module to ReporterV2 error #11415

Merged
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
16 changes: 5 additions & 11 deletions metricbeat/module/memcached/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)
Expand All @@ -85,5 +79,5 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) {

reporter.Event(mb.Event{MetricSetFields: event})

return
return nil
}
10 changes: 5 additions & 5 deletions metricbeat/module/memcached/stats/stats_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ 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)
}
}

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)
}
Expand Down