feature: support dlopen for prober plugin (#588)

This commit is contained in:
yubo 2021-02-23 18:04:03 +08:00 committed by GitHub
parent b055bc73c5
commit 9c1c894e29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package all
import (
"fmt"
"os"
"path/filepath"
"plugin"
"strings"
)
const pluginDir = "plugins"
func init() {
plugins, err := listPlugins(pluginDir)
if err != nil {
fmt.Printf("list plugins: \n", err)
return
}
for _, file := range plugins {
_, err := plugin.Open(file)
if err != nil {
fmt.Printf("plugin.Open %s err %s\n", file, err)
continue
}
}
}
func listPlugins(dir string) ([]string, error) {
df, err := os.Open(dir)
if err != nil {
return nil, fmt.Errorf("failed opening directory: %s", err)
}
defer df.Close()
list, err := df.Readdirnames(0) // 0 to read all files and folders
if err != nil {
return nil, fmt.Errorf("read dir names: %s", err)
}
var plugins []string
for _, name := range list {
if !strings.HasSuffix(name, ".so") {
continue
}
file := filepath.Join(dir, name)
if fi, err := os.Stat(file); err != nil {
continue
} else if fi.IsDir() {
continue
}
plugins = append(plugins, file)
}
return plugins, nil
}

View File

@ -0,0 +1,4 @@
all: lib.so
lib.so: lib.go
go build -buildmode=plugin -o $@

View File

@ -0,0 +1,5 @@
package main
import (
_ "github.com/didi/nightingale/src/modules/monapi/plugins/demo"
)

View File

@ -19,6 +19,7 @@ import (
"github.com/didi/nightingale/src/modules/prober/http"
"github.com/didi/nightingale/src/modules/prober/manager"
"github.com/didi/nightingale/src/modules/monapi/collector"
_ "github.com/didi/nightingale/src/modules/monapi/plugins/all"
_ "github.com/go-sql-driver/mysql"
@ -78,6 +79,8 @@ func main() {
manager.NewManager(cfg, cache.CollectRule).Start(ctx)
pluginInfo()
http.Start()
ending(cancel)
@ -132,3 +135,10 @@ func ending(cancel context.CancelFunc) {
http.Shutdown()
fmt.Printf("%s stopped successfully\n", os.Args[0])
}
func pluginInfo() {
fmt.Println("remote collector")
for k, v := range collector.GetRemoteCollectors() {
fmt.Printf(" %d %s\n", k, v)
}
}