mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 22:57:16 +08:00
Backend Plugins: Refactor backend plugin registration and start (#21452)
Moves the details of loading plugins into the backend plugin manager from the respective plugin (datasource, transform and renderer).
This commit is contained in:

committed by
GitHub

parent
8505d90768
commit
bb849d53bf
@ -2,22 +2,16 @@ package plugins
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
|
||||
datasourceV1 "github.com/grafana/grafana-plugin-model/go/datasource"
|
||||
sdk "github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins/datasource/wrapper"
|
||||
"github.com/grafana/grafana/pkg/tsdb"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
)
|
||||
|
||||
// DataSourcePlugin contains all metadata about a datasource plugin
|
||||
@ -45,10 +39,6 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
|
||||
return errutil.Wrapf(err, "Failed to decode datasource plugin")
|
||||
}
|
||||
|
||||
if !p.isVersionOne() && !setting.IsExpressionsEnabled() {
|
||||
return errors.New("A plugin version 2 was found, but expressions feature toggle is not enabled")
|
||||
}
|
||||
|
||||
if err := p.registerPlugin(pluginDir); err != nil {
|
||||
return errutil.Wrapf(err, "Failed to register plugin")
|
||||
}
|
||||
@ -56,8 +46,11 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
|
||||
if p.Backend {
|
||||
cmd := ComposePluginStartCommmand(p.Executable)
|
||||
fullpath := path.Join(p.PluginDir, cmd)
|
||||
descriptor := backendplugin.NewBackendPluginDescriptor(p.Id, fullpath)
|
||||
if err := backendplugin.Register(descriptor, p.onPluginStart); err != nil {
|
||||
descriptor := backendplugin.NewBackendPluginDescriptor(p.Id, fullpath, backendplugin.PluginStartFuncs{
|
||||
OnLegacyStart: p.onLegacyPluginStart,
|
||||
OnStart: p.onPluginStart,
|
||||
})
|
||||
if err := backendplugin.Register(descriptor); err != nil {
|
||||
return errutil.Wrapf(err, "Failed to register backend plugin")
|
||||
}
|
||||
}
|
||||
@ -66,41 +59,20 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *DataSourcePlugin) isVersionOne() bool {
|
||||
return !p.SDK
|
||||
}
|
||||
|
||||
func (p *DataSourcePlugin) onPluginStart(pluginID string, client *plugin.Client, logger log.Logger) error {
|
||||
rpcClient, err := client.Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if client.NegotiatedVersion() == 1 {
|
||||
raw, err := rpcClient.Dispense(pluginID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
plugin := raw.(datasourceV1.DatasourcePlugin)
|
||||
|
||||
tsdb.RegisterTsdbQueryEndpoint(pluginID, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||
return wrapper.NewDatasourcePluginWrapper(logger, plugin), nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
raw, err := rpcClient.Dispense("backend")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
plugin, ok := raw.(sdk.BackendPlugin)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T, expected sdk.Plugin", raw)
|
||||
}
|
||||
|
||||
func (p *DataSourcePlugin) onLegacyPluginStart(pluginID string, client *backendplugin.LegacyClient, logger log.Logger) error {
|
||||
tsdb.RegisterTsdbQueryEndpoint(pluginID, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||
return wrapper.NewDatasourcePluginWrapperV2(logger, plugin), nil
|
||||
return wrapper.NewDatasourcePluginWrapper(logger, client.DatasourcePlugin), nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *DataSourcePlugin) onPluginStart(pluginID string, client *backendplugin.Client, logger log.Logger) error {
|
||||
if client.BackendPlugin != nil {
|
||||
tsdb.RegisterTsdbQueryEndpoint(pluginID, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
||||
return wrapper.NewDatasourcePluginWrapperV2(logger, client.BackendPlugin), nil
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user