Loading & namespacing external plugins

This commit is contained in:
Cameron Sparr 2017-02-06 16:45:35 +00:00
parent 2eee1b84fb
commit f4e48f9909
No known key found for this signature in database
GPG Key ID: 19E67263DCB25D0F
9 changed files with 78 additions and 5 deletions

View File

@ -16,6 +16,7 @@ import (
"github.com/influxdata/telegraf/agent"
"github.com/influxdata/telegraf/internal/config"
"github.com/influxdata/telegraf/logger"
"github.com/influxdata/telegraf/registry"
"github.com/influxdata/telegraf/registry/inputs"
"github.com/influxdata/telegraf/registry/outputs"
@ -273,6 +274,15 @@ func loadExternalPlugins(rootDir string) error {
return nil
}
// name will be the path to the plugin file beginning at the root
// directory, minus the extension.
// ie, if the plugin file is /opt/telegraf-plugins/group1/foo.so, name
// will be "group1/foo"
name := strings.TrimPrefix(strings.TrimPrefix(pth, rootDir), string(os.PathSeparator))
name = strings.TrimSuffix(name, filepath.Ext(pth))
registry.SetName("external" + string(os.PathSeparator) + name)
defer registry.SetName("")
// Load plugin.
_, err = plugin.Open(pth)
if err != nil {

1
doc.go
View File

@ -1 +0,0 @@
package telegraf

View File

@ -40,6 +40,14 @@ var (
// envVarRe is a regex to find environment variables in the config file
envVarRe = regexp.MustCompile(`\$\w+`)
// addQuoteRe is a regex for finding and adding quotes around / characters
// when they are used for distinguishing external plugins.
// ie, a ReplaceAll() with this pattern will be used to turn this:
// [[inputs.external/test/example]]
// to
// [[inputs."external/test/example"]]
addQuoteRe = regexp.MustCompile(`(\[?\[?inputs|outputs|processors|aggregators)\.(external\/[^.\]]+)`)
)
// Config specifies the URL/user/password for the database that telegraf
@ -701,6 +709,9 @@ func parseFile(fpath string) (*ast.Table, error) {
}
}
// add quotes around external plugin paths.
contents = addQuoteRe.ReplaceAll(contents, []byte(`$1."$2"`))
return toml.Parse(contents)
}

View File

@ -1,11 +1,20 @@
package aggregators
import "github.com/influxdata/telegraf"
import (
"log"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/registry"
)
type Creator func() telegraf.Aggregator
var Aggregators = map[string]Creator{}
func Add(name string, creator Creator) {
if override := registry.GetName(); override != "" {
name = override
}
log.Println("D! Loading plugin: [[aggregators." + name + "]]")
Aggregators[name] = creator
}

View File

@ -1 +0,0 @@
package registry

View File

@ -1,11 +1,20 @@
package inputs
import "github.com/influxdata/telegraf"
import (
"log"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/registry"
)
type Creator func() telegraf.Input
var Inputs = map[string]Creator{}
func Add(name string, creator Creator) {
if override := registry.GetName(); override != "" {
name = override
}
log.Println("D! Loading plugin: [[inputs." + name + "]]")
Inputs[name] = creator
}

20
registry/name.go Normal file
View File

@ -0,0 +1,20 @@
package registry
import (
"sync"
)
var nameOverride string
var mu sync.Mutex
func SetName(s string) {
mu.Lock()
nameOverride = s
mu.Unlock()
}
func GetName() string {
mu.Lock()
defer mu.Unlock()
return nameOverride
}

View File

@ -1,7 +1,10 @@
package outputs
import (
"log"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/registry"
)
type Creator func() telegraf.Output
@ -9,5 +12,9 @@ type Creator func() telegraf.Output
var Outputs = map[string]Creator{}
func Add(name string, creator Creator) {
if override := registry.GetName(); override != "" {
name = override
}
log.Println("D! Loading plugin: [[outputs." + name + "]]")
Outputs[name] = creator
}

View File

@ -1,11 +1,20 @@
package processors
import "github.com/influxdata/telegraf"
import (
"log"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/registry"
)
type Creator func() telegraf.Processor
var Processors = map[string]Creator{}
func Add(name string, creator Creator) {
if override := registry.GetName(); override != "" {
name = override
}
log.Println("D! Loading plugin: [[processors." + name + "]]")
Processors[name] = creator
}