Documentation ¶
Overview ¶
Package mb (short for Metricbeat) contains the public interfaces that are used to implement Modules and their associated MetricSets.
Index ¶
- Constants
- Variables
- func NewModules(config []*common.Config, r *Register) (map[Module][]MetricSet, error)
- type BaseMetricSet
- type BaseModule
- type Closer
- type EventFetcher
- type EventsFetcher
- type HostData
- type HostParser
- type MetricSet
- type MetricSetFactory
- type Module
- type ModuleConfig
- type ModuleFactory
- type PushMetricSet
- type PushReporter
- type Register
- func (r *Register) AddMetricSet(module string, name string, factory MetricSetFactory, hostParser ...HostParser) error
- func (r *Register) AddModule(name string, factory ModuleFactory) error
- func (r *Register) MetricSets(module string) []string
- func (r *Register) Modules() []string
- func (r *Register) String() string
- type Reporter
- type ReportingMetricSet
Examples ¶
Constants ¶
const ( // ModuleData is the key used in events created by MetricSets to add data // to an event that is common to the module. The data must be a // common.MapStr and when the final event is built the object will be stored // in the event under a key that is the module name. ModuleData string = "_module" )
Variables ¶
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") )
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.
var Registry = NewRegister()
Registry is the singleton Register instance where all ModuleFactory's and MetricSetFactory's should be registered.
Functions ¶
func NewModules ¶
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) GoString ¶
func (b *BaseMetricSet) GoString() string
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) HostData ¶
func (b *BaseMetricSet) HostData() HostData
HostData returns the parsed host data.
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.
func (*BaseMetricSet) String ¶
func (b *BaseMetricSet) String() string
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) GoString ¶
func (m *BaseModule) GoString() string
func (*BaseModule) String ¶
func (m *BaseModule) String() string
func (*BaseModule) UnpackConfig ¶
func (m *BaseModule) UnpackConfig(to interface{}) error
UnpackConfig unpacks the raw module config to the given object.
type Closer ¶
type Closer interface {
Close() error
}
Closer is an optional interface that a MetricSet can implement in order to cleanup any resources it has open at shutdown.
type EventFetcher ¶
EventFetcher is a MetricSet that returns a single event when collecting data. Use ReportingMetricSet for new MetricSet implementations.
type EventsFetcher ¶
EventsFetcher is a MetricSet that returns a multiple events when collecting data. Use ReportingMetricSet for new MetricSet implementations.
type HostData ¶
type HostData struct { URI string // The full URI that should be used in connections. SanitizedURI string // A sanitized version of the URI without credentials. Host string // The host and possibly port. User string // Username Password string // Password }
HostData contains values parsed from the 'host' configuration. Other configuration data like protocols, usernames, and passwords may also be used to construct this HostData data.
type HostParser ¶
HostParser is a function that parses a host value from the configuration and returns a HostData object. The module is provided in case additional configuration values are required to parse and build the HostData object. An error should be returned if the host or configuration is invalid.
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 // that identifies a specific host or service instance from which to collect // metrics. HostData() HostData // HostData returns the parsed host data. }
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).
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"` Raw bool `config:"raw"` common.EventMetadata `config:",inline"` // Fields and tags to add to events. }
ModuleConfig is the base configuration data for all Modules.
The Raw config option is used to enable raw fields in a metricset. This means the metricset fetches not only the predefined fields but add alls raw data under the raw namespace to the event.
func DefaultModuleConfig ¶
func DefaultModuleConfig() ModuleConfig
DefaultModuleConfig returns a ModuleConfig with the default values populated.
func (ModuleConfig) GoString ¶
func (c ModuleConfig) GoString() string
func (ModuleConfig) String ¶
func (c ModuleConfig) String() string
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 PushMetricSet ¶
type PushMetricSet interface { MetricSet Run(r PushReporter) }
PushMetricSet is a MetricSet that pushes events (rather than pulling them periodically via a Fetch callback). Run is invoked to start the event subscription and it should block until the MetricSet is ready to stop or the PushReporter's done channel is closed.
type PushReporter ¶
type PushReporter interface { Reporter // Done returns a channel that's closed when work done on behalf of this // reporter should be canceled. Done() <-chan struct{} }
PushReporter is used by a MetricSet to report events, errors, or errors with metadata. It provides a done channel used to signal that reporter should stop.
type Register ¶
type Register struct {
// contains filtered or unexported fields
}
Register contains the factory functions for creating new Modules and new MetricSets. Registers are thread safe for concurrent usage.
func (*Register) AddMetricSet ¶
func (r *Register) AddMetricSet(module string, name string, factory MetricSetFactory, hostParser ...HostParser) error
AddMetricSet registers a new MetricSetFactory. Optionally it accepts a single HostParser function for parsing the 'host' configuration data. 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) MetricSets ¶
MetricSets returns the list of metricsets registered for a given module
type Reporter ¶
type Reporter interface { Event(event common.MapStr) bool // Event reports a single successful event. ErrorWith(err error, meta common.MapStr) bool // ErrorWith reports a single error event with the additional metadata. Error(err error) bool // Error reports a single error event. }
Reporter is used by a MetricSet to report events, errors, or errors with metadata. The methods return false if and only if publishing failed because the MetricSet is being closed.
type ReportingMetricSet ¶
ReportingMetricSet is a MetricSet that reports events or errors through the Reporter interface. Fetch is called periodically to collect events.
Example ¶
ExampleReportingMetricSet demonstrates how to register a MetricSetFactory and implement a ReportingMetricSet.
package main import ( "fmt" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/metricbeat/mb" "github.com/elastic/beats/metricbeat/mb/parse" ) var hostParser = parse.URLHostParserBuilder{ DefaultScheme: "http", }.Build() func init() { // Register the MetricSetFactory function for the "status" MetricSet. if err := mb.Registry.AddMetricSet("someapp", "status", NewMetricSet, hostParser); err != nil { panic(err) } } type MetricSet struct { mb.BaseMetricSet } func NewMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) { fmt.Println("someapp-status url=", base.HostData().SanitizedURI) return &MetricSet{BaseMetricSet: base}, nil } // Fetch will be called periodically by the framework. func (ms *MetricSet) Fetch(report mb.Reporter) { // Fetch data from the host at ms.HostData().URI and return the data. data, err := common.MapStr{ "some_metric": 18.0, "answer_to_everything": 42, }, error(nil) if err != nil { // Report an error if it occurs. report.Error(err) return } // Otherwise report the collected data. report.Event(data) } // ExampleReportingMetricSet demonstrates how to register a MetricSetFactory // and implement a ReportingMetricSet. func main() {}
Output: