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

Refactoring: extracting helper function for broader usage #7693

Merged
merged 1 commit into from
Jul 24, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
// specific language governing permissions and limitations
// under the License.

package xpack
package elastic

import (
"fmt"
"strings"

"github.com/elastic/beats/metricbeat/mb"
)

// Product supported by X-Pack Monitoring
Expand All @@ -38,7 +41,7 @@ const (
Beats
)

func (p Product) String() string {
func (p Product) xPackMonitoringIndexString() string {
indexProductNames := []string{
"es",
"kibana",
Expand All @@ -53,10 +56,33 @@ func (p Product) String() string {
return indexProductNames[p]
}

// MakeMonitoringIndexName method returns the name of the monitoring index for
func (p Product) String() string {
productNames := []string{
"elasticsearch",
"kibana",
"logstash",
"beats",
}

if int(p) < 0 || int(p) > len(productNames) {
panic("Unknown product")
}

return productNames[p]
}

// MakeXPackMonitoringIndexName method returns the name of the monitoring index for
// a given product { elasticsearch, kibana, logstash, beats }
func MakeMonitoringIndexName(product Product) string {
func MakeXPackMonitoringIndexName(product Product) string {
const version = "6"

return fmt.Sprintf(".monitoring-%v-%v-mb", product, version)
return fmt.Sprintf(".monitoring-%v-%v-mb", product.xPackMonitoringIndexString(), version)
}

// ReportErrorForMissingField reports and returns an error message for the given
// field being missing in API response received from a given product
func ReportErrorForMissingField(field string, product Product, r mb.ReporterV2) error {
err := fmt.Errorf("Could not find field '%v' in %v stats API response", field, strings.Title(product.String()))
r.Error(err)
return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
// specific language governing permissions and limitations
// under the License.

package xpack
package elastic

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/metricbeat/mb"
)

func TestMakeMonitoringIndexName(t *testing.T) {
func TestMakeXPackMonitoringIndexName(t *testing.T) {
tests := []struct {
Name string
Product Product
Expand Down Expand Up @@ -55,8 +57,33 @@ func TestMakeMonitoringIndexName(t *testing.T) {
for _, test := range tests {
name := fmt.Sprintf("Test naming %v", test.Name)
t.Run(name, func(t *testing.T) {
indexName := MakeMonitoringIndexName(test.Product)
indexName := MakeXPackMonitoringIndexName(test.Product)
assert.Equal(t, test.Expected, indexName)
})
}
}

type MockReporterV2 struct {
mb.ReporterV2
}

func (MockReporterV2) Event(event mb.Event) bool {
return true
}

var currentErr error // This hack is necessary because the Error method below cannot receive the type *MockReporterV2

func (m MockReporterV2) Error(err error) bool {
currentErr = err
return true
}

func TestReportErrorForMissingField(t *testing.T) {
field := "some.missing.field"
r := MockReporterV2{}
err := ReportErrorForMissingField(field, Elasticsearch, r)

expectedError := fmt.Errorf("Could not find field '%v' in Elasticsearch stats API response", field)
assert.Equal(t, expectedError, err)
assert.Equal(t, expectedError, currentErr)
}
4 changes: 2 additions & 2 deletions metricbeat/module/elasticsearch/node_stats/data_xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
s "github.com/elastic/beats/libbeat/common/schema"
c "github.com/elastic/beats/libbeat/common/schema/mapstriface"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper/xpack"
"github.com/elastic/beats/metricbeat/helper/elastic"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/elasticsearch"
)
Expand Down Expand Up @@ -230,7 +230,7 @@ func eventsMappingXPack(r mb.ReporterV2, m *MetricSet, content []byte) {
"node_stats": nodeData,
}

event.Index = xpack.MakeMonitoringIndexName(xpack.Elasticsearch)
event.Index = elastic.MakeXPackMonitoringIndexName(elastic.Elasticsearch)
r.Event(event)
}
}
32 changes: 0 additions & 32 deletions metricbeat/module/kibana/kibana.go

This file was deleted.

52 changes: 0 additions & 52 deletions metricbeat/module/kibana/kibana_test.go

This file was deleted.

8 changes: 4 additions & 4 deletions metricbeat/module/kibana/stats/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/elastic/beats/libbeat/common"
s "github.com/elastic/beats/libbeat/common/schema"
c "github.com/elastic/beats/libbeat/common/schema/mapstriface"
"github.com/elastic/beats/metricbeat/helper/elastic"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/kibana"
)

var (
Expand Down Expand Up @@ -99,18 +99,18 @@ func eventMapping(r mb.ReporterV2, content []byte) error {
// Set elasticsearch cluster id
elasticsearchClusterID, ok := data["cluster_uuid"]
if !ok {
return kibana.ReportErrorForMissingField("cluster_uuid", r)
return elastic.ReportErrorForMissingField("cluster_uuid", elastic.Kibana, r)
}
event.RootFields.Put("elasticsearch.cluster.id", elasticsearchClusterID)

// Set process PID
process, ok := data["process"].(map[string]interface{})
if !ok {
return kibana.ReportErrorForMissingField("process", r)
return elastic.ReportErrorForMissingField("process", elastic.Kibana, r)
}
pid, ok := process["pid"].(float64)
if !ok {
return kibana.ReportErrorForMissingField("process.pid", r)
return elastic.ReportErrorForMissingField("process.pid", elastic.Kibana, r)
}
event.RootFields.Put("process.pid", int(pid))

Expand Down
11 changes: 5 additions & 6 deletions metricbeat/module/kibana/stats/data_xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
"github.com/elastic/beats/libbeat/common"
s "github.com/elastic/beats/libbeat/common/schema"
c "github.com/elastic/beats/libbeat/common/schema/mapstriface"
"github.com/elastic/beats/metricbeat/helper/xpack"
"github.com/elastic/beats/metricbeat/helper/elastic"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/module/kibana"
)

var (
Expand Down Expand Up @@ -156,16 +155,16 @@ func eventMappingXPack(r mb.ReporterV2, intervalMs int64, content []byte) error

process, ok := data["process"].(map[string]interface{})
if !ok {
return kibana.ReportErrorForMissingField("process", r)
return elastic.ReportErrorForMissingField("process", elastic.Kibana, r)
}
memory, ok := process["memory"].(map[string]interface{})
if !ok {
return kibana.ReportErrorForMissingField("process.memory", r)
return elastic.ReportErrorForMissingField("process.memory", elastic.Kibana, r)
}

rss, ok := memory["resident_set_size_bytes"].(float64)
if !ok {
return kibana.ReportErrorForMissingField("process.memory.resident_set_size_bytes", r)
return elastic.ReportErrorForMissingField("process.memory.resident_set_size_bytes", elastic.Kibana, r)
}
kibanaStatsFields.Put("process.memory.resident_set_size_in_bytes", int64(rss))

Expand All @@ -181,7 +180,7 @@ func eventMappingXPack(r mb.ReporterV2, intervalMs int64, content []byte) error
"kibana_stats": kibanaStatsFields,
}

event.Index = xpack.MakeMonitoringIndexName(xpack.Kibana)
event.Index = elastic.MakeXPackMonitoringIndexName(elastic.Kibana)
r.Event(event)

return nil
Expand Down