Skip to content

Commit

Permalink
chore: Use build tags to select plugins (#11654)
Browse files Browse the repository at this point in the history
  • Loading branch information
reimda authored Aug 15, 2022
1 parent fbf1151 commit 07179c4
Show file tree
Hide file tree
Showing 338 changed files with 1,763 additions and 358 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ docs: build_tools embed_readme_inputs embed_readme_outputs embed_readme_processo

.PHONY: build
build:
go build -ldflags "$(LDFLAGS)" ./cmd/telegraf
go build -tags "$(BUILDTAGS)" -ldflags "$(LDFLAGS)" ./cmd/telegraf

.PHONY: telegraf
telegraf: build
Expand Down
26 changes: 24 additions & 2 deletions docs/AGGREGATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ This section is for developers who want to create a new aggregator plugin.
* A aggregator must conform to the [telegraf.Aggregator][] interface.
* Aggregators should call `aggregators.Add` in their `init` function to
register themselves. See below for a quick example.
* To be available within Telegraf itself, plugins must add themselves to the
`github.com/influxdata/telegraf/plugins/aggregators/all/all.go` file.
* To be available within Telegraf itself, plugins must register themselves
using a file in `github.com/influxdata/telegraf/plugins/aggregators/all`
named according to the plugin name. Make sure your also add build-tags to
conditionally build the plugin.
* Each plugin requires a file called `sample.conf` containing the sample configuration
for the plugin in TOML format.
Please consult the [Sample Config][] page for the latest style guidelines.
Expand All @@ -22,6 +24,8 @@ This section is for developers who want to create a new aggregator plugin.

### Aggregator Plugin Example

Content of your plugin file e.g. `min.go`

```go
//go:generate ../../../tools/readme_config_includer/generator
package min
Expand Down Expand Up @@ -122,3 +126,21 @@ func init() {
})
}
```

Registration of the plugin on `plugins/aggregators/all/min.go`:

```go
//go:build !custom || aggregators || aggregators.min

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/min" // register plugin

```

The _build-tags_ in the first line allow to selectively include/exclude your
plugin when customizing Telegraf.

[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
[telegraf.Aggregator]: https://godoc.org/github.com/influxdata/telegraf#Aggregator
45 changes: 45 additions & 0 deletions docs/CUSTOMIZATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Customization

You can build customized versions of Telegraf with a specific plugin set
using [build-tags](https://pkg.go.dev/cmd/go#hdr-Build_constraints).
The plugins can be selected either category-wise, i.e. `inputs`, `outputs`,
`processors`, `aggregators` and `parsers` or individually, e.g. `inputs.modbus`
or `outputs.influxdb`.

Usually the build tags correspond to the plugin names used in the Telegraf
configuration. To be sure, check the files in the corresponding
`plugin/<category>/all` directory. Make sure to include all parsers you intend
to use.

__Note:__ You _always_ need to include the `custom` tag when customizing the
build as otherwise _all_ plugins will be selected regardless of other tags.

## Via make

When using the project's makefile, the build can be customized via the
`BUILDTAGS` environment variable containing a __space-separated__ list of the
selected plugins (or categories) __and__ the `custom` tag.

For example

```shell
BUILDTAGS="custom inputs outputs.influxdb_v2 parsers.json" make
```

will build a customized Telegraf including _all_ `inputs`, the InfluxDB v2
`output` and the `json` parser.

## Via `go build`

If you wish to build Telegraf using native go tools, you can use the `go build`
command with the `-tags` option. Specify a __space-separated__ list of the
selected plugins (or categories) __and__ the `custom` tag as argument.

For example

```shell
go build -tags "custom inputs outputs.influxdb_v2 parsers.json" ./cmd/telegraf
```

will build a customized Telegraf including _all_ `inputs`, the InfluxDB v2
`output` and the `json` parser.
24 changes: 21 additions & 3 deletions docs/INPUTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ and submit new inputs.
- 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.
- Input Plugins must be added to the
`github.com/influxdata/telegraf/plugins/inputs/all/all.go` file.
- 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.
- Each plugin requires a file called `sample.conf` containing the sample
configuration for the plugin in TOML format.
Please consult the [Sample Config][] page for the latest style guidelines.
Expand All @@ -27,6 +29,8 @@ current host.

## Input Plugin Example

Content of your plugin file e.g. `simple.go`

```go
//go:generate ../../../tools/readme_config_includer/generator
package simple
Expand Down Expand Up @@ -71,11 +75,25 @@ func init() {
}
```

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.

### Development

- Run `make static` followed by `make plugin-[pluginName]` to spin up a docker
dev environment using docker-compose.
- ***[Optional]*** When developing a plugin, add a `dev` directory with a
- __[Optional]__ When developing a plugin, add a `dev` directory with a
`docker-compose.yml` and `telegraf.conf` as well as any other supporting
files, where sensible.

Expand Down
21 changes: 19 additions & 2 deletions docs/OUTPUTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ similar constructs.
- 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.
- To be available within Telegraf itself, plugins must add themselves to the
`github.com/influxdata/telegraf/plugins/outputs/all/all.go` file.
- 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.
- Each plugin requires a file called `sample.conf` containing the sample
configuration for the plugin in TOML format.
Please consult the [Sample Config][] page for the latest style guidelines.
Expand All @@ -20,6 +22,8 @@ similar constructs.

## Output Plugin Example

Content of your plugin file e.g. `simpleoutput.go`

```go
//go:generate ../../../tools/readme_config_includer/generator
package simpleoutput
Expand Down Expand Up @@ -75,9 +79,22 @@ func (s *Simple) Write(metrics []telegraf.Metric) error {
func init() {
outputs.Add("simpleoutput", func() telegraf.Output { return &Simple{} })
}
```

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

```

The _build-tags_ in the first line allow to selectively include/exclude your
plugin when customizing Telegraf.

## Data Formats

Some output plugins, such as the [file][] plugin, can write in any supported
Expand Down
21 changes: 19 additions & 2 deletions docs/PROCESSORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ This section is for developers who want to create a new processor plugin.
* A processor must conform to the [telegraf.Processor][] interface.
* Processors should call `processors.Add` in their `init` function to register
themselves. See below for a quick example.
* To be available within Telegraf itself, plugins must add themselves to the
`github.com/influxdata/telegraf/plugins/processors/all/all.go` file.
* To be available within Telegraf itself, plugins must register themselves
using a file in `github.com/influxdata/telegraf/plugins/processors/all`
named according to the plugin name. Make sure your also add build-tags to
conditionally build the plugin.
* Each plugin requires a file called `sample.conf` containing the sample
configuration for the plugin in TOML format.
Please consult the [Sample Config][] page for the latest style guidelines.
Expand All @@ -18,6 +20,8 @@ This section is for developers who want to create a new processor plugin.

## Processor Plugin Example

Content of your plugin file e.g. `printer.go`

```go
//go:generate ../../../tools/readme_config_includer/generator
package printer
Expand Down Expand Up @@ -63,6 +67,19 @@ func init() {
}
```

Registration of the plugin on `plugins/processors/all/printer.go`:

```go
//go:build !custom || processors || processors.printer

package all

import _ "github.com/influxdata/telegraf/plugins/processors/printer" // register plugin
```

The _build-tags_ in the first line allow to selectively include/exclude your
plugin when customizing Telegraf.

## Streaming Processors

Streaming processors are a new processor type available to you. They are
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- [Windows Service][winsvc]
- [FAQ][faq]
- Developer Builds
- [Nightlies](nightlies)
- [Nightlies][nightlies]

[conf]: /docs/CONFIGURATION.md
[metrics]: /docs/METRICS.md
Expand Down
13 changes: 0 additions & 13 deletions plugins/aggregators/all/all.go
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
package all

import (
//Blank imports for plugins to register themselves
_ "github.com/influxdata/telegraf/plugins/aggregators/basicstats"
_ "github.com/influxdata/telegraf/plugins/aggregators/derivative"
_ "github.com/influxdata/telegraf/plugins/aggregators/final"
_ "github.com/influxdata/telegraf/plugins/aggregators/histogram"
_ "github.com/influxdata/telegraf/plugins/aggregators/merge"
_ "github.com/influxdata/telegraf/plugins/aggregators/minmax"
_ "github.com/influxdata/telegraf/plugins/aggregators/quantile"
_ "github.com/influxdata/telegraf/plugins/aggregators/starlark"
_ "github.com/influxdata/telegraf/plugins/aggregators/valuecounter"
)
5 changes: 5 additions & 0 deletions plugins/aggregators/all/basicstats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.basicstats

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/basicstats" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/derivative.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.derivative

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/derivative" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/final.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.final

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/final" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/histogram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.histogram

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/histogram" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.merge

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/merge" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/minmax.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.minmax

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/minmax" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/quantile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.quantile

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/quantile" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/starlark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.starlark

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/starlark" // register plugin
5 changes: 5 additions & 0 deletions plugins/aggregators/all/valuecounter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || aggregators || aggregators.valuecounter

package all

import _ "github.com/influxdata/telegraf/plugins/aggregators/valuecounter" // register plugin
5 changes: 5 additions & 0 deletions plugins/inputs/all/activemq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || inputs || inputs.activemq

package all

import _ "github.com/influxdata/telegraf/plugins/inputs/activemq" // register plugin
5 changes: 5 additions & 0 deletions plugins/inputs/all/aerospike.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || inputs || inputs.aerospike

package all

import _ "github.com/influxdata/telegraf/plugins/inputs/aerospike" // register plugin
5 changes: 5 additions & 0 deletions plugins/inputs/all/aliyuncms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || inputs || inputs.aliyuncms

package all

import _ "github.com/influxdata/telegraf/plugins/inputs/aliyuncms" // register plugin
Loading

0 comments on commit 07179c4

Please sign in to comment.