diff --git a/metricbeat/module/ceph/monitor_health/_meta/data.json b/metricbeat/module/ceph/monitor_health/_meta/data.json index 3557141daf40..8f2a9159b511 100644 --- a/metricbeat/module/ceph/monitor_health/_meta/data.json +++ b/metricbeat/module/ceph/monitor_health/_meta/data.json @@ -1,22 +1,18 @@ { - "@timestamp": "2017-10-12T08:05:34.853Z", - "agent": { - "hostname": "host.example.com", - "name": "host.example.com" - }, + "@timestamp": "2019-03-01T08:05:34.853Z", "ceph": { "monitor_health": { "available": { - "kb": 46814916, - "pct": 76 + "kb": 773013080, + "pct": 82 }, "health": "HEALTH_OK", - "last_updated": "2019-01-25T12:37:24.921587Z", - "name": "f0e2841bb524", + "last_updated": "2019-03-13T11:21:24.667025Z", + "name": "26c372192772", "store_stats": { "last_updated": "0.000000", "log": { - "bytes": 2031616 + "bytes": 4128768 }, "misc": { "bytes": 65552 @@ -25,14 +21,14 @@ "bytes": 1087 }, "total": { - "bytes": 2098255 + "bytes": 4195407 } }, "total": { - "kb": 61255492 + "kb": 936145620 }, "used": { - "kb": 11299252 + "kb": 115509168 } } }, @@ -45,7 +41,7 @@ "name": "monitor_health" }, "service": { - "address": "127.0.0.1:5000", + "address": "127.0.0.1:55555", "type": "ceph" } } \ No newline at end of file diff --git a/metricbeat/module/ceph/monitor_health/_meta/testdata/config.yml b/metricbeat/module/ceph/monitor_health/_meta/testdata/config.yml new file mode 100644 index 000000000000..5a461d8aa1e0 --- /dev/null +++ b/metricbeat/module/ceph/monitor_health/_meta/testdata/config.yml @@ -0,0 +1,2 @@ +type: http +url: "/api/v0.1/health" diff --git a/metricbeat/module/ceph/monitor_health/_meta/testdata/docs.json b/metricbeat/module/ceph/monitor_health/_meta/testdata/docs.json new file mode 100644 index 000000000000..bc4a393c7686 --- /dev/null +++ b/metricbeat/module/ceph/monitor_health/_meta/testdata/docs.json @@ -0,0 +1 @@ +{"status": "OK", "output": {"detail": [], "timechecks": {"round_status": "finished", "epoch": 3, "round": 0}, "health": {"health_services": [{"mons": [{"last_updated": "2019-03-13 11:21:24.667025", "name": "26c372192772", "avail_percent": 82, "kb_total": 936145620, "kb_avail": 773013080, "health": "HEALTH_OK", "kb_used": 115509168, "store_stats": {"bytes_total": 4195407, "bytes_log": 4128768, "last_updated": "0.000000", "bytes_misc": 65552, "bytes_sst": 1087}}]}]}, "overall_status": "HEALTH_OK", "summary": []}} diff --git a/metricbeat/module/ceph/monitor_health/_meta/testdata/docs.json-expected.json b/metricbeat/module/ceph/monitor_health/_meta/testdata/docs.json-expected.json new file mode 100644 index 000000000000..b6a63a9d7488 --- /dev/null +++ b/metricbeat/module/ceph/monitor_health/_meta/testdata/docs.json-expected.json @@ -0,0 +1,48 @@ +[ + { + "ceph": { + "monitor_health": { + "available": { + "kb": 773013080, + "pct": 82 + }, + "health": "HEALTH_OK", + "last_updated": "2019-03-13T11:21:24.667025Z", + "name": "26c372192772", + "store_stats": { + "last_updated": "0.000000", + "log": { + "bytes": 4128768 + }, + "misc": { + "bytes": 65552 + }, + "sst": { + "bytes": 1087 + }, + "total": { + "bytes": 4195407 + } + }, + "total": { + "kb": 936145620 + }, + "used": { + "kb": 115509168 + } + } + }, + "event": { + "dataset": "ceph.monitor_health", + "duration": 115000, + "module": "ceph" + }, + "metricset": { + "name": "monitor_health" + }, + "service": { + "address": "127.0.0.1:55555", + "type": "ceph" + } + } +] \ No newline at end of file diff --git a/metricbeat/module/ceph/monitor_health/data.go b/metricbeat/module/ceph/monitor_health/data.go index c4ed630c3859..7ca509ea212e 100644 --- a/metricbeat/module/ceph/monitor_health/data.go +++ b/metricbeat/module/ceph/monitor_health/data.go @@ -21,8 +21,9 @@ import ( "encoding/json" "time" + "github.com/pkg/errors" + "github.com/elastic/beats/libbeat/common" - "github.com/elastic/beats/libbeat/logp" ) type Tick struct { @@ -85,11 +86,11 @@ type HealthRequest struct { Output Output `json:"output"` } -func eventsMapping(content []byte) []common.MapStr { +func eventsMapping(content []byte) ([]common.MapStr, error) { var d HealthRequest err := json.Unmarshal(content, &d) if err != nil { - logp.Err("Error: %+v", err) + return nil, errors.Wrapf(err, "could not get HealthRequest data") } events := []common.MapStr{} @@ -131,5 +132,5 @@ func eventsMapping(content []byte) []common.MapStr { } } - return events + return events, nil } diff --git a/metricbeat/module/ceph/monitor_health/monitor_health.go b/metricbeat/module/ceph/monitor_health/monitor_health.go index 1828c8697d32..16f7b4373243 100644 --- a/metricbeat/module/ceph/monitor_health/monitor_health.go +++ b/metricbeat/module/ceph/monitor_health/monitor_health.go @@ -18,7 +18,6 @@ package monitor_health import ( - "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" @@ -61,11 +60,23 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { }, nil } -func (m *MetricSet) Fetch() ([]common.MapStr, 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) error { content, err := m.HTTP.FetchContent() if err != nil { - return nil, err + return err + } + + events, err := eventsMapping(content) + if err != nil { + return err + } + + for _, event := range events { + reporter.Event(mb.Event{MetricSetFields: event}) } - return eventsMapping(content), nil + return nil } diff --git a/metricbeat/module/ceph/monitor_health/monitor_health_integration_test.go b/metricbeat/module/ceph/monitor_health/monitor_health_integration_test.go deleted file mode 100644 index 662e0b580d4e..000000000000 --- a/metricbeat/module/ceph/monitor_health/monitor_health_integration_test.go +++ /dev/null @@ -1,65 +0,0 @@ -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you under -// the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package monitor_health - -import ( - "fmt" - "os" - "testing" - - mbtest "github.com/elastic/beats/metricbeat/mb/testing" -) - -func TestData(t *testing.T) { - f := mbtest.NewEventsFetcher(t, getConfig()) - err := mbtest.WriteEvents(f, t) - if err != nil { - t.Fatal("write", err) - } -} - -func getConfig() map[string]interface{} { - return map[string]interface{}{ - "module": "ceph", - "metricsets": []string{"monitor_health"}, - "hosts": getTestCephHost(), - } -} - -const ( - cephDefaultHost = "127.0.0.1" - cephDefaultPort = "5000" -) - -func getTestCephHost() string { - return fmt.Sprintf("%v:%v", - getenv("CEPH_HOST", cephDefaultHost), - getenv("CEPH_PORT", cephDefaultPort), - ) -} - -func getenv(name, defaultValue string) string { - return strDefault(os.Getenv(name), defaultValue) -} - -func strDefault(a, defaults string) string { - if len(a) == 0 { - return defaults - } - return a -} diff --git a/metricbeat/module/ceph/monitor_health/monitor_health_test.go b/metricbeat/module/ceph/monitor_health/monitor_health_test.go index 6c9ad8a41ee6..93c8e3bac0e1 100644 --- a/metricbeat/module/ceph/monitor_health/monitor_health_test.go +++ b/metricbeat/module/ceph/monitor_health/monitor_health_test.go @@ -32,6 +32,7 @@ import ( func TestFetchEventContents(t *testing.T) { absPath, err := filepath.Abs("../_meta/testdata/") + assert.NoError(t, err) response, err := ioutil.ReadFile(absPath + "/sample_response.json") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -47,12 +48,13 @@ func TestFetchEventContents(t *testing.T) { "hosts": []string{server.URL}, } - f := mbtest.NewEventsFetcher(t, config) - events, err := f.Fetch() - if err != nil { - t.Fatal(err) + f := mbtest.NewReportingMetricSetV2Error(t, config) + events, errs := mbtest.ReportingFetchV2Error(f) + if len(errs) > 0 { + t.Fatalf("Expected 0 error, had %d. %v\n", len(errs), errs) } - event := events[0] + assert.NotEmpty(t, events) + event := events[0].MetricSetFields t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint())