2021-11-25 02:45:19 +08:00
# Output Plugins
2018-11-06 05:34:28 +08:00
This section is for developers who want to create a new output sink. Outputs
are created in a similar manner as collection plugins, and their interface has
similar constructs.
2021-11-25 02:45:19 +08:00
## Output Plugin Guidelines
2018-11-06 05:34:28 +08:00
- An output must conform to the [telegraf.Output][] interface.
- Outputs should call `outputs.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/outputs/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
2021-11-25 02:45:19 +08:00
## Output 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. `simpleoutput.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 simpleoutput
// simpleoutput.go
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/outputs"
)
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) Connect() error {
2020-10-14 23:12:41 +08:00
// Make any connection required here
2018-11-06 05:34:28 +08:00
return nil
}
func (s *Simple) Close() error {
2020-10-14 23:12:41 +08:00
// Close any connections here.
// Write will not be called once Close is called, so there is no need to synchronize.
2018-11-06 05:34:28 +08:00
return nil
}
2020-10-14 23:12:41 +08:00
// 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.
2018-11-06 05:34:28 +08:00
func (s *Simple) Write(metrics []telegraf.Metric) error {
for _, metric := range metrics {
// write `metric` to the output sink here
}
return nil
}
func init() {
outputs.Add("simpleoutput", func() telegraf.Output { return & Simple{} })
}
2022-08-16 04:44:59 +08:00
```
Registration of the plugin on `plugins/outputs/all/simpleoutput.go` :
```go
//go:build !custom || outputs || outputs.simpleoutput
package all
import _ "github.com/influxdata/telegraf/plugins/outputs/simpleoutput" // register plugin
2018-11-06 05:34:28 +08:00
```
2022-08-16 04:44:59 +08:00
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
## Data Formats
Some output plugins, such as the [file][] plugin, can write in any supported
[output data formats][].
In order to enable this, you must specify a
`SetSerializer(serializer serializers.Serializer)`
function on the plugin object (see the file plugin for an example), as well as
defining `serializer` as a field of the object.
You can then utilize the serializer internally in your plugin, serializing data
before it's written. Telegraf's configuration layer will take care of
instantiating and creating the `Serializer` object.
You should also add the following to your `SampleConfig()` :
```toml
## Data format to output.
## 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_OUTPUT.md
data_format = "influx"
```
2020-04-21 01:49:10 +08:00
## Flushing Metrics to Outputs
Metrics are flushed to outputs when any of the following events happen:
2021-11-25 02:45:19 +08:00
2020-04-21 01:49:10 +08:00
- `flush_interval + rand(flush_jitter)` has elapsed since start or the last flush interval
- At least `metric_batch_size` count of metrics are waiting in the buffer
- The telegraf process has received a SIGUSR1 signal
Note that if the flush takes longer than the `agent.interval` to write the metrics
2020-10-14 23:12:41 +08:00
to the output, you'll see a message saying the output `did not complete within its
flush interval`. This may mean your output is not keeping up with the flow of metrics,
and you may want to look into enabling compression, reducing the size of your metrics,
2020-04-21 01:49:10 +08:00
or investigate other reasons why the writes might be taking longer than expected.
2018-11-06 05:34:28 +08:00
[file]: https://github.com/influxdata/telegraf/tree/master/plugins/inputs/file
[output data formats]: https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.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.Output]: https://godoc.org/github.com/influxdata/telegraf#Output