Skip to content

Commit

Permalink
Convert to MetricsReporterV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Pérez-Aradros Herce committed May 16, 2018
1 parent 5a4a97a commit ffa4308
Show file tree
Hide file tree
Showing 5 changed files with 13,125 additions and 6,795 deletions.
5 changes: 2 additions & 3 deletions metricbeat/helper/prometheus/module.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package prometheus

import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
)
Expand Down Expand Up @@ -40,6 +39,6 @@ type prometheusMetricSet struct {
mapping *MetricsMapping
}

func (m *prometheusMetricSet) Fetch() ([]common.MapStr, error) {
return m.prometheus.GetProcessedMetrics(m.mapping)
func (m *prometheusMetricSet) Fetch(r mb.ReporterV2) {
m.prometheus.ReportProcessedMetrics(m.mapping, r)
}
16 changes: 15 additions & 1 deletion metricbeat/helper/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Prometheus interface {
GetFamilies() ([]*dto.MetricFamily, error)

GetProcessedMetrics(mapping *MetricsMapping) ([]common.MapStr, error)

ReportProcessedMetrics(mapping *MetricsMapping, r mb.ReporterV2)
}

type prometheus struct {
Expand Down Expand Up @@ -138,10 +140,22 @@ func (p *prometheus) GetProcessedMetrics(mapping *MetricsMapping) ([]common.MapS
for k, v := range mapping.ExtraFields {
event[k] = v
}

events = append(events, event)

}
return events, nil

}

func (p *prometheus) ReportProcessedMetrics(mapping *MetricsMapping, r mb.ReporterV2) {
events, err := p.GetProcessedMetrics(mapping)
if err != nil {
r.Error(err)
return
}
for _, event := range events {
r.Event(mb.Event{MetricSetFields: event})
}
}

func getEvent(m map[string]common.MapStr, labels common.MapStr) common.MapStr {
Expand Down
13 changes: 9 additions & 4 deletions metricbeat/helper/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/common"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
)

const promMetrics = `
Expand Down Expand Up @@ -241,12 +242,16 @@ func TestPrometheus(t *testing.T) {
}

for _, test := range tests {
res, err := p.GetProcessedMetrics(test.mapping)
assert.Nil(t, err, test.msg)
reporter := &mbtest.CapturingReporterV2{}
p.ReportProcessedMetrics(test.mapping, reporter)
assert.Nil(t, reporter.GetErrors(), test.msg)
// Sort slice to avoid randomness
res := reporter.GetEvents()
sort.Slice(res, func(i, j int) bool {
return res[i].String() < res[j].String()
return res[i].MetricSetFields.String() < res[j].MetricSetFields.String()
})
assert.Equal(t, test.expected, res, test.msg)
for j, ev := range res {
assert.Equal(t, test.expected[j], ev.MetricSetFields, test.msg)
}
}
}
20 changes: 11 additions & 9 deletions metricbeat/helper/prometheus/ptest/ptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"testing"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -54,13 +54,13 @@ func TestMetricSet(t *testing.T, module, metricset string, cases TestCases) {
"hosts": []string{server.URL},
}

f := mbtest.NewEventsFetcher(t, config)

events, err := f.Fetch()
assert.NoError(t, err)
f := mbtest.NewReportingMetricSetV2(t, config)
reporter := &mbtest.CapturingReporterV2{}
f.Fetch(reporter)
assert.Nil(t, reporter.GetErrors(), "Errors while fetching metrics")

if *expectedFlag {
eventsJSON, _ := json.MarshalIndent(events, "", "\t")
eventsJSON, _ := json.MarshalIndent(reporter.GetEvents(), "", "\t")
err = ioutil.WriteFile(test.ExpectedFile, eventsJSON, 0644)
assert.NoError(t, err)
}
Expand All @@ -71,17 +71,19 @@ func TestMetricSet(t *testing.T, module, metricset string, cases TestCases) {
t.Fatal(err)
}

var expectedEvents []common.MapStr
var expectedEvents []mb.Event
err = json.Unmarshal(expected, &expectedEvents)
if err != nil {
t.Fatal(err)
}

for _, event := range events {
for _, event := range reporter.GetEvents() {
// ensure the event is in expected list
found := -1
for i, expectedEvent := range expectedEvents {
if event.String() == expectedEvent.String() {
if event.RootFields.String() == expectedEvent.RootFields.String() &&
event.ModuleFields.String() == expectedEvent.ModuleFields.String() &&
event.MetricSetFields.String() == expectedEvent.MetricSetFields.String() {
found = i
break
}
Expand Down
Loading

0 comments on commit ffa4308

Please sign in to comment.