Commit 44e485af by bergquist

backend plugins: improves logging

parent 75a54e85
...@@ -92,7 +92,7 @@ func pluginScenario(desc string, t *testing.T, fn func()) { ...@@ -92,7 +92,7 @@ func pluginScenario(desc string, t *testing.T, fn func()) {
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.test-app") sec, _ := setting.Cfg.NewSection("plugin.test-app")
sec.NewKey("path", "../../tests/test-app") sec.NewKey("path", "../../tests/test-app")
err := Init(context.TODO()) err := initPlugins(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
......
...@@ -18,7 +18,7 @@ func TestPluginDashboards(t *testing.T) { ...@@ -18,7 +18,7 @@ func TestPluginDashboards(t *testing.T) {
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.test-app") sec, _ := setting.Cfg.NewSection("plugin.test-app")
sec.NewKey("path", "../../tests/test-app") sec.NewKey("path", "../../tests/test-app")
err := Init(context.TODO()) err := initPlugins(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
......
...@@ -73,10 +73,12 @@ func buildExecutablePath(pluginDir, executable, os, arch string) string { ...@@ -73,10 +73,12 @@ func buildExecutablePath(pluginDir, executable, os, arch string) string {
func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger) error { func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger) error {
p.log = log.New("plugin-id", p.Id) p.log = log.New("plugin-id", p.Id)
p.spawnSubProcess() err := p.spawnSubProcess()
go p.reattachKilledProcess(ctx) if err == nil {
go p.restartKilledProcess(ctx)
}
return nil return err
} }
func (p *DataSourcePlugin) spawnSubProcess() error { func (p *DataSourcePlugin) spawnSubProcess() error {
...@@ -109,7 +111,7 @@ func (p *DataSourcePlugin) spawnSubProcess() error { ...@@ -109,7 +111,7 @@ func (p *DataSourcePlugin) spawnSubProcess() error {
return nil return nil
} }
func (p *DataSourcePlugin) reattachKilledProcess(ctx context.Context) error { func (p *DataSourcePlugin) restartKilledProcess(ctx context.Context) error {
ticker := time.NewTicker(time.Second * 1) ticker := time.NewTicker(time.Second * 1)
for { for {
......
...@@ -23,7 +23,6 @@ var ( ...@@ -23,7 +23,6 @@ var (
StaticRoutes []*PluginStaticRoute StaticRoutes []*PluginStaticRoute
Apps map[string]*AppPlugin Apps map[string]*AppPlugin
Plugins map[string]*PluginBase Plugins map[string]*PluginBase
//BackendDatasources map[string]*BackendDatasource
PluginTypes map[string]interface{} PluginTypes map[string]interface{}
GrafanaLatestVersion string GrafanaLatestVersion string
...@@ -43,7 +42,12 @@ type PluginManager struct { ...@@ -43,7 +42,12 @@ type PluginManager struct {
} }
func NewPluginManager(ctx context.Context) (*PluginManager, error) { func NewPluginManager(ctx context.Context) (*PluginManager, error) {
Init(ctx) err := initPlugins(ctx)
if err != nil {
return nil, err
}
return &PluginManager{ return &PluginManager{
log: log.New("plugins"), log: log.New("plugins"),
}, nil }, nil
...@@ -60,14 +64,14 @@ func (p *PluginManager) Run(ctx context.Context) error { ...@@ -60,14 +64,14 @@ func (p *PluginManager) Run(ctx context.Context) error {
return ctx.Err() return ctx.Err()
} }
func Init(ctx context.Context) error { func initPlugins(ctx context.Context) error {
plog = log.New("plugins") plog = log.New("plugins")
DataSources = make(map[string]*DataSourcePlugin) DataSources = map[string]*DataSourcePlugin{}
StaticRoutes = make([]*PluginStaticRoute, 0) StaticRoutes = []*PluginStaticRoute{}
Panels = make(map[string]*PanelPlugin) Panels = map[string]*PanelPlugin{}
Apps = make(map[string]*AppPlugin) Apps = map[string]*AppPlugin{}
Plugins = make(map[string]*PluginBase) Plugins = map[string]*PluginBase{}
PluginTypes = map[string]interface{}{ PluginTypes = map[string]interface{}{
"panel": PanelPlugin{}, "panel": PanelPlugin{},
"datasource": DataSourcePlugin{}, "datasource": DataSourcePlugin{},
...@@ -79,9 +83,8 @@ func Init(ctx context.Context) error { ...@@ -79,9 +83,8 @@ func Init(ctx context.Context) error {
// check if plugins dir exists // check if plugins dir exists
if _, err := os.Stat(setting.PluginsPath); os.IsNotExist(err) { if _, err := os.Stat(setting.PluginsPath); os.IsNotExist(err) {
plog.Warn("Plugin dir does not exist", "dir", setting.PluginsPath)
if err = os.MkdirAll(setting.PluginsPath, os.ModePerm); err != nil { if err = os.MkdirAll(setting.PluginsPath, os.ModePerm); err != nil {
plog.Warn("Failed to create plugin dir", "dir", setting.PluginsPath, "error", err) plog.Error("Failed to create plugin dir", "dir", setting.PluginsPath, "error", err)
} else { } else {
plog.Info("Plugin dir created", "dir", setting.PluginsPath) plog.Info("Plugin dir created", "dir", setting.PluginsPath)
scan(setting.PluginsPath) scan(setting.PluginsPath)
...@@ -96,13 +99,18 @@ func Init(ctx context.Context) error { ...@@ -96,13 +99,18 @@ func Init(ctx context.Context) error {
for _, panel := range Panels { for _, panel := range Panels {
panel.initFrontendPlugin() panel.initFrontendPlugin()
} }
for _, ds := range DataSources { for _, ds := range DataSources {
if ds.Backend { if ds.Backend {
ds.initBackendPlugin(ctx, plog) err := ds.initBackendPlugin(ctx, plog)
if err != nil {
plog.Error("Failed to init plugin.", "error", err, "plugin", ds.Id)
}
} }
ds.initFrontendPlugin() ds.initFrontendPlugin()
} }
for _, app := range Apps { for _, app := range Apps {
app.initApp() app.initApp()
} }
......
...@@ -15,7 +15,7 @@ func TestPluginScans(t *testing.T) { ...@@ -15,7 +15,7 @@ func TestPluginScans(t *testing.T) {
Convey("When scaning for plugins", t, func() { Convey("When scaning for plugins", t, func() {
setting.StaticRootPath, _ = filepath.Abs("../../public/") setting.StaticRootPath, _ = filepath.Abs("../../public/")
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
err := Init(context.TODO()) err := initPlugins(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(len(DataSources), ShouldBeGreaterThan, 1) So(len(DataSources), ShouldBeGreaterThan, 1)
...@@ -30,7 +30,7 @@ func TestPluginScans(t *testing.T) { ...@@ -30,7 +30,7 @@ func TestPluginScans(t *testing.T) {
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.nginx-app") sec, _ := setting.Cfg.NewSection("plugin.nginx-app")
sec.NewKey("path", "../../tests/test-app") sec.NewKey("path", "../../tests/test-app")
err := Init(context.TODO()) err := initPlugins(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(len(Apps), ShouldBeGreaterThan, 0) So(len(Apps), ShouldBeGreaterThan, 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment