Skip to content

Commit 5facdef

Browse files
committed
Require goplugin build flag to enable go plugin support (influxdata#6393)
(cherry picked from commit 776e92f)
1 parent 797c24a commit 5facdef

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

cmd/telegraf/telegraf.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
_ "net/http/pprof" // Comment this line to disable pprof endpoint.
1111
"os"
1212
"os/signal"
13-
"path"
14-
"path/filepath"
15-
"plugin"
1613
"runtime"
1714
"strings"
1815
"syscall"
@@ -21,6 +18,7 @@ import (
2118
"github.com/influxdata/telegraf/agent"
2219
"github.com/influxdata/telegraf/internal"
2320
"github.com/influxdata/telegraf/internal/config"
21+
"github.com/influxdata/telegraf/internal/goplugin"
2422
"github.com/influxdata/telegraf/logger"
2523
_ "github.com/influxdata/telegraf/plugins/aggregators/all"
2624
"github.com/influxdata/telegraf/plugins/inputs"
@@ -116,36 +114,6 @@ func reloadLoop(
116114
}
117115
}
118116

119-
// loadExternalPlugins loads external plugins from shared libraries (.so, .dll, etc.)
120-
// in the specified directory.
121-
func loadExternalPlugins(rootDir string) error {
122-
return filepath.Walk(rootDir, func(pth string, info os.FileInfo, err error) error {
123-
// Stop if there was an error.
124-
if err != nil {
125-
return err
126-
}
127-
128-
// Ignore directories.
129-
if info.IsDir() {
130-
return nil
131-
}
132-
133-
// Ignore files that aren't shared libraries.
134-
ext := strings.ToLower(path.Ext(pth))
135-
if ext != ".so" && ext != ".dll" {
136-
return nil
137-
}
138-
139-
// Load plugin.
140-
_, err = plugin.Open(pth)
141-
if err != nil {
142-
return fmt.Errorf("error loading %s: %s", pth, err)
143-
}
144-
145-
return nil
146-
})
147-
}
148-
149117
func runAgent(ctx context.Context,
150118
inputFilters []string,
151119
outputFilters []string,
@@ -318,7 +286,7 @@ func main() {
318286
// Load external plugins, if requested.
319287
if *fPlugins != "" {
320288
log.Printf("I! Loading external plugins from: %s", *fPlugins)
321-
if err := loadExternalPlugins(*fPlugins); err != nil {
289+
if err := goplugin.LoadExternalPlugins(*fPlugins); err != nil {
322290
log.Fatal("E! " + err.Error())
323291
}
324292
}

internal/goplugin/noplugin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// +build !goplugin
2+
3+
package goplugin
4+
5+
import "errors"
6+
7+
func LoadExternalPlugins(rootDir string) error {
8+
return errors.New("go plugin support is not enabled")
9+
}

internal/goplugin/plugin.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// +build goplugin
2+
3+
package goplugin
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"path"
9+
"path/filepath"
10+
"plugin"
11+
"strings"
12+
)
13+
14+
// loadExternalPlugins loads external plugins from shared libraries (.so, .dll, etc.)
15+
// in the specified directory.
16+
func LoadExternalPlugins(rootDir string) error {
17+
return filepath.Walk(rootDir, func(pth string, info os.FileInfo, err error) error {
18+
// Stop if there was an error.
19+
if err != nil {
20+
return err
21+
}
22+
23+
// Ignore directories.
24+
if info.IsDir() {
25+
return nil
26+
}
27+
28+
// Ignore files that aren't shared libraries.
29+
ext := strings.ToLower(path.Ext(pth))
30+
if ext != ".so" && ext != ".dll" {
31+
return nil
32+
}
33+
34+
// Load plugin.
35+
_, err = plugin.Open(pth)
36+
if err != nil {
37+
return fmt.Errorf("error loading %s: %s", pth, err)
38+
}
39+
40+
return nil
41+
})
42+
}

0 commit comments

Comments
 (0)