Skip to content

Commit

Permalink
Cherry-pick #8292 to 6.x: Added 'query' parameter to metricbeats glob…
Browse files Browse the repository at this point in the history
…al configuration (#8463)

Added 'query' parameter to metricbeats global configuration (#8292)
  • Loading branch information
sayden authored Sep 28, 2018
1 parent 8beedae commit d32e631
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ https://github.com/elastic/beats/compare/v6.4.0...6.x[Check the HEAD diff]
- Added 'died' PID state to process_system metricset on system module {pull}8275[8275]
- Add `metrics` metricset to MongoDB module. {pull}7611[7611]
- Added `ccr` metricset to Elasticsearch module. {pull}8335[8335]
- Added support for query params in configuration {issue}8286[8286] {pull}8292[8292]

*Packetbeat*

Expand Down
17 changes: 17 additions & 0 deletions metricbeat/docs/metricbeat-options.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,20 @@ and then use the value in an HTTP Authorization header.

An optional base path to be used in HTTP URIs. If defined, Metricbeat will insert this value
as the first segment in the HTTP URI path.

[float]
==== `query`

An optional value to pass common query params in YAML. Instead of setting the query params
within hosts values using the syntax `?key=value&key2&value2`, you can set it here like this:

[source,yaml]
----
query:
key: value
key2: value2
list:
- 1.1
- 2.95
- -15
----
34 changes: 32 additions & 2 deletions metricbeat/mb/mb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package mb

import (
"fmt"
"net/url"
"time"

"github.com/elastic/beats/libbeat/common"
Expand Down Expand Up @@ -306,17 +307,46 @@ type ModuleConfig struct {
MetricSets []string `config:"metricsets"`
Enabled bool `config:"enabled"`
Raw bool `config:"raw"`
Query QueryParams `config:"query"`
}

func (c ModuleConfig) String() string {
return fmt.Sprintf(`{Module:"%v", MetricSets:%v, Enabled:%v, `+
`Hosts:[%v hosts], Period:"%v", Timeout:"%v", Raw:%v}`,
`Hosts:[%v hosts], Period:"%v", Timeout:"%v", Raw:%v, Query:%v}`,
c.Module, c.MetricSets, c.Enabled, len(c.Hosts), c.Period, c.Timeout,
c.Raw)
c.Raw, c.Query)
}

func (c ModuleConfig) GoString() string { return c.String() }

// QueryParams is a convenient map[string]interface{} wrapper to implement the String interface which returns the
// values in common query params format (key=value&key2=value2) which is the way that the url package expects this
// params (without the initial '?')
type QueryParams map[string]interface{}

// String returns the values in common query params format (key=value&key2=value2) which is the way that the url
// package expects this params (without the initial '?')
func (q QueryParams) String() (s string) {
u := url.Values{}

for k, v := range q {
if values, ok := v.([]interface{}); ok {
for _, innerValue := range values {
u.Add(k, fmt.Sprintf("%v", innerValue))
}
} else {
//nil values in YAML shouldn't be stringified anyhow
if v == nil {
u.Add(k, "")
} else {
u.Add(k, fmt.Sprintf("%v", v))
}
}
}

return u.Encode()
}

// defaultModuleConfig contains the default values for ModuleConfig instances.
var defaultModuleConfig = ModuleConfig{
Enabled: true,
Expand Down
25 changes: 24 additions & 1 deletion metricbeat/mb/mb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestNewModulesHostParser(t *testing.T) {
r := newTestRegistry(t)

factory := func(base BaseMetricSet) (MetricSet, error) {
return &testMetricSet{base}, nil
return &testMetricSet{BaseMetricSet: base}, nil
}

hostParser := func(m Module, rawHost string) (HostData, error) {
Expand Down Expand Up @@ -375,3 +375,26 @@ func newConfig(t testing.TB, moduleConfig interface{}) *common.Config {
}
return config
}

func TestModuleConfigQueryParams(t *testing.T) {
qp := QueryParams{
"stringKey": "value",
"intKey": 10,
"floatKey": 11.5,
"boolKey": true,
"nullKey": nil,
"arKey": []interface{}{1, 2},
}

res := qp.String()

expectedValues := []string{"stringKey=value", "intKey=10", "floatKey=11.5", "boolKey=true", "nullKey=", "arKey=1", "arKey=2"}
for _, expected := range expectedValues {
assert.Contains(t, res, expected)
}

assert.NotContains(t, res, "?")
assert.NotContains(t, res, "%")
assert.NotEqual(t, "&", res[0])
assert.NotEqual(t, "&", res[len(res)-1])
}
10 changes: 10 additions & 0 deletions metricbeat/mb/parse/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func (b URLHostParserBuilder) Build() mb.HostParser {
return mb.HostData{}, err
}

query, ok := conf["query"]
if ok {
queryMap, ok := query.(map[string]interface{})
if !ok {
return mb.HostData{}, errors.Errorf("'query' config for module %v is not a map", module.Name())
}

b.QueryParams = mb.QueryParams(queryMap).String()
}

var user, pass, path, basePath string
t, ok := conf["username"]
if ok {
Expand Down
3 changes: 3 additions & 0 deletions metricbeat/mb/parse/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package parse
import (
"testing"

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

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

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -118,6 +120,7 @@ func TestURLHostParserBuilder(t *testing.T) {
{map[string]interface{}{"basepath": "foo/"}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "/foo/"}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "foo"}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "foo"}, URLHostParserBuilder{DefaultPath: "/queryParams", QueryParams: mb.QueryParams{"key": "value"}.String()}, "http://example.com/foo/queryParams?key=value"},
}

for _, test := range cases {
Expand Down

0 comments on commit d32e631

Please sign in to comment.