add plugin documentation

This commit is contained in:
Steven Soroka 2020-10-14 11:12:41 -04:00
parent 423259905e
commit ac8f4c1e15
8 changed files with 71 additions and 24 deletions

View File

@ -39,6 +39,7 @@ import (
type Simple struct { type Simple struct {
Ok bool `toml:"ok"` Ok bool `toml:"ok"`
Log telegraf.Logger `toml:"-"`
} }
func (s *Simple) Description() string { func (s *Simple) Description() string {
@ -52,6 +53,7 @@ func (s *Simple) SampleConfig() string {
` `
} }
// Init is for setup, and validating config.
func (s *Simple) Init() error { func (s *Simple) Init() error {
return nil return nil
} }

View File

@ -31,6 +31,7 @@ import (
type Simple struct { type Simple struct {
Ok bool `toml:"ok"` Ok bool `toml:"ok"`
Log telegraf.Logger `toml:"-"`
} }
func (s *Simple) Description() string { func (s *Simple) Description() string {
@ -43,20 +44,25 @@ func (s *Simple) SampleConfig() string {
` `
} }
// Init is for setup, and validating config.
func (s *Simple) Init() error { func (s *Simple) Init() error {
return nil return nil
} }
func (s *Simple) Connect() error { func (s *Simple) Connect() error {
// Make a connection to the URL here // Make any connection required here
return nil return nil
} }
func (s *Simple) Close() error { func (s *Simple) Close() error {
// Close connection to the URL here // Close any connections here.
// Write will not be called once Close is called, so there is no need to synchronize.
return nil return nil
} }
// Write should write immediately to the output, and not buffer writes
// (Telegraf manages the buffer for you). Returning an error will fail this
// batch of writes and the entire batch will be retried automatically.
func (s *Simple) Write(metrics []telegraf.Metric) error { func (s *Simple) Write(metrics []telegraf.Metric) error {
for _, metric := range metrics { for _, metric := range metrics {
// write `metric` to the output sink here // write `metric` to the output sink here

View File

@ -33,6 +33,7 @@ import (
) )
type Printer struct { type Printer struct {
Log telegraf.Logger `toml:"-"`
} }
var sampleConfig = ` var sampleConfig = `
@ -46,6 +47,7 @@ func (p *Printer) Description() string {
return "Print all metrics that pass through this filter." return "Print all metrics that pass through this filter."
} }
// Init is for setup, and validating config.
func (p *Printer) Init() error { func (p *Printer) Init() error {
return nil return nil
} }
@ -97,6 +99,7 @@ import (
) )
type Printer struct { type Printer struct {
Log telegraf.Logger `toml:"-"`
} }
var sampleConfig = ` var sampleConfig = `
@ -110,13 +113,27 @@ func (p *Printer) Description() string {
return "Print all metrics that pass through this filter." return "Print all metrics that pass through this filter."
} }
// Init is for setup, and validating config.
func (p *Printer) Init() error { func (p *Printer) Init() error {
return nil return nil
} }
// Start is called once when the plugin starts; it is only called once per
// plugin instance, and never in parallel.
// Start should return once it is ready to receive metrics.
// The passed in accumulator is the same as the one passed to Add(), so you
// can choose to save it in the plugin, or use the one received from Add().
func (p *Printer) Start(acc telegraf.Accumulator) error { func (p *Printer) Start(acc telegraf.Accumulator) error {
} }
// Add is called for each metric to be processed. The Add() function does not
// need to wait for the metric to be processed before returning, and it may
// be acceptable to let background goroutine(s) handle the processing if you
// have slow processing you need to do in parallel.
// Keep in mind Add() should not spawn unbounded goroutines, so you may need
// to use a semaphore or pool of workers (eg: reverse_dns plugin does this).
// Metrics you don't want to pass downstream should have metric.Drop() called,
// rather than simply omitting the acc.AddMetric() call
func (p *Printer) Add(metric telegraf.Metric, acc telegraf.Accumulator) error { func (p *Printer) Add(metric telegraf.Metric, acc telegraf.Accumulator) error {
// print! // print!
fmt.Println(metric.String()) fmt.Println(metric.String())
@ -127,6 +144,12 @@ func (p *Printer) Add(metric telegraf.Metric, acc telegraf.Accumulator) error {
return nil return nil
} }
// Stop gives you an opportunity to gracefully shut down the processor.
// Once Stop() is called, Add() will not be called any more. If you are using
// goroutines, you should wait for any in-progress metrics to be processed
// before returning from Stop().
// When stop returns, you should no longer be writing metrics to the
// accumulator.
func (p *Printer) Stop() error { func (p *Printer) Stop() error {
} }

View File

@ -4,7 +4,7 @@ type Input interface {
PluginDescriber PluginDescriber
// Gather takes in an accumulator and adds the metrics that the Input // Gather takes in an accumulator and adds the metrics that the Input
// gathers. This is called every "interval" // gathers. This is called every agent.interval
Gather(Accumulator) error Gather(Accumulator) error
} }
@ -15,6 +15,9 @@ type ServiceInput interface {
// Stop returns. // Stop returns.
Start(Accumulator) error Start(Accumulator) error
// Stop stops the services and closes any necessary channels and connections // Stop stops the services and closes any necessary channels and connections.
// Metrics should not be written out to the accumulator once stop returns, so
// Stop() should stop reading and wait for any in-flight metrics to write out
// to the accumulator before returning.
Stop() Stop()
} }

View File

@ -3,9 +3,12 @@ package telegraf
type Output interface { type Output interface {
PluginDescriber PluginDescriber
// Connect to the Output // Connect to the Output; connect is only called once when the plugin starts
Connect() error Connect() error
// Close any connections to the Output // Close any connections to the Output. Close is called once when the output
// is shutting down. Close will not be called until all writes have finished,
// and Write() will not be called once Close() has been, so locking is not
// necessary.
Close() error Close() error
// Write takes in group of points to be written to the Output // Write takes in group of points to be written to the Output
Write(metrics []Metric) error Write(metrics []Metric) error

View File

@ -10,7 +10,9 @@ type Initializer interface {
} }
// PluginDescriber contains the functions all plugins must implement to describe // PluginDescriber contains the functions all plugins must implement to describe
// themselves to Telegraf // themselves to Telegraf. Note that all plugins may define a logger that is
// not part of the interface, but will receive an injected logger if it's set.
// eg: Log telegraf.Logger `toml:"-"`
type PluginDescriber interface { type PluginDescriber interface {
// SampleConfig returns the default configuration of the Processor // SampleConfig returns the default configuration of the Processor
SampleConfig() string SampleConfig() string

View File

@ -7,9 +7,7 @@ This input plugin checks HTTP/HTTPS connections.
```toml ```toml
# HTTP/HTTPS request given an address a method and a timeout # HTTP/HTTPS request given an address a method and a timeout
[[inputs.http_response]] [[inputs.http_response]]
## Deprecated in 1.12, use 'urls' ## address is Deprecated in 1.12, use 'urls'
## Server address (default http://localhost)
# address = "http://localhost"
## List of urls to query. ## List of urls to query.
# urls = ["http://localhost"] # urls = ["http://localhost"]

View File

@ -14,17 +14,27 @@ type Processor interface {
type StreamingProcessor interface { type StreamingProcessor interface {
PluginDescriber PluginDescriber
// Start is the initializer for the processor // Start is called once when the plugin starts; it is only called once per
// Start is only called once per plugin instance, and never in parallel. // plugin instance, and never in parallel.
// Start should exit immediately after setup // Start should return once it is ready to receive metrics.
// The passed in accumulator is the same as the one passed to Add(), so you
// can choose to save it in the plugin, or use the one received from Add().
Start(acc Accumulator) error Start(acc Accumulator) error
// Add is called for each metric to be processed. // Add is called for each metric to be processed. The Add() function does not
// need to wait for the metric to be processed before returning, and it may
// be acceptable to let background goroutine(s) handle the processing if you
// have slow processing you need to do in parallel.
// Keep in mind Add() should not spawn unbounded goroutines, so you may need
// to use a semaphore or pool of workers (eg: reverse_dns plugin does this)
// Metrics you don't want to pass downstream should have metric.Drop() called,
// rather than simply omitting the acc.AddMetric() call
Add(metric Metric, acc Accumulator) error Add(metric Metric, acc Accumulator) error
// Stop gives you a callback to free resources. // Stop gives you an opportunity to gracefully shut down the processor.
// by the time Stop is called, the input stream will have already been closed // Once Stop() is called, Add() will not be called any more. If you are using
// and Add will not be called anymore. // goroutines, you should wait for any in-progress metrics to be processed
// before returning from Stop().
// When stop returns, you should no longer be writing metrics to the // When stop returns, you should no longer be writing metrics to the
// accumulator. // accumulator.
Stop() error Stop() error