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 Dropwizard collector Metricset to use ReporterV2Error interface #11743

Merged
merged 4 commits into from
Apr 11, 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
21 changes: 11 additions & 10 deletions metricbeat/module/dropwizard/collector/_meta/data.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"@timestamp": "2017-10-12T08:05:34.853Z",
"beat": {
"hostname": "host.example.com",
"name": "host.example.com"
},
"@timestamp": "2019-03-01T08:05:34.853Z",
"dropwizard": {
"testnamespace": {
"my_gauge": {},
Expand Down Expand Up @@ -41,11 +37,16 @@
}
}
},
"event": {
"dataset": "dropwizard.testnamespace",
"duration": 115000,
"module": "dropwizard"
},
"metricset": {
"host": "dropwizard:8080",
"module": "dropwizard",
"name": "collector",
"namespace": "testnamespace",
"rtt": 115
"name": "collector"
},
"service": {
"address": "127.0.0.1:55555",
"type": "dropwizard"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: http
url: "/metrics/metrics"
omit_documented_fields_check:
- "dropwizard.testnamespace.*"
module:
namespace: testnamespace
59 changes: 59 additions & 0 deletions metricbeat/module/dropwizard/collector/_meta/testdata/docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"version": "3.0.0",
"gauges": {
"my_gauge": {
"value": null
}
},
"counters": {
"my_counter{this=that}": {
"count": 1
}
},
"histograms": {
"my_histogram": {
"count": 0,
"max": 0,
"mean": 0,
"min": 0,
"p50": 0,
"p75": 0,
"p95": 0,
"p98": 0,
"p99": 0,
"p999": 0,
"stddev": 0
}
},
"meters": {
"my_meter{this=that}": {
"count": 0,
"m15_rate": 0,
"m1_rate": 0,
"m5_rate": 0,
"mean_rate": 0,
"units": "events/second"
}
},
"timers": {
"my_timer": {
"count": 0,
"max": 0,
"mean": 0,
"min": 0,
"p50": 0,
"p75": 0,
"p95": 0,
"p98": 0,
"p99": 0,
"p999": 0,
"stddev": 0,
"m15_rate": 0,
"m1_rate": 0,
"m5_rate": 0,
"mean_rate": 0,
"duration_units": "seconds",
"rate_units": "calls/second"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[
{
"dropwizard": {
"testnamespace": {
"my_gauge": {},
"my_histogram": {
"count": 0,
"max": 0,
"mean": 0,
"min": 0,
"p50": 0,
"p75": 0,
"p95": 0,
"p98": 0,
"p99": 0,
"p999": 0,
"stddev": 0
},
"my_timer": {
"count": 0,
"duration_units": "seconds",
"m15_rate": 0,
"m1_rate": 0,
"m5_rate": 0,
"max": 0,
"mean": 0,
"mean_rate": 0,
"min": 0,
"p50": 0,
"p75": 0,
"p95": 0,
"p98": 0,
"p99": 0,
"p999": 0,
"rate_units": "calls/second",
"stddev": 0
}
}
},
"event": {
"dataset": "dropwizard.testnamespace",
"duration": 115000,
"module": "dropwizard"
},
"metricset": {
"name": "collector"
},
"service": {
"address": "127.0.0.1:55555",
"type": "dropwizard"
}
},
{
"dropwizard": {
"testnamespace": {
"my_counter": {
"count": 1
},
"my_meter": {
"count": 0,
"m15_rate": 0,
"m1_rate": 0,
"m5_rate": 0,
"mean_rate": 0,
"units": "events/second"
},
"tags": {
"this": "that"
}
}
},
"event": {
"dataset": "dropwizard.testnamespace",
"duration": 115000,
"module": "dropwizard"
},
"metricset": {
"name": "collector"
},
"service": {
"address": "127.0.0.1:55555",
"type": "dropwizard"
}
}
]
18 changes: 10 additions & 8 deletions metricbeat/module/dropwizard/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"strings"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
Expand Down Expand Up @@ -86,10 +85,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() ([]common.MapStr, error) {
func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
body, err := m.http.FetchContent()
if err != nil {
return nil, err
return err
}
dw := map[string]interface{}{}

Expand All @@ -98,17 +97,20 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) {

err = d.Decode(&dw)
if err != nil {
return nil, err
return err
}

eventList := eventMapping(dw)

// Converts hash list to slice
events := []common.MapStr{}
for _, event := range eventList {
event[mb.NamespaceKey] = m.namespace
events = append(events, event)
if reported := reporter.Event(mb.Event{
MetricSetFields: event,
Namespace: "dropwizard." + m.namespace,
}); !reported {
m.Logger().Debug("event not reported", event)
}
}

return events, err
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,29 @@ import (
func TestFetch(t *testing.T) {
compose.EnsureUp(t, "dropwizard")

f := mbtest.NewEventsFetcher(t, getConfig())
events, err := f.Fetch()
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)

hasTag := false
doesntHaveTag := false
for _, event := range events {

ok, _ := event.HasKey("my_histogram")
ok, _ := event.MetricSetFields.HasKey("my_histogram")
if ok {
_, err := event.GetValue("tags")
_, err := event.MetricSetFields.GetValue("tags")
if err == nil {
t.Fatal("write", "my_counter not supposed to have tags")
}
doesntHaveTag = true
}

ok, _ = event.HasKey("my_counter")
ok, _ = event.MetricSetFields.HasKey("my_counter")
if ok {
tagsRaw, err := event.GetValue("tags")
tagsRaw, err := event.MetricSetFields.GetValue("tags")
if err != nil {
t.Fatal("write", err)
} else {
Expand All @@ -67,23 +71,10 @@ func TestFetch(t *testing.T) {
}
assert.Equal(t, hasTag, true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we only need to check hasTag once after all for _, event := range events {} loops?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uhmmm 🤔 You are right. I'll leave it for a follow up PR, I'm looking forward to finish with the migration and this was like this already anyways 😅

assert.Equal(t, doesntHaveTag, true)
if !assert.NoError(t, err) {
t.FailNow()
}

t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), events)
}

func TestData(t *testing.T) {
compose.EnsureUp(t, "dropwizard")

f := mbtest.NewEventsFetcher(t, getConfig())
err := mbtest.WriteEvents(f, t)
if err != nil {
t.Fatal("write", err)
}
}

func getEnvHost() string {
host := os.Getenv("DROPWIZARD_HOST")

Expand Down