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

Add secondary source of modules to Metricbeat to read light modules #12465

Merged
merged 40 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c100145
Add light modules registry
jsoriano May 24, 2019
8ba9a38
Revert changes in current registry
jsoriano May 31, 2019
758afb2
Add light modules to registry
jsoriano May 31, 2019
0a6b190
Correctly override defaults
jsoriano Jun 1, 2019
c181e51
Add tests
jsoriano Jun 2, 2019
4ceab7b
Refactor module parsing
jsoriano Jun 3, 2019
3a1768e
Add Light modules support as a metricbeat option
jsoriano Jun 3, 2019
5f85188
Refactor metricset config reader for coherence with module config reader
jsoriano Jun 6, 2019
e23df08
Call module factories if registered
jsoriano Jun 6, 2019
c138347
Refactor base module creation for better error handling
jsoriano Jun 6, 2019
ae9c7e2
Add godocs
jsoriano Jun 6, 2019
d64ccb1
Add changelog entry
jsoriano Jun 6, 2019
b53acb3
Rename child register to secondary source and read to load
jsoriano Jun 7, 2019
1e9272c
Rename some internal variables to more meaningful names
jsoriano Jun 7, 2019
a25ec8c
Yet another refactor to simplify signatures
jsoriano Jun 7, 2019
0135353
Explictly check for module presence
jsoriano Jun 7, 2019
8b8521a
Recover test for existence of module
jsoriano Jun 7, 2019
29c55b2
Add context to some errors
jsoriano Jun 7, 2019
8652a0c
Fix beater
jsoriano Jun 7, 2019
db6d303
Remove references to child registry
jsoriano Jun 7, 2019
ae9e00f
Add PR reference to the changelog
jsoriano Jun 7, 2019
3f3d046
Fix test
jsoriano Jun 7, 2019
6af6437
Move light modules to x-pack
jsoriano Jun 12, 2019
af38b6b
Make hound CI happy
jsoriano Jun 12, 2019
6f38790
Make metricbeat modules test work with licensed metricbeat
jsoriano Jun 12, 2019
2af1d4c
Add missing license header
jsoriano Jun 12, 2019
49f5648
Merge remote-tracking branch 'origin/master' into metricbeat-light-mo…
jsoriano Jun 12, 2019
8722e1a
Add some sanity tests for the light metric set
jsoriano Jun 12, 2019
6bfb015
Add check for error in test
jsoriano Jun 12, 2019
53bbabd
Rename x-pack creator
jsoriano Jun 13, 2019
a70c415
Typo that caused failing test
jsoriano Jun 13, 2019
9a6c336
Add suggestions from review
jsoriano Jun 14, 2019
edcd41b
Merge remote-tracking branch 'origin/master' into metricbeat-light-mo…
jsoriano Jun 14, 2019
38ee770
Properly add the test options to x-pack metricbeat command
jsoriano Jun 14, 2019
724f41a
Use secondary source stringer to print light modules
jsoriano Jun 20, 2019
84cba87
Move metricbeat creator definitions to where they are used
jsoriano Jun 21, 2019
9e2cdb8
Add module names to base module override error
jsoriano Jun 21, 2019
d701bff
Move test cases to map with titles as key
jsoriano Jun 21, 2019
b3dbfda
Fix unsafe base module config override
jsoriano Jun 21, 2019
f175eb5
Add test for custom period
jsoriano Jun 21, 2019
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
Next Next commit
Add light modules registry
  • Loading branch information
jsoriano committed Jun 6, 2019
commit c1001455fd7208f836acbc7365c6b590794e6ed8
2 changes: 1 addition & 1 deletion metricbeat/autodiscover/builder/hints/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import "github.com/elastic/beats/metricbeat/mb"

type config struct {
Key string `config:"key"`
Registry *mb.Register
Registry mb.ModulesRegistry
}

