2016-01-09 16:34:20 -06:00
|
|
|
package plugins
|
|
|
|
|
2017-08-31 07:05:52 -05:00
|
|
|
import (
|
2021-03-08 00:02:49 -06:00
|
|
|
"context"
|
2017-08-31 07:05:52 -05:00
|
|
|
"encoding/json"
|
2021-03-08 00:02:49 -06:00
|
|
|
"fmt"
|
2020-10-19 10:35:31 -05:00
|
|
|
"path/filepath"
|
2017-12-15 10:17:58 -06:00
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2017-12-15 10:17:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-06-11 09:14:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/grpcplugin"
|
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2017-08-31 07:05:52 -05:00
|
|
|
)
|
2016-01-09 16:34:20 -06:00
|
|
|
|
2018-07-20 10:07:17 -05:00
|
|
|
// DataSourcePlugin contains all metadata about a datasource plugin
|
2016-01-09 16:34:20 -06:00
|
|
|
type DataSourcePlugin struct {
|
|
|
|
FrontendPluginBase
|
2019-10-30 11:51:13 -05:00
|
|
|
Annotations bool `json:"annotations"`
|
|
|
|
Metrics bool `json:"metrics"`
|
|
|
|
Alerting bool `json:"alerting"`
|
|
|
|
Explore bool `json:"explore"`
|
|
|
|
Table bool `json:"tables"`
|
|
|
|
Logs bool `json:"logs"`
|
2020-03-25 06:25:39 -05:00
|
|
|
Tracing bool `json:"tracing"`
|
2019-10-30 11:51:13 -05:00
|
|
|
QueryOptions map[string]bool `json:"queryOptions,omitempty"`
|
|
|
|
BuiltIn bool `json:"builtIn,omitempty"`
|
|
|
|
Mixed bool `json:"mixed,omitempty"`
|
|
|
|
Routes []*AppPluginRoute `json:"routes"`
|
|
|
|
Streaming bool `json:"streaming"`
|
2017-12-15 10:17:58 -06:00
|
|
|
|
|
|
|
Backend bool `json:"backend,omitempty"`
|
|
|
|
Executable string `json:"executable,omitempty"`
|
2019-10-24 10:15:27 -05:00
|
|
|
SDK bool `json:"sdk,omitempty"`
|
2021-03-08 00:02:49 -06:00
|
|
|
|
2021-05-19 01:57:48 -05:00
|
|
|
client *grpcplugin.Client
|
|
|
|
logger log.Logger
|
2016-01-09 16:34:20 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func (p *DataSourcePlugin) Load(decoder *json.Decoder, base *PluginBase, backendPluginManager backendplugin.Manager) (
|
|
|
|
interface{}, error) {
|
2019-10-28 11:25:35 -05:00
|
|
|
if err := decoder.Decode(p); err != nil {
|
2021-03-08 00:02:49 -06:00
|
|
|
return nil, errutil.Wrapf(err, "Failed to decode datasource plugin")
|
2016-02-10 04:03:12 -06:00
|
|
|
}
|
2016-01-09 16:34:20 -06:00
|
|
|
|
2020-01-08 10:43:28 -06:00
|
|
|
if p.Backend {
|
2020-06-01 10:11:25 -05:00
|
|
|
cmd := ComposePluginStartCommand(p.Executable)
|
2021-03-08 00:02:49 -06:00
|
|
|
fullpath := filepath.Join(base.PluginDir, cmd)
|
2020-06-11 09:14:05 -05:00
|
|
|
factory := grpcplugin.NewBackendPlugin(p.Id, fullpath, grpcplugin.PluginStartFuncs{
|
2021-05-19 01:57:48 -05:00
|
|
|
OnStart: p.onPluginStart,
|
2020-01-13 10:13:17 -06:00
|
|
|
})
|
2021-05-12 13:05:16 -05:00
|
|
|
if err := backendPluginManager.RegisterAndStart(context.Background(), p.Id, factory); err != nil {
|
2021-03-08 00:02:49 -06:00
|
|
|
return nil, errutil.Wrapf(err, "failed to register backend plugin")
|
2019-10-11 14:02:15 -05:00
|
|
|
}
|
2020-01-08 10:43:28 -06:00
|
|
|
}
|
2019-10-11 14:02:15 -05:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DataSourcePlugin) DataQuery(ctx context.Context, dsInfo *models.DataSource, query DataQuery) (DataResponse, error) {
|
|
|
|
if !p.CanHandleDataQueries() {
|
|
|
|
return DataResponse{}, fmt.Errorf("plugin %q can't handle data queries", p.Id)
|
|
|
|
}
|
|
|
|
|
2021-05-19 01:57:48 -05:00
|
|
|
endpoint := newDataSourcePluginWrapperV2(p.logger, p.Id, p.Type, p.client.DataPlugin)
|
2021-03-08 00:02:49 -06:00
|
|
|
return endpoint.Query(ctx, dsInfo, query)
|
2017-12-22 08:40:45 -06:00
|
|
|
}
|
2019-10-29 11:22:31 -05:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func (p *DataSourcePlugin) CanHandleDataQueries() bool {
|
2021-05-19 01:57:48 -05:00
|
|
|
return p.client != nil
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:14:05 -05:00
|
|
|
func (p *DataSourcePlugin) onPluginStart(pluginID string, client *grpcplugin.Client, logger log.Logger) error {
|
2021-03-08 00:02:49 -06:00
|
|
|
if client.DataPlugin == nil {
|
|
|
|
return nil
|
2020-01-08 10:43:28 -06:00
|
|
|
}
|
2017-12-15 10:17:58 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
p.client = client
|
|
|
|
p.logger = logger
|
|
|
|
|
2017-12-15 10:17:58 -06:00
|
|
|
return nil
|
|
|
|
}
|