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 golang module to ReporterV2 error #11382

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
13 changes: 5 additions & 8 deletions metricbeat/module/golang/expvar/expvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
package expvar

import (
"github.com/elastic/beats/libbeat/logp"
"github.com/pkg/errors"

"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
"github.com/elastic/beats/metricbeat/module/golang"
)

var logger = logp.NewLogger("golang.expvar")

const (
defaultScheme = "http"
defaultPath = "/debug/vars"
Expand Down Expand Up @@ -85,13 +83,10 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch methods implements the data gathering and data conversion to the right format
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
func (m *MetricSet) Fetch(reporter mb.ReporterV2) {
func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
json, err := m.http.FetchJSON()

if err != nil {
logger.Error(err)
reporter.Error(err)
return
return errors.Wrap(err, "error in http fetch")
}

//flatten cmdline
Expand All @@ -101,4 +96,6 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) {
MetricSetFields: json,
Namespace: m.Module().Name() + "." + m.namespace,
})

return nil
}
8 changes: 4 additions & 4 deletions metricbeat/module/golang/expvar/expvar_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
func TestData(t *testing.T) {
compose.EnsureUp(t, "golang")

f := mbtest.NewReportingMetricSetV2(t, getConfig())
f := mbtest.NewReportingMetricSetV2Error(t, getConfig())

err := mbtest.WriteEventsReporterV2(f, t, "")
err := mbtest.WriteEventsReporterV2Error(f, t, "")
if !assert.NoError(t, err) {
t.FailNow()
}
Expand All @@ -43,9 +43,9 @@ func TestData(t *testing.T) {
func TestFetch(t *testing.T) {
compose.EnsureUp(t, "golang")

f := mbtest.NewReportingMetricSetV2(t, getConfig())
f := mbtest.NewReportingMetricSetV2Error(t, getConfig())

events, errs := mbtest.ReportingFetchV2(f)
events, errs := mbtest.ReportingFetchV2Error(f)
if len(errs) > 0 {
t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs)
}
Expand Down
13 changes: 6 additions & 7 deletions metricbeat/module/golang/heap/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package heap
import (
"encoding/json"

"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
Expand Down Expand Up @@ -76,25 +78,22 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch methods implements the data gathering and data conversion to the right format
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
func (m *MetricSet) Fetch(reporter mb.ReporterV2) {
func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
data, err := m.http.FetchContent()
if err != nil {
logger.Error(err)
reporter.Error(err)
return
return errors.Wrap(err, "error in http fetch")
}

var stats Stats

err = json.Unmarshal(data, &stats)
if err != nil {
logger.Error(err)
reporter.Error(err)
return
return errors.Wrap(err, "error unmarshalling json")
}

reporter.Event(mb.Event{
MetricSetFields: eventMapping(stats, m),
})

return nil
}
8 changes: 4 additions & 4 deletions metricbeat/module/golang/heap/heap_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
func TestData(t *testing.T) {
compose.EnsureUp(t, "golang")

f := mbtest.NewReportingMetricSetV2(t, getConfig())
f := mbtest.NewReportingMetricSetV2Error(t, getConfig())

err := mbtest.WriteEventsReporterV2(f, t, "")
err := mbtest.WriteEventsReporterV2Error(f, t, "")
if !assert.NoError(t, err) {
t.FailNow()
}
Expand All @@ -43,9 +43,9 @@ func TestData(t *testing.T) {
func TestFetch(t *testing.T) {
compose.EnsureUp(t, "golang")

f := mbtest.NewReportingMetricSetV2(t, getConfig())
f := mbtest.NewReportingMetricSetV2Error(t, getConfig())

events, errs := mbtest.ReportingFetchV2(f)
events, errs := mbtest.ReportingFetchV2Error(f)
if len(errs) > 0 {
t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs)
}
Expand Down