Commit 44e485af by bergquist

backend plugins: improves logging

parent 75a54e85
......@@ -92,7 +92,7 @@ func pluginScenario(desc string, t *testing.T, fn func()) {
setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.test-app")
sec.NewKey("path", "../../tests/test-app")
err := Init(context.TODO())
err := initPlugins(context.TODO())
So(err, ShouldBeNil)
......
......@@ -18,7 +18,7 @@ func TestPluginDashboards(t *testing.T) {
setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.test-app")
sec.NewKey("path", "../../tests/test-app")
err := Init(context.TODO())
err := initPlugins(context.TODO())
So(err, ShouldBeNil)
......
......@@ -73,10 +73,12 @@ func buildExecutablePath(pluginDir, executable, os, arch string) string {
func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger) error {
p.log = log.New("plugin-id", p.Id)
p.spawnSubProcess()
go p.reattachKilledProcess(ctx)
err := p.spawnSubProcess()
if err == nil {
go p.restartKilledProcess(ctx)
}
return nil
return err
}
func (p *DataSourcePlugin) spawnSubProcess() error {
......@@ -109,7 +111,7 @@ func (p *DataSourcePlugin) spawnSubProcess() error {
return nil
}
func (p *DataSourcePlugin) reattachKilledProcess(ctx context.Context) error {
func (p *DataSourcePlugin) restartKilledProcess(ctx context.Context) error {
ticker := time.NewTicker(time.Second * 1)
for {
......
......@@ -23,7 +23,6 @@ var (
StaticRoutes []*PluginStaticRoute
Apps map[string]*AppPlugin
Plugins map[string]*PluginBase
//BackendDatasources map[string]*BackendDatasource
PluginTypes map[string]interface{}
GrafanaLatestVersion string
......@@ -43,7 +42,12 @@ type PluginManager struct {
}
func NewPluginManager(ctx context.Context) (*PluginManager, error) {
Init(ctx)
err := initPlugins(ctx)
if err != nil {
return nil, err
}
return &PluginManager{
log: log.New("plugins"),
}, nil
......@@ -60,14 +64,14 @@ func (p *PluginManager) Run(ctx context.Context) error {
return ctx.Err()
}
func Init(ctx context.Context) error {
func initPlugins(ctx context.Context) error {
plog = log.New("plugins")
DataSources = make(map[string]*DataSourcePlugin)
StaticRoutes = make([]*PluginStaticRoute, 0)
Panels = make(map[string]*PanelPlugin)
Apps = make(map[string]*AppPlugin)
Plugins = make(map[string]*PluginBase)
DataSources = map[string]*DataSourcePlugin{}
StaticRoutes = []*PluginStaticRoute{}
Panels = map[string]*PanelPlugin{}
Apps = map[string]*AppPlugin{}
Plugins = map[string]*PluginBase{}
PluginTypes = map[string]interface{}{
"panel": PanelPlugin{},
"datasource": DataSourcePlugin{},
......@@ -79,9 +83,8 @@ func Init(ctx context.Context) error {
// check if plugins dir exists
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 {
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 {
plog.Info("Plugin dir created", "dir", setting.PluginsPath)
scan(setting.PluginsPath)
......@@ -96,13 +99,18 @@ func Init(ctx context.Context) error {
for _, panel := range Panels {
panel.initFrontendPlugin()
}
for _, ds := range DataSources {
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()
}
for _, app := range Apps {
app.initApp()
}
......
......@@ -15,7 +15,7 @@ func TestPluginScans(t *testing.T) {
Convey("When scaning for plugins", t, func() {
setting.StaticRootPath, _ = filepath.Abs("../../public/")
setting.Cfg = ini.Empty()
err := Init(context.TODO())
err := initPlugins(context.TODO())
So(err, ShouldBeNil)
So(len(DataSources), ShouldBeGreaterThan, 1)
......@@ -30,7 +30,7 @@ func TestPluginScans(t *testing.T) {
setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.nginx-app")
sec.NewKey("path", "../../tests/test-app")
err := Init(context.TODO())
err := initPlugins(context.TODO())
So(err, ShouldBeNil)
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