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

Move light modules to OSS #14369

Merged
merged 9 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- Need to register new processors to be used in the JS processor in their `init` functions. {pull}13509[13509]
- The custom beat generator now uses mage instead of python, `mage GenerateCustomBeat` can be used to create a new beat, and `mage vendorUpdate` to update the vendored libbeat in a custom beat. {pull}13610[13610]
- Altered all remaining uses of mapval to use the renamed and enhanced version: https://github.com/elastic/go-lookslike[go-lookslike] instead, which is a separate project. The mapval tree is now gone. {pull}14165[14165]
- Move light modules to OSS. {pull}14369[14369]

==== Bugfixes

Expand Down
10 changes: 10 additions & 0 deletions metricbeat/beater/metricbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package beater
import (
"sync"

"github.com/elastic/beats/libbeat/paths"
"github.com/elastic/beats/libbeat/common/reload"
"github.com/elastic/beats/libbeat/management"

Expand Down Expand Up @@ -69,6 +70,14 @@ func WithModuleOptions(options ...module.Option) Option {
}
}

// WithLightModules enables light modules support
func WithLightModules() Option {
return func(*Metricbeat) {
path := paths.Resolve(paths.Home, "module")
mb.Registry.SetSecondarySource(mb.NewLightModulesSource(path))
}
}

// Creator returns a beat.Creator for instantiating a new instance of the
// Metricbeat framework with the given options.
func Creator(options ...Option) beat.Creator {
Expand All @@ -90,6 +99,7 @@ func Creator(options ...Option) beat.Creator {
// )
func DefaultCreator() beat.Creator {
return Creator(
WithLightModules(),
WithModuleOptions(
module.WithMetricSetInfo(),
module.WithServiceName(),
Expand Down
1 change: 1 addition & 0 deletions metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
// been disabled to workaround the fact that Modules() will return
// the static modules (not the dynamic ones) with a start delay.
testModulesCreator = beater.Creator(
beater.WithLightModules(),
beater.WithModuleOptions(
module.WithMetricSetInfo(),
module.WithMaxStartDelay(0),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package mb

Expand All @@ -15,7 +28,6 @@ import (

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
)

const (
Expand Down Expand Up @@ -105,15 +117,15 @@ func (s *LightModulesSource) HasMetricSet(moduleName, metricSetName string) bool
}

// MetricSetRegistration obtains a registration for a light metric set
func (s *LightModulesSource) MetricSetRegistration(register *mb.Register, moduleName, metricSetName string) (mb.MetricSetRegistration, error) {
func (s *LightModulesSource) MetricSetRegistration(register *Register, moduleName, metricSetName string) (MetricSetRegistration, error) {
lightModule, err := s.loadModule(moduleName)
if err != nil {
return mb.MetricSetRegistration{}, errors.Wrapf(err, "failed to load module '%s'", moduleName)
return MetricSetRegistration{}, errors.Wrapf(err, "failed to load module '%s'", moduleName)
}

ms, found := lightModule.MetricSets[metricSetName]
if !found {
return mb.MetricSetRegistration{}, fmt.Errorf("metricset '%s/%s' not found", moduleName, metricSetName)
return MetricSetRegistration{}, fmt.Errorf("metricset '%s/%s' not found", moduleName, metricSetName)
}

return ms.Registration(register)
Expand Down Expand Up @@ -141,7 +153,7 @@ type lightModuleConfig struct {
// LightModule contains the definition of a light module
type LightModule struct {
Name string
MetricSets map[string]mb.LightMetricSet
MetricSets map[string]LightMetricSet
}

func (s *LightModulesSource) loadModule(moduleName string) (*LightModule, error) {
Expand Down Expand Up @@ -186,8 +198,8 @@ func (s *LightModulesSource) loadModuleConfig(modulePath string) (*lightModuleCo
return &moduleConfig, nil
}

func (s *LightModulesSource) loadMetricSets(moduleDirPath, moduleName string, metricSetNames []string) (map[string]mb.LightMetricSet, error) {
metricSets := make(map[string]mb.LightMetricSet)
func (s *LightModulesSource) loadMetricSets(moduleDirPath, moduleName string, metricSetNames []string) (map[string]LightMetricSet, error) {
metricSets := make(map[string]LightMetricSet)
for _, metricSet := range metricSetNames {
manifestPath := filepath.Join(moduleDirPath, metricSet, manifestYML)

Expand All @@ -203,7 +215,7 @@ func (s *LightModulesSource) loadMetricSets(moduleDirPath, moduleName string, me
return metricSets, nil
}

func (s *LightModulesSource) loadMetricSetConfig(manifestPath string) (ms mb.LightMetricSet, err error) {
func (s *LightModulesSource) loadMetricSetConfig(manifestPath string) (ms LightMetricSet, err error) {
config, err := common.LoadFile(manifestPath)
if err != nil {
return ms, errors.Wrapf(err, "failed to load metricset manifest from '%s'", manifestPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

// +build !integration

Expand All @@ -15,7 +28,6 @@ import (

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
)

// TestLightModulesAsModuleSource checks that registry correctly lists
Expand All @@ -27,7 +39,7 @@ func TestLightModulesAsModuleSource(t *testing.T) {
name string
module string
isDefault bool
hostParser mb.HostParser
hostParser HostParser
}

cases := map[string]struct {
Expand Down Expand Up @@ -77,19 +89,19 @@ func TestLightModulesAsModuleSource(t *testing.T) {
},
}

fakeMetricSetFactory := func(base mb.BaseMetricSet) (mb.MetricSet, error) {
fakeMetricSetFactory := func(base BaseMetricSet) (MetricSet, error) {
return &base, nil
}

newRegistry := func(metricSets []testMetricSet) *mb.Register {
r := mb.NewRegister()
newRegistry := func(metricSets []testMetricSet) *Register {
r := NewRegister()
for _, m := range metricSets {
opts := []mb.MetricSetOption{}
opts := []MetricSetOption{}
if m.isDefault {
opts = append(opts, mb.DefaultMetricSet())
opts = append(opts, DefaultMetricSet())
}
if m.hostParser != nil {
opts = append(opts, mb.WithHostParser(m.hostParser))
opts = append(opts, WithHostParser(m.hostParser))
}
r.MustAddMetricSet(m.module, m.name, fakeMetricSetFactory, opts...)
}
Expand Down Expand Up @@ -174,7 +186,7 @@ func TestNewModuleFromConfig(t *testing.T) {
config common.MapStr
err bool
expectedOption string
expectedQuery mb.QueryParams
expectedQuery QueryParams
expectedPeriod time.Duration
}{
"normal module": {
Expand All @@ -196,7 +208,7 @@ func TestNewModuleFromConfig(t *testing.T) {
"light module with query": {
config: common.MapStr{"module": "service", "query": common.MapStr{"param": "foo"}},
expectedOption: "test",
expectedQuery: mb.QueryParams{"param": "foo"},
expectedQuery: QueryParams{"param": "foo"},
},
"light module with custom period": {
config: common.MapStr{"module": "service", "period": "42s"},
Expand All @@ -217,7 +229,7 @@ func TestNewModuleFromConfig(t *testing.T) {
},
}

r := mb.NewRegister()
r := NewRegister()
r.MustAddMetricSet("foo", "bar", newMetricSetWithOption)
r.SetSecondarySource(NewLightModulesSource("testdata/lightmodules"))

Expand All @@ -226,7 +238,7 @@ func TestNewModuleFromConfig(t *testing.T) {
config, err := common.NewConfigFrom(c.config)
require.NoError(t, err)

module, metricSets, err := mb.NewModule(config, r)
module, metricSets, err := NewModule(config, r)
if c.err {
assert.Error(t, err)
return
Expand All @@ -248,7 +260,7 @@ func TestNewModuleFromConfig(t *testing.T) {
assert.Equal(t, c.expectedQuery, ms.Module().Config().Query)
expectedPeriod := c.expectedPeriod
if expectedPeriod == 0 {
expectedPeriod = mb.DefaultModuleConfig().Period
expectedPeriod = DefaultModuleConfig().Period
}
assert.Equal(t, expectedPeriod, ms.Module().Config().Period)
})
Expand All @@ -260,31 +272,31 @@ func TestNewModuleFromConfig(t *testing.T) {
func TestNewModulesCallModuleFactory(t *testing.T) {
logp.TestingSetup()

r := mb.NewRegister()
r := NewRegister()
r.MustAddMetricSet("foo", "bar", newMetricSetWithOption)
r.SetSecondarySource(NewLightModulesSource("testdata/lightmodules"))

called := false
r.AddModule("foo", func(base mb.BaseModule) (mb.Module, error) {
r.AddModule("foo", func(base BaseModule) (Module, error) {
called = true
return mb.DefaultModuleFactory(base)
return DefaultModuleFactory(base)
})

config, err := common.NewConfigFrom(common.MapStr{"module": "service"})
require.NoError(t, err)

_, _, err = mb.NewModule(config, r)
_, _, err = NewModule(config, r)
assert.NoError(t, err)

assert.True(t, called, "module factory must be called if registered")
}

type metricSetWithOption struct {
mb.BaseMetricSet
BaseMetricSet
Option string
}

func newMetricSetWithOption(base mb.BaseMetricSet) (mb.MetricSet, error) {
func newMetricSetWithOption(base BaseMetricSet) (MetricSet, error) {
config := struct {
Option string `config:"option"`
}{
Expand All @@ -301,4 +313,4 @@ func newMetricSetWithOption(base mb.BaseMetricSet) (mb.MetricSet, error) {
}, nil
}

func (*metricSetWithOption) Fetch(mb.ReporterV2) error { return nil }
func (*metricSetWithOption) Fetch(ReporterV2) error { return nil }
20 changes: 0 additions & 20 deletions x-pack/metricbeat/beater/metricbeat.go

This file was deleted.

5 changes: 2 additions & 3 deletions x-pack/metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/elastic/beats/metricbeat/cmd/test"
"github.com/elastic/beats/metricbeat/mb/module"
xpackcmd "github.com/elastic/beats/x-pack/libbeat/cmd"
xpackbeater "github.com/elastic/beats/x-pack/metricbeat/beater"

// Register the includes.
_ "github.com/elastic/beats/x-pack/metricbeat/include"
Expand All @@ -34,7 +33,7 @@ var RootCmd *cmd.BeatsRootCmd

var (
rootCreator = beater.Creator(
xpackbeater.WithLightModules(),
beater.WithLightModules(),
beater.WithModuleOptions(
module.WithMetricSetInfo(),
module.WithServiceName(),
Copy link
Member

@jsoriano jsoriano Nov 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, could OSS metricbeat creators be reused here now?

Expand All @@ -45,7 +44,7 @@ var (
// been disabled to workaround the fact that Modules() will return
// the static modules (not the dynamic ones) with a start delay.
testModulesCreator = beater.Creator(
xpackbeater.WithLightModules(),
beater.WithLightModules(),
beater.WithModuleOptions(
module.WithMetricSetInfo(),
module.WithMaxStartDelay(0),
Expand Down
3 changes: 1 addition & 2 deletions x-pack/metricbeat/module/aws/ebs/ebs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"

"github.com/elastic/beats/metricbeat/mb"
xpackmb "github.com/elastic/beats/x-pack/metricbeat/mb"

// Register input module and metricset
_ "github.com/elastic/beats/x-pack/metricbeat/module/aws"
Expand All @@ -18,5 +17,5 @@ import (
func init() {
// To be moved to some kind of helper
os.Setenv("BEAT_STRICT_PERMS", "false")
mb.Registry.SetSecondarySource(xpackmb.NewLightModulesSource("../../../module"))
mb.Registry.SetSecondarySource(mb.NewLightModulesSource("../../../module"))
}
3 changes: 1 addition & 2 deletions x-pack/metricbeat/module/aws/elb/elb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"

"github.com/elastic/beats/metricbeat/mb"
xpackmb "github.com/elastic/beats/x-pack/metricbeat/mb"

// Register input module and metricset
_ "github.com/elastic/beats/x-pack/metricbeat/module/aws"
Expand All @@ -18,5 +17,5 @@ import (
func init() {
// To be moved to some kind of helper
os.Setenv("BEAT_STRICT_PERMS", "false")
mb.Registry.SetSecondarySource(xpackmb.NewLightModulesSource("../../../module"))
mb.Registry.SetSecondarySource(mb.NewLightModulesSource("../../../module"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/elastic/beats/libbeat/tests/compose"
"github.com/elastic/beats/metricbeat/mb"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
xpackmb "github.com/elastic/beats/x-pack/metricbeat/mb"

// Register input module and metricset
_ "github.com/elastic/beats/metricbeat/module/prometheus"
_ "github.com/elastic/beats/metricbeat/module/prometheus/collector"
Expand All @@ -25,7 +23,7 @@ import (
func init() {
// To be moved to some kind of helper
os.Setenv("BEAT_STRICT_PERMS", "false")
mb.Registry.SetSecondarySource(xpackmb.NewLightModulesSource("../../../module"))
mb.Registry.SetSecondarySource(mb.NewLightModulesSource("../../../module"))
}

func TestFetch(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions x-pack/metricbeat/module/cockroachdb/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
xpackmb "github.com/elastic/beats/x-pack/metricbeat/mb"

// Register input module and metricset
_ "github.com/elastic/beats/metricbeat/module/prometheus"
Expand All @@ -23,7 +22,7 @@ import (
func init() {
// To be moved to some kind of helper
os.Setenv("BEAT_STRICT_PERMS", "false")
mb.Registry.SetSecondarySource(xpackmb.NewLightModulesSource("../../../module"))
mb.Registry.SetSecondarySource(mb.NewLightModulesSource("../../../module"))
}

func TestEventMapping(t *testing.T) {
Expand Down
Loading