Skip to content

Commit

Permalink
Avoid modifications of base module host
Browse files Browse the repository at this point in the history
Running the host parser in metricbeat modules modifies the host in the
base module, so host parser cannot be run a second time. Change builders
so they don't modify the host.
  • Loading branch information
jsoriano committed Jul 22, 2020
1 parent f543705 commit 3605783
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- Remove vendor folder from repository. {pull}18655[18655]
- Added SQL helper that can be used from any Metricbeat module {pull}18955[18955]
- Update Go version to 1.14.4. {pull}19753[19753]
- Metricbeat module builders don't modify the base module host, so host parsers can be called multiple times with the original host. {pull}20143[20143]
3 changes: 1 addition & 2 deletions metricbeat/mb/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,14 @@ func initMetricSets(r *Register, m Module) ([]MetricSet, error) {
}

bm.registration = registration
bm.hostData = HostData{URI: bm.host}
bm.hostData = HostData{URI: bm.host, Host: bm.host}
if registration.HostParser != nil {
bm.hostData, err = registration.HostParser(bm.Module(), bm.host)
if err != nil {
errs = append(errs, errors.Wrapf(err, "host parsing failed for %v-%v",
bm.Module().Name(), bm.Name()))
continue
}
bm.host = bm.hostData.Host
}

metricSet, err := registration.Factory(bm)
Expand Down
19 changes: 1 addition & 18 deletions metricbeat/mb/lightmetricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
package mb

import (
"fmt"
"net/url"

"github.com/pkg/errors"

"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -86,12 +83,10 @@ func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error
// At this point host parser was already run, we need to run this again
// with the overriden defaults
if registration.HostParser != nil {
host := m.useHostURISchemeIfPossible(base.host, base.hostData.URI)
base.hostData, err = registration.HostParser(base.module, host)
base.hostData, err = registration.HostParser(base.module, base.host)
if err != nil {
return nil, errors.Wrapf(err, "host parser failed on light metricset factory for '%s/%s'", m.Module, m.Name)
}
base.host = base.hostData.Host
}

return originalFactory(base)
Expand All @@ -100,18 +95,6 @@ func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error
return registration, nil
}

// useHostURISchemeIfPossible method parses given URI to extract protocol scheme and prepend it to the host.
// It prevents from skipping protocol scheme (e.g. https) while executing HostParser.
func (m *LightMetricSet) useHostURISchemeIfPossible(host, uri string) string {
u, err := url.ParseRequestURI(uri)
if err == nil {
if u.Scheme != "" {
return fmt.Sprintf("%s://%s", u.Scheme, u.Host)
}
}
return host
}

// baseModule does the configuration overrides in the base module configuration
// taking into account the light metric set default configurations
func (m *LightMetricSet) baseModule(from Module) (*BaseModule, error) {
Expand Down
3 changes: 1 addition & 2 deletions metricbeat/mb/mb.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ type HostData struct {
SanitizedURI string // A sanitized version of the URI without credentials.

// Parts of the URI.

Host string // The host and possibly port.
User string // Username
Password string // Password
Expand Down Expand Up @@ -357,7 +356,7 @@ func (b *BaseMetricSet) Module() Module {
// Host returns the hostname or other module specific value that identifies a
// specific host or service instance from which to collect metrics.
func (b *BaseMetricSet) Host() string {
return b.host
return b.hostData.Host
}

// HostData returns the parsed host data.
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/mb/mb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func TestNewModulesHostParser(t *testing.T) {

// The URI is passed through in the Host() and HostData().URI.
assert.Equal(t, uri, ms.Host())
assert.Equal(t, HostData{URI: uri}, ms.HostData())
assert.Equal(t, HostData{URI: uri, Host: uri}, ms.HostData())
})

t.Run("MetricSet with HostParser", func(t *testing.T) {
Expand Down

0 comments on commit 3605783

Please sign in to comment.