From 7f6a14d3a61ba8b60faf8926d53a1426d15dc46f Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 22 Jul 2020 17:32:05 +0200 Subject: [PATCH 1/4] Call host parser only once --- metricbeat/mb/lightmetricset.go | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/metricbeat/mb/lightmetricset.go b/metricbeat/mb/lightmetricset.go index 2354187b4ea..abbba24bd34 100644 --- a/metricbeat/mb/lightmetricset.go +++ b/metricbeat/mb/lightmetricset.go @@ -18,9 +18,6 @@ package mb import ( - "fmt" - "net/url" - "github.com/pkg/errors" "github.com/elastic/beats/v7/libbeat/common" @@ -55,6 +52,10 @@ func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error originalFactory := registration.Factory registration.IsDefault = m.Default + // Disable the host parser, we will call it as part of the factory + originalHostParser := registration.HostParser + registration.HostParser = nil + // Light modules factory has to override defaults and reproduce builder // functionality with the resulting configuration, it does: // - Override defaults @@ -83,11 +84,9 @@ func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error base.module = module } - // 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) + // Run the host parser if there was any + if originalHostParser != nil { + base.hostData, err = originalHostParser(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) } @@ -100,18 +99,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) { From 3ac258152c5961866fb7e055409f87c628598d1f Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 22 Jul 2020 17:37:31 +0200 Subject: [PATCH 2/4] Add changelog --- CHANGELOG-developer.next.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-developer.next.asciidoc b/CHANGELOG-developer.next.asciidoc index 8f2a8b4fbf5..7eca9675e0f 100644 --- a/CHANGELOG-developer.next.asciidoc +++ b/CHANGELOG-developer.next.asciidoc @@ -55,6 +55,7 @@ The list below covers the major changes between 7.0.0-rc2 and master only. ==== Bugfixes - Stop using `mage:import` in community beats. This was ignoring the vendorized beats directory for some mage targets, using the code available in GOPATH, this causes inconsistencies and compilation problems if the version of the code in the GOPATH is different to the vendored one. Use of `mage:import` will continue to be unsupported in custom beats till beats is migrated to go modules, or mage supports vendored dependencies. {issue}13998[13998] {pull}14162[14162] +- Metricbeat module builders call host parser only once when instantiating light modules. {pull}20149[20149] ==== Added From 10bfeb4c05a17ffbfaea21ec3f7b88d6f327e184 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 22 Jul 2020 17:39:18 +0200 Subject: [PATCH 3/4] Fix comment --- metricbeat/mb/lightmetricset.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metricbeat/mb/lightmetricset.go b/metricbeat/mb/lightmetricset.go index abbba24bd34..d19d718ab94 100644 --- a/metricbeat/mb/lightmetricset.go +++ b/metricbeat/mb/lightmetricset.go @@ -52,7 +52,8 @@ func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error originalFactory := registration.Factory registration.IsDefault = m.Default - // Disable the host parser, we will call it as part of the factory + // Disable the host parser, we will call it as part of the factory so the original + // host in the base module is not modified. originalHostParser := registration.HostParser registration.HostParser = nil From 23d326f857555f469c3c820e5687e6992ff95ff9 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 22 Jul 2020 21:15:08 +0200 Subject: [PATCH 4/4] Update comments --- metricbeat/mb/lightmetricset.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/metricbeat/mb/lightmetricset.go b/metricbeat/mb/lightmetricset.go index d19d718ab94..b78b2ef997c 100644 --- a/metricbeat/mb/lightmetricset.go +++ b/metricbeat/mb/lightmetricset.go @@ -62,8 +62,7 @@ func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error // - Override defaults // - Call module factory if registered (it wouldn't have been called // if light module is really a registered mixed module) - // - Call host parser if defined (it would have already been called - // without the light module defaults) + // - Call host parser if there was one defined // - Finally, call the original factory for the registered metricset registration.Factory = func(base BaseMetricSet) (MetricSet, error) { // Override default config on base module and metricset @@ -85,7 +84,7 @@ func (m *LightMetricSet) Registration(r *Register) (MetricSetRegistration, error base.module = module } - // Run the host parser if there was any + // Run the host parser if there was anyone defined if originalHostParser != nil { base.hostData, err = originalHostParser(base.module, base.host) if err != nil {