func defaultConfig() config {
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/autodiscover/builder/hints/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const (

type metricHints struct {
Key string
Registry *mb.Register
Registry mb.ModulesRegistry
}

// NewMetricHints builds a new metrics builder based on hints
Expand Down
8 changes: 4 additions & 4 deletions metricbeat/mb/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var (
// will be unpacked into ModuleConfig structs). r is the Register where the
// ModuleFactory's and MetricSetFactory's will be obtained from. This method
// returns a Module and its configured MetricSets or an error.
func NewModule(config *common.Config, r *Register) (Module, []MetricSet, error) {
func NewModule(config *common.Config, r ModulesRegistry) (Module, []MetricSet, error) {
if !config.Enabled() {
return nil, nil, ErrModuleDisabled
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func newBaseModuleFromConfig(rawConfig *common.Config) (BaseModule, error) {
return baseModule, nil
}

func createModule(r *Register, bm BaseModule) (Module, error) {
func createModule(r ModulesRegistry, bm BaseModule) (Module, error) {
f := r.moduleFactory(bm.Name())
if f == nil {
f = DefaultModuleFactory
Expand All @@ -106,7 +106,7 @@ func createModule(r *Register, bm BaseModule) (Module, error) {
return f(bm)
}

func initMetricSets(r *Register, m Module) ([]MetricSet, error) {
func initMetricSets(r ModulesRegistry, m Module) ([]MetricSet, error) {
var (
errs multierror.Errors
metricsets []MetricSet
Expand Down Expand Up @@ -157,7 +157,7 @@ func initMetricSets(r *Register, m Module) ([]MetricSet, error) {
// newBaseMetricSets creates a new BaseMetricSet for all MetricSets defined
// in the module's config. An error is returned if no MetricSets are specified
// in the module's config and no default MetricSet is defined.
func newBaseMetricSets(r *Register, m Module) ([]BaseMetricSet, error) {
func newBaseMetricSets(r ModulesRegistry, m Module) ([]BaseMetricSet, error) {
hosts := []string{""}
if l := m.Config().Hosts; len(l) > 0 {
hosts = l
Expand Down
125 changes: 125 additions & 0 deletions metricbeat/mb/lightmodules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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

import (
"fmt"
"sort"

"github.com/pkg/errors"
)

// LigthModulesRegistry wraps
type LightModulesRegistry struct {
ModulesRegistry

paths []string
}

// NewLightModulesRegistry creates a new LightModulesRegistry
func NewLightModulesRegistry(paths ...string) *LightModulesRegistry {
return &LightModulesRegistry{
ModulesRegistry: NewRegister(),
paths: paths,
}
}

func (r *LightModulesRegistry) DefaultMetricSets(module string) ([]string, error) {
if stringInSlice(module, r.parent().Modules()) {
return r.parent().DefaultMetricSets(module)
}
module, err := r.readModule(module)
if err != nil {
return nil, errors.Wrapf(err, "failed to get default metricsets for module '%s'", module)
}
var metricsets []string
for i, ms := range module.Metricsets {
if ms.Default {
metricsets = append(metricsets, ms)
}
}
if len(metricsets) == 0 {
return nil, fmt.Errorf("no default metricset exists for module '%s'", module)
}
return metricsets, nil
}

func (r *LightModulesRegistry) Modules() []string {
registeredModules := r.parent().Modules()
modules := r.listModules()
return append(registeredModules, modules...)
}

func (r *LightModulesRegistry) MetricSets(module string) []string {
if stringInSlice(module, r.parent().Modules()) {
return r.parent().MetricSets(module)
}
module, err := r.readModule(module)
if err != nil {
return nil, errors.Wrapf(err, "failed to get metricsets for module '%s'", module)
}
metricsets := make([]string, len(module.MetricSets))
for i := range module.Metricsets {
metricset[i] = module.MetricSets[i].Name
}
return metricsets
}

func (r *LightModulesRegistry) String() string {
var metricsets []string
modules := r.listModules()
for _, moduleName := range modules {
module, err := r.readModule(module)
if err != nil {
continue
}
for _, metricset := range module.MetricSets {
metricsets = append(metricsets, fmt.Sprintf("%s/%s", moduleName, metricset.Name))
}
}
sort.Strings(metricsets)
parent := r.parent().String()
if len(modules) == 0 {
return parent
}
return fmt.Sprintf("%s, Light Modules [MetricSets:[%s]]",
parent,
strings.Join(metricsets, ", "),
)
}

func (r *LightModulesRegistry) moduleFactory(name string) ModuleFactory {
return r.parent().moduleFactory(name)
}

func (r *LightModulesRegistry) metricSetRegistration(module, name string) (MetricSetRegistration, error) {
return r.parent().metricSetRegistration(module, name)
}

func (r *LightModulesRegistry) parent() ModulesRegistry {
return r.ModulesRegistry
}

func stringInSlice(s string, slice []string) bool {
for i := range slice {
if s == slice[i] {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion metricbeat/mb/module/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type stats struct {

// NewWrapper create a new Module and its associated MetricSets based
// on the given configuration.
func NewWrapper(config *common.Config, r *mb.Register, options ...Option) (*Wrapper, error) {
func NewWrapper(config *common.Config, r mb.ModulesRegistry, options ...Option) (*Wrapper, error) {
module, metricsets, err := mb.NewModule(config, r)
if err != nil {
return nil, err
Expand Down
16 changes: 15 additions & 1 deletion metricbeat/mb/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,23 @@ import (

const initialSize = 20 // initialSize specifies the initial size of the Register.

// ModulesRegistry is the interface implemented by module registries
type ModulesRegistry interface {
AddModule(name string, factory ModuleFactory) error
AddMetricSet(module string, name string, factory MetricSetFactory, hostParser ...HostParser) error
MustAddMetricSet(module, name string, factory MetricSetFactory, options ...MetricSetOption)
DefaultMetricSets(module string) ([]string, error)
Modules() []string
MetricSets(module string) []string
String() string

moduleFactory(name string) ModuleFactory
metricSetRegistration(module, name string) (MetricSetRegistration, error)
}

// Registry is the singleton Register instance where all ModuleFactory's and
// MetricSetFactory's should be registered.
var Registry = NewRegister()
var Registry ModulesRegistry = NewLightModulesRegistry()

// DefaultModuleFactory returns the given BaseModule and never returns an error.
// If a MetricSets are registered without an associated ModuleFactory, then
Expand Down