Skip to content

Commit

Permalink
Make URLHostParserBuilder support a basepath setting (#7700)
Browse files Browse the repository at this point in the history
This will allow modules using URLHostParserBuilder to support a setting named "basepath" in their configuration. If users set this setting to some value, the value will be inserted into the HTTP URI as the first path segment.
  • Loading branch information
ycombinator authored and andrewkroh committed Jul 24, 2018
1 parent c63b135 commit 4a0c511
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ https://github.com/elastic/beats/compare/v6.2.3...master[Check the HEAD diff]
- Add envoyproxy module. {pull}7569[7569]
- Release prometheus collector metricset as GA. {pull}7660[7660]
- Add Elasticsearch `cluster_stats` metricset. {pull}7638[7638]
- Added `basepath` setting for HTTP-based metricsets {pull}7700[7700]

*Packetbeat*

Expand Down
8 changes: 7 additions & 1 deletion metricbeat/docs/metricbeat-options.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,10 @@ The password to use for basic authentication.
==== `bearer_token_file`

If defined, Metricbeat will read the contents of the file once at initialization
and then use the value in an HTTP Authorization header.
and then use the value in an HTTP Authorization header.

[float]
==== `basepath`

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.
17 changes: 8 additions & 9 deletions metricbeat/mb/parse/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ import (
// URLHostParserBuilder builds a tailored HostParser for used with host strings
// that are URLs.
type URLHostParserBuilder struct {
BasePathConfigKey string
PathConfigKey string
DefaultPath string
DefaultUsername string
DefaultPassword string
DefaultScheme string
QueryParams string
PathConfigKey string
DefaultPath string
DefaultUsername string
DefaultPassword string
DefaultScheme string
QueryParams string
}

// Build returns a new HostParser function whose behavior is influenced by the
Expand Down Expand Up @@ -82,11 +81,11 @@ func (b URLHostParserBuilder) Build() mb.HostParser {
// Normalize path
path = strings.Trim(path, "/")

t, ok = conf[b.BasePathConfigKey]
t, ok = conf["basepath"]
if ok {
basePath, ok = t.(string)
if !ok {
return mb.HostData{}, errors.Errorf("'%v' config for module %v is not a string", b.BasePathConfigKey, module.Name())
return mb.HostData{}, errors.Errorf("'basepath' config for module %v is not a string", module.Name())
}
}
// Normalize basepath
Expand Down
8 changes: 4 additions & 4 deletions metricbeat/mb/parse/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ func TestURLHostParserBuilder(t *testing.T) {
{map[string]interface{}{}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/default"},
{map[string]interface{}{"username": "guest"}, URLHostParserBuilder{}, "http://[email protected]"},
{map[string]interface{}{"username": "guest", "password": "secret"}, URLHostParserBuilder{}, "http://guest:[email protected]"},
{map[string]interface{}{"basepath": "/foo"}, URLHostParserBuilder{BasePathConfigKey: "basepath", DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "foo/"}, URLHostParserBuilder{BasePathConfigKey: "basepath", DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "/foo/"}, URLHostParserBuilder{BasePathConfigKey: "basepath", DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "foo"}, URLHostParserBuilder{BasePathConfigKey: "basepath", 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: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "foo"}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/foo/default"},
}

for _, test := range cases {
Expand Down
4 changes: 4 additions & 0 deletions metricbeat/module/kibana/stats/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
This is the `stats` metricset of the Kibana module. This stats endpoint is available in 6.4 by default.

The intention of the Kibana module is to have a minimal data set that works across Kibana versions.

=== Module-specific configuration notes

If the Kibana instance is using a basepath in its URL, you must set the `basepath` setting for this module with the same value.
7 changes: 3 additions & 4 deletions metricbeat/module/kibana/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ func init() {

var (
hostParser = parse.URLHostParserBuilder{
DefaultScheme: "http",
BasePathConfigKey: "basepath",
DefaultPath: "api/stats",
QueryParams: "extended=true", // make Kibana fetch the cluster_uuid
DefaultScheme: "http",
DefaultPath: "api/stats",
QueryParams: "extended=true", // make Kibana fetch the cluster_uuid
}.Build()
)

Expand Down

0 comments on commit 4a0c511

Please sign in to comment.