Skip to content

Commit

Permalink
Loading & namespacing external plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Feb 6, 2017
1 parent 2eee1b8 commit 9e57c5f
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 5 deletions.
5 changes: 5 additions & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -273,6 +274,10 @@ func loadExternalPlugins(rootDir string) error {
return nil
}

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 {
Expand Down
1 change: 0 additions & 1 deletion doc.go

This file was deleted.

11 changes: 11 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
11 changes: 10 additions & 1 deletion registry/aggregators/registry.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion registry/doc.go

This file was deleted.

11 changes: 10 additions & 1 deletion registry/inputs/registry.go
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions registry/name.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions registry/outputs/registry.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package outputs

import (
"log"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/registry"
)

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
}
11 changes: 10 additions & 1 deletion registry/processors/registry.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 9e57c5f

Please sign in to comment.