mb

package
v5.0.0-alpha5+incompat... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2016 License: Apache-2.0 Imports: 9 Imported by: 20,866

Documentation

Overview

Package mb (short for Metricbeat) contains the public interfaces that are used to implement Modules and their associated MetricSets.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyConfig indicates that modules configuration list is nil or empty.
	ErrEmptyConfig = errors.New("one or more modules must be configured")

	// ErrAllModulesDisabled indicates that all modules are disabled. At least
	// one module must be enabled.
	ErrAllModulesDisabled = errors.New("all modules are disabled")
)
View Source
var DefaultModuleFactory = func(base BaseModule) (Module, error) {
	return &base, nil
}

DefaultModuleFactory returns the given BaseModule and never returns an error. If a MetricSets are registered without an associated ModuleFactory, then the DefaultModuleFactory will be used to instantiate a Module.

View Source
var Registry = NewRegister()

Registry is the singleton Register instance where all ModuleFactory's and MetricSetFactory's should be registered.

Functions

func NewModules

func NewModules(config []*common.Config, r *Register) (map[Module][]MetricSet, error)

NewModules builds new Modules and their associated MetricSets based on the provided configuration data. config is a list module config data (the data 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 mapping of Modules to MetricSets or an error.

Types

type BaseMetricSet

type BaseMetricSet struct {
	// contains filtered or unexported fields
}

BaseMetricSet implements the MetricSet interface.

The BaseMetricSet type can be embedded into another struct to satisfy the MetricSet interface requirements, leaving only the Fetch() method to be implemented to have a complete MetricSet implementation.

func (*BaseMetricSet) Host

func (b *BaseMetricSet) Host() string

Host returns the hostname or other module specific value that identifies a specific host or service instance from which to collect metrics.

func (*BaseMetricSet) Module

func (b *BaseMetricSet) Module() Module

Module returns the parent Module for the MetricSet.

func (*BaseMetricSet) Name

func (b *BaseMetricSet) Name() string

Name returns the name of the MetricSet. It should not include the name of the module.

type BaseModule

type BaseModule struct {
	// contains filtered or unexported fields
}

BaseModule implements the Module interface.

When a Module needs to store additional data or provide methods to its MetricSets, it can embed this type into another struct to satisfy the Module interface requirements.

func (*BaseModule) Config

func (m *BaseModule) Config() ModuleConfig

Config returns the ModuleConfig used to create the Module.

func (*BaseModule) Name

func (m *BaseModule) Name() string

Name returns the name of the Module.

func (*BaseModule) UnpackConfig

func (m *BaseModule) UnpackConfig(to interface{}) error

UnpackConfig unpacks the raw module config to the given object.

type EventFetcher

type EventFetcher interface {
	MetricSet
	Fetch() (common.MapStr, error)
}

EventFetcher is a MetricSet that returns a single event when collecting data.

type EventsFetcher

type EventsFetcher interface {
	MetricSet
	Fetch() ([]common.MapStr, error)
}

EventsFetcher is a MetricSet that returns a multiple events when collecting data.

type MetricSet

type MetricSet interface {
	Name() string   // Name returns the name of the MetricSet.
	Module() Module // Module returns the parent Module for the MetricSet.
	Host() string   // Host returns a hostname or other module specific value

}

MetricSet is the common interface for all MetricSet implementations. In addition to this interface, all MetricSets must implement either EventFetcher or EventsFetcher (but not both).

type MetricSetFactory

type MetricSetFactory func(base BaseMetricSet) (MetricSet, error)

MetricSetFactory accepts a BaseMetricSet and returns a MetricSet. If there was an error creating the MetricSet then an error will be returned. The returned MetricSet must also implement either EventFetcher or EventsFetcher (but not both).

Example

ExampleMetricSetFactory demonstrates how to register a MetricSetFactory and unpack additional configuration data.

package main

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

func init() {
	// Register the MetricSetFactory function for the "status" MetricSet.
	if err := mb.Registry.AddMetricSet("someapp", "status", NewMetricSet); err != nil {
		panic(err)
	}
}

type MetricSet struct {
	mb.BaseMetricSet
	username string
	password string
}

func NewMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
	// Unpack additional configuration options.
	config := struct {
		Username string `config:"username"`
		Password string `config:"password"`
	}{
		Username: "",
		Password: "",
	}
	if err := base.Module().UnpackConfig(&config); err != nil {
		return nil, err
	}

	return &MetricSet{
		BaseMetricSet: base,
		username:      config.Username,
		password:      config.Password,
	}, nil
}

func (ms *MetricSet) Fetch() (common.MapStr, error) {
	// Fetch data from host and return the data.
	return common.MapStr{
		"someParam":  "value",
		"otherParam": 42,
	}, nil
}

// ExampleMetricSetFactory demonstrates how to register a MetricSetFactory
// and unpack additional configuration data.
func main() {}
Output:

type Module

type Module interface {
	Name() string                      // Name returns the name of the Module.
	Config() ModuleConfig              // Config returns the ModuleConfig used to create the Module.
	UnpackConfig(to interface{}) error // UnpackConfig unpacks the raw module config to the given object.
}

Module is the common interface for all Module implementations.

type ModuleConfig

type ModuleConfig struct {
	Hosts      []string                `config:"hosts"`
	Period     time.Duration           `config:"period"     validate:"positive"`
	Timeout    time.Duration           `config:"timeout"    validate:"positive"`
	Module     string                  `config:"module"     validate:"required"`
	MetricSets []string                `config:"metricsets" validate:"required"`
	Enabled    bool                    `config:"enabled"`
	Filters    processors.PluginConfig `config:"filters"`

	common.EventMetadata `config:",inline"` // Fields and tags to add to events.
}

ModuleConfig is the base configuration data for all Modules.

func DefaultModuleConfig

func DefaultModuleConfig() ModuleConfig

DefaultModuleConfig returns a ModuleConfig with the default values populated.

type ModuleFactory

type ModuleFactory func(base BaseModule) (Module, error)

ModuleFactory accepts a BaseModule and returns a Module. If there was an error creating the Module then an error will be returned.

Example

ExampleModuleFactory demonstrates how to register a custom ModuleFactory and unpack additional configuration data.

package main

import (
	"github.com/elastic/beats/metricbeat/mb"
)

func init() {
	// Register the ModuleFactory function for the "example" module.
	if err := mb.Registry.AddModule("example", NewModule); err != nil {
		panic(err)
	}
}

type Module struct {
	mb.BaseModule
	Protocol string
}

func NewModule(base mb.BaseModule) (mb.Module, error) {
	// Unpack additional configuration options.
	config := struct {
		Protocol string `config:"protocol"`
	}{
		Protocol: "udp",
	}
	if err := base.UnpackConfig(&config); err != nil {
		return nil, err
	}

	return &Module{BaseModule: base, Protocol: config.Protocol}, nil
}

// ExampleModuleFactory demonstrates how to register a custom ModuleFactory
// and unpack additional configuration data.
func main() {}
Output:

type Register

type Register struct {
	// contains filtered or unexported fields
}

Register contains the factory functions for creating new Modules and new MetricSets.

func NewRegister

func NewRegister() *Register

NewRegister creates and returns a new Register.

func (*Register) AddMetricSet

func (r *Register) AddMetricSet(module string, name string, factory MetricSetFactory) error

AddMetricSet registers a new MetricSetFactory. An error is returned if any parameter is empty or nil or if a factory has already been registered under the name.

func (*Register) AddModule

func (r *Register) AddModule(name string, factory ModuleFactory) error

AddModule registers a new ModuleFactory. An error is returned if the name is empty, factory is nil, or if a factory has already been registered under the name.

func (Register) String

func (r Register) String() string

String return a string representation of the registered ModuleFactory's and MetricSetFactory's.

Directories

Path Synopsis
Package testing provides utility functions for testing Module and MetricSet implementations.
Package testing provides utility functions for testing Module and MetricSet implementations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL