From f4e48f99098b51c494a3b0c4d6f0586443c23f8a Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Mon, 6 Feb 2017 16:45:35 +0000 Subject: [PATCH] Loading & namespacing external plugins --- cmd/telegraf/telegraf.go | 10 ++++++++++ doc.go | 1 - internal/config/config.go | 11 +++++++++++ registry/aggregators/registry.go | 11 ++++++++++- registry/doc.go | 1 - registry/inputs/registry.go | 11 ++++++++++- registry/name.go | 20 ++++++++++++++++++++ registry/outputs/registry.go | 7 +++++++ registry/processors/registry.go | 11 ++++++++++- 9 files changed, 78 insertions(+), 5 deletions(-) delete mode 100644 doc.go delete mode 100644 registry/doc.go create mode 100644 registry/name.go diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 26d6ebc66..be22fdf3a 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -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 { diff --git a/doc.go b/doc.go deleted file mode 100644 index 8757fc4a6..000000000 --- a/doc.go +++ /dev/null @@ -1 +0,0 @@ -package telegraf diff --git a/internal/config/config.go b/internal/config/config.go index 4e68038d2..763fe6ad0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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) } diff --git a/registry/aggregators/registry.go b/registry/aggregators/registry.go index 77a9c9a64..fe3858497 100644 --- a/registry/aggregators/registry.go +++ b/registry/aggregators/registry.go @@ -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 } diff --git a/registry/doc.go b/registry/doc.go deleted file mode 100644 index b2a276fb4..000000000 --- a/registry/doc.go +++ /dev/null @@ -1 +0,0 @@ -package registry diff --git a/registry/inputs/registry.go b/registry/inputs/registry.go index 9951cd5cd..51472c18a 100644 --- a/registry/inputs/registry.go +++ b/registry/inputs/registry.go @@ -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 } diff --git a/registry/name.go b/registry/name.go new file mode 100644 index 000000000..33c42904f --- /dev/null +++ b/registry/name.go @@ -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 +} diff --git a/registry/outputs/registry.go b/registry/outputs/registry.go index 5787a82b0..bb3b9d366 100644 --- a/registry/outputs/registry.go +++ b/registry/outputs/registry.go @@ -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 } diff --git a/registry/processors/registry.go b/registry/processors/registry.go index 592c688f3..b5026e005 100644 --- a/registry/processors/registry.go +++ b/registry/processors/registry.go @@ -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 }