2021-11-25 02:45:19 +08:00
# Input Plugins
2018-11-06 05:34:28 +08:00
This section is for developers who want to create new collection inputs.
Telegraf is entirely plugin driven. This interface allows for operators to
pick and chose what is gathered and makes it easy for developers
to create new ways of generating metrics.
Plugin authorship is kept as simple as possible to promote people to develop
and submit new inputs.
2021-11-25 02:45:19 +08:00
## Input Plugin Guidelines
2018-11-06 05:34:28 +08:00
- A plugin must conform to the [telegraf.Input][] interface.
- Input Plugins should call `inputs.Add` in their `init` function to register
themselves. See below for a quick example.
2022-08-16 04:44:59 +08:00
- To be available within Telegraf itself, plugins must register themselves
using a file in `github.com/influxdata/telegraf/plugins/inputs/all` named
according to the plugin name. Make sure your also add build-tags to
conditionally build the plugin.
2022-05-26 00:25:51 +08:00
- Each plugin requires a file called `sample.conf` containing the sample
configuration for the plugin in TOML format.
2022-04-06 06:11:09 +08:00
Please consult the [Sample Config][] page for the latest style guidelines.
2022-05-26 00:25:51 +08:00
- Each plugin `README.md` file should include the `sample.conf` file in a section
describing the configuration by specifying a `toml` section in the form `toml @sample.conf` . The specified file(s) are then injected automatically into the Readme.
2021-05-08 03:26:11 +08:00
- Follow the recommended [Code Style][].
2018-11-06 05:34:28 +08:00
Let's say you've written a plugin that emits metrics about processes on the
current host.
2021-11-25 02:45:19 +08:00
## Input Plugin Example
2018-11-06 05:34:28 +08:00
2022-08-16 04:44:59 +08:00
Content of your plugin file e.g. `simple.go`
2018-11-06 05:34:28 +08:00
```go
2022-05-26 00:25:51 +08:00
//go:generate ../../../tools/readme_config_includer/generator
2018-11-06 05:34:28 +08:00
package simple
import (
2022-05-26 00:25:51 +08:00
_ "embed"
2018-11-06 05:34:28 +08:00
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
2022-05-26 00:25:51 +08:00
//go:embed sample.conf
var sampleConfig string
2018-11-06 05:34:28 +08:00
type Simple struct {
2020-10-14 23:12:41 +08:00
Ok bool `toml:"ok"`
Log telegraf.Logger `toml:"-"`
2018-11-06 05:34:28 +08:00
}
2022-05-26 00:25:51 +08:00
func (*Simple) SampleConfig() string {
return sampleConfig
}
2020-10-14 23:12:41 +08:00
// Init is for setup, and validating config.
2019-06-15 06:12:27 +08:00
func (s *Simple) Init() error {
2021-11-25 02:45:19 +08:00
return nil
2019-06-15 06:12:27 +08:00
}
2018-11-06 05:34:28 +08:00
func (s *Simple) Gather(acc telegraf.Accumulator) error {
if s.Ok {
acc.AddFields("state", map[string]interface{}{"value": "pretty good"}, nil)
} else {
acc.AddFields("state", map[string]interface{}{"value": "not great"}, nil)
}
return nil
}
func init() {
inputs.Add("simple", func() telegraf.Input { return & Simple{} })
}
```
2022-08-16 04:44:59 +08:00
Registration of the plugin on `plugins/inputs/all/simple.go` :
```go
//go:build !custom || inputs || inputs.simple
package all
import _ "github.com/influxdata/telegraf/plugins/inputs/simple" // register plugin
```
The _build-tags_ in the first line allow to selectively include/exclude your
plugin when customizing Telegraf.
2018-11-06 05:34:28 +08:00
### Development
2021-11-25 02:45:19 +08:00
- Run `make static` followed by `make plugin-[pluginName]` to spin up a docker
2018-11-06 05:34:28 +08:00
dev environment using docker-compose.
2022-08-16 04:44:59 +08:00
- __[Optional]__ When developing a plugin, add a `dev` directory with a
2018-11-06 05:34:28 +08:00
`docker-compose.yml` and `telegraf.conf` as well as any other supporting
files, where sensible.
### Typed Metrics
2020-12-17 02:23:50 +08:00
In addition to the `AddFields` function, the accumulator also supports
2018-11-06 05:34:28 +08:00
functions to add typed metrics: `AddGauge` , `AddCounter` , etc. Metric types
are ignored by the InfluxDB output, but can be used for other outputs, such as
[prometheus][prom metric types].
### Data Formats
Some input plugins, such as the [exec][] plugin, can accept any supported
[input data formats][].
In order to enable this, you must specify a `SetParser(parser parsers.Parser)`
function on the plugin object (see the exec plugin for an example), as well as
defining `parser` as a field of the object.
You can then utilize the parser internally in your plugin, parsing data as you
see fit. Telegraf's configuration layer will take care of instantiating and
creating the `Parser` object.
2022-04-06 06:11:09 +08:00
Add the following to the sample configuration in the README.md:
2018-11-06 05:34:28 +08:00
```toml
## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "influx"
```
### Service Input Plugins
This section is for developers who want to create new "service" collection
inputs. A service plugin differs from a regular plugin in that it operates a
background service while Telegraf is running. One example would be the
`statsd` plugin, which operates a statsd server.
Service Input Plugins are substantially more complicated than a regular
plugin, as they will require threads and locks to verify data integrity.
Service Input Plugins should be avoided unless there is no way to create their
behavior with a regular plugin.
To create a Service Input implement the [telegraf.ServiceInput][] interface.
### Metric Tracking
Metric Tracking provides a system to be notified when metrics have been
successfully written to their outputs or otherwise discarded. This allows
inputs to be created that function as reliable queue consumers.
To get started with metric tracking begin by calling `WithTracking` on the
[telegraf.Accumulator][]. Add metrics using the `AddTrackingMetricGroup`
function on the returned [telegraf.TrackingAccumulator][] and store the
`TrackingID` . The `Delivered()` channel will return a type with information
about the final delivery status of the metric group.
Check the [amqp_consumer][] for an example implementation.
[exec]: https://github.com/influxdata/telegraf/tree/master/plugins/inputs/exec
[amqp_consumer]: https://github.com/influxdata/telegraf/tree/master/plugins/inputs/amqp_consumer
[prom metric types]: https://prometheus.io/docs/concepts/metric_types/
[input data formats]: https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
2021-05-08 03:26:11 +08:00
[Sample Config]: https://github.com/influxdata/telegraf/blob/master/docs/developers/SAMPLE_CONFIG.md
[Code Style]: https://github.com/influxdata/telegraf/blob/master/docs/developers/CODE_STYLE.md
2018-11-06 05:34:28 +08:00
[telegraf.Input]: https://godoc.org/github.com/influxdata/telegraf#Input
[telegraf.ServiceInput]: https://godoc.org/github.com/influxdata/telegraf#ServiceInput
[telegraf.Accumulator]: https://godoc.org/github.com/influxdata/telegraf#Accumulator
[telegraf.TrackingAccumulator]: https://godoc.org/github.com/influxdata/telegraf#Accumulator