module

package
v6.2.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2018 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package module contains the low-level utilities for running Metricbeat modules and metricsets. This is useful for building your own tool that has a module and sub-module concept. If you want to reuse the whole Metricbeat framework see the github.com/elastic/beats/metricbeat/beater package that provides a higher level interface.

This contains the tools for instantiating modules, running them, and connecting their outputs to the Beat's output pipeline.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PublishChannels

func PublishChannels(client beat.Client, cs ...<-chan beat.Event)

PublishChannels publishes the events read from each channel to the given publisher client. If the publisher client blocks for any reason then events will not be read from the given channels.

This method blocks until all of the channels have been closed and are fully read. To stop the method immediately, close the channels and close the publisher client to ensure that publishing does not block. This may result is some events being discarded.

Types

type Connector

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

Connector configures and establishes a beat.Client for publishing events to the publisher pipeline.

func NewConnector

func NewConnector(pipeline beat.Pipeline, c *common.Config, dynFields *common.MapStrPointer) (*Connector, error)

func (*Connector) Connect

func (c *Connector) Connect() (beat.Client, error)

type Factory

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

Factory creates new Runner instances from configuration objects. It is used to register and reload modules.

func NewFactory

func NewFactory(p beat.Pipeline, options ...Option) *Factory

NewFactory creates new Reloader instance for the given config

func (*Factory) Create

func (r *Factory) Create(c *common.Config, meta *common.MapStrPointer) (cfgfile.Runner, error)

type Option

type Option func(wrapper *Wrapper)

Option specifies some optional arguments used for configuring the behavior of a module Wrapper.

func WithEventModifier

func WithEventModifier(modifier mb.EventModifier) Option

WithEventModifier attaches an EventModifier that will be executed for each event generated by the MetricSets of the module. Multiple EventModifiers can be added and they will be executed in the order in which they were added.

func WithMaxStartDelay

func WithMaxStartDelay(delay time.Duration) Option

WithMaxStartDelay specifies the upper bound for the random startup delay for each MetricSet in the module. By default there is no delay.

func WithMetricSetInfo

func WithMetricSetInfo() Option

WithMetricSetInfo attaches an EventModifier that adds information about the MetricSet that generated the event. It will always add the metricset and module names. And it will add the host and rtt (round-trip time in microseconds) values if they are non-zero values.

"metricset": {
  "host": "apache",
  "module": "apache",
  "name": "status",
  "rtt": 115
}

type Runner

type Runner interface {
	// Start starts the Module. If Start is called more than once, only the
	// first will start the Module.
	Start()

	// Stop stops the Module and waits for module's MetricSets to exit. The
	// publisher.Client will be closed by Stop. If Stop is called more than
	// once, only the first stop the Module and wait for it to exit.
	Stop()
}

Runner is a facade for a Wrapper that provides a simple interface for starting and stopping a Module.

Example

ExampleRunner demonstrates how to use Runner to start and stop a module.

// A *beat.Beat is injected into a Beater when it runs and contains the
// Publisher used to publish events. This Beat pointer is created here only
// for demonstration purposes.
var b *beat.Beat

config, err := common.NewConfigFrom(map[string]interface{}{
	"module":     moduleName,
	"metricsets": []string{eventFetcherName},
})
if err != nil {
	return
}

// Create a new Wrapper based on the configuration.
m, err := module.NewWrapper(config, mb.Registry, module.WithMetricSetInfo())
if err != nil {
	return
}

connector, err := module.NewConnector(b.Publisher, config, nil)
if err != nil {
	return
}

client, err := connector.Connect()
if err != nil {
	return
}

// Create the Runner facade.
runner := module.NewRunner(client, m)

// Start the module and have it publish to a new publisher.Client.
runner.Start()

// Stop the module. This blocks until all MetricSets in the Module have
// stopped and the publisher.Client is closed.
runner.Stop()
Output:

func NewRunner

func NewRunner(client beat.Client, mod *Wrapper) Runner

NewRunner returns a Runner facade. The events generated by the Module will be published to a new publisher.Client generated from the pubClientFactory.

type Wrapper

type Wrapper struct {
	mb.Module
	// contains filtered or unexported fields
}

Wrapper contains the Module and the private data associated with running the Module and its MetricSets.

Use NewWrapper or NewWrappers to construct new Wrappers.

Example

ExampleWrapper demonstrates how to create a single Wrapper from configuration, start the module, and consume events generated by the module.

// Build a configuration object.
config, err := common.NewConfigFrom(map[string]interface{}{
	"module":     moduleName,
	"metricsets": []string{eventFetcherName},
})
if err != nil {
	fmt.Println("Error:", err)
	return
}

// Create a new Wrapper based on the configuration.
m, err := module.NewWrapper(config, mb.Registry, module.WithMetricSetInfo())
if err != nil {
	fmt.Println("Error:", err)
	return
}

// Run the module until done is closed.
done := make(chan struct{})
output := m.Start(done)

// Process events from the output channel until it is closed.
var wg sync.WaitGroup
wg.Add(1)
go func() {
	defer wg.Done()
	for event := range output {
		event.Fields.Put("metricset.rtt", 111)

		output, err := encodeEvent(event)
		if err == nil {
			fmt.Println(output)
		}
	}
}()

// Simulate running for a while.
time.Sleep(50 * time.Millisecond)

// When finished with the module, close the done channel. When the Module
// stops it will automatically close its output channel so that the output
// for loop stops.
close(done)
wg.Wait()
Output:

{
  "@metadata": {
    "beat": "noindex",
    "type": "doc",
    "version": "1.2.3"
  },
  "@timestamp": "2016-05-10T23:27:58.485Z",
  "fake": {
    "eventfetcher": {
      "metric": 1
    }
  },
  "metricset": {
    "module": "fake",
    "name": "eventfetcher",
    "rtt": 111
  }
}

func NewWrapper

func NewWrapper(config *common.Config, r *mb.Register, options ...Option) (*Wrapper, error)

NewWrapper create a new Module and its associated MetricSets based on the given configuration.

func (*Wrapper) MetricSets

func (mw *Wrapper) MetricSets() []*metricSetWrapper

MetricSets return the list of metricsets of the module

func (*Wrapper) Start

func (mw *Wrapper) Start(done <-chan struct{}) <-chan beat.Event

Start starts the Module's MetricSet workers which are responsible for fetching metrics. The workers will continue to periodically fetch until the done channel is closed. When the done channel is closed all MetricSet workers will stop and the returned output channel will be closed.

The returned channel is buffered with a length one one. It must drained to prevent blocking the operation of the MetricSets.

Start should be called only once in the life of a Wrapper.

func (*Wrapper) String

func (mw *Wrapper) String() string

String returns a string representation of Wrapper.

Jump to

Keyboard shortcuts

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