mirror of
https://github.com/grafana/grafana.git
synced 2025-09-28 16:34:03 +08:00
Plugins: Use grafana-plugin-sdk-go v0.5.0 (#21116)
* fix dispense * plugin loading refactor Moves common functionality of registering, starting, stopping and restarting backend plugins to backendplugin package. * simplify plugin descriptors * target sdk v0.5.0 Co-authored-by: Kyle Brandt <kyle@kbrandt.com>
This commit is contained in:

committed by
Kyle Brandt

parent
5321e7536f
commit
baba1634b8
@ -3,34 +3,29 @@ package plugins
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"time"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/dataframe"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/transform"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/tsdb"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
type TransformPlugin struct {
|
||||
PluginBase
|
||||
// TODO we probably want a Backend Plugin Base? Or some way to dedup proc management code
|
||||
|
||||
Executable string `json:"executable,omitempty"`
|
||||
|
||||
*TransformWrapper
|
||||
|
||||
client *plugin.Client
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func (tp *TransformPlugin) Load(decoder *json.Decoder, pluginDir string) error {
|
||||
@ -42,108 +37,57 @@ func (tp *TransformPlugin) Load(decoder *json.Decoder, pluginDir string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := ComposePluginStartCommmand(tp.Executable)
|
||||
fullpath := path.Join(tp.PluginDir, cmd)
|
||||
descriptor := backendplugin.NewBackendPluginDescriptor(tp.Id, fullpath)
|
||||
if err := backendplugin.Register(descriptor, tp.onPluginStart); err != nil {
|
||||
return errutil.Wrapf(err, "Failed to register backend plugin")
|
||||
}
|
||||
|
||||
Transform = tp
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *TransformPlugin) startBackendPlugin(ctx context.Context, log log.Logger) error {
|
||||
p.log = log.New("plugin-id", p.Id)
|
||||
|
||||
if err := p.spawnSubProcess(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := p.restartKilledProcess(ctx); err != nil {
|
||||
p.log.Error("Attempting to restart killed process failed", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *TransformPlugin) spawnSubProcess() error {
|
||||
cmd := ComposePluginStartCommmand(p.Executable)
|
||||
fullpath := path.Join(p.PluginDir, cmd)
|
||||
|
||||
p.client = backendplugin.NewTransformClient(p.Id, fullpath, p.log)
|
||||
|
||||
rpcClient, err := p.client.Client()
|
||||
func (p *TransformPlugin) onPluginStart(pluginID string, client *plugin.Client, logger log.Logger) error {
|
||||
rpcClient, err := client.Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
raw, err := rpcClient.Dispense(p.Id)
|
||||
raw, err := rpcClient.Dispense("transform")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
plugin, ok := raw.(transform.TransformPlugin)
|
||||
plugin, ok := raw.(backend.TransformPlugin)
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T, expected *transform.GRPCClient", raw)
|
||||
return fmt.Errorf("unexpected type %T, expected *backend.TransformPlugin", raw)
|
||||
}
|
||||
|
||||
p.TransformWrapper = NewTransformWrapper(p.log, plugin)
|
||||
p.TransformWrapper = NewTransformWrapper(logger, plugin)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *TransformPlugin) restartKilledProcess(ctx context.Context) error {
|
||||
ticker := time.NewTicker(time.Second * 1)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if err := ctx.Err(); err != nil && !xerrors.Is(err, context.Canceled) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if !p.client.Exited() {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := p.spawnSubProcess(); err != nil {
|
||||
p.log.Error("Failed to restart plugin", "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
p.log.Debug("Plugin process restarted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *TransformPlugin) Kill() {
|
||||
if p.client != nil {
|
||||
p.log.Debug("Killing subprocess ", "name", p.Name)
|
||||
p.client.Kill()
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
// Wrapper Code
|
||||
// ...
|
||||
|
||||
func NewTransformWrapper(log log.Logger, plugin transform.TransformPlugin) *TransformWrapper {
|
||||
return &TransformWrapper{plugin, log, &grafanaAPI{log}}
|
||||
func NewTransformWrapper(log log.Logger, plugin backend.TransformPlugin) *TransformWrapper {
|
||||
return &TransformWrapper{plugin, log, &transformCallback{log}}
|
||||
}
|
||||
|
||||
type TransformWrapper struct {
|
||||
transform.TransformPlugin
|
||||
logger log.Logger
|
||||
api *grafanaAPI
|
||||
backend.TransformPlugin
|
||||
logger log.Logger
|
||||
callback *transformCallback
|
||||
}
|
||||
|
||||
func (tw *TransformWrapper) Transform(ctx context.Context, query *tsdb.TsdbQuery) (*tsdb.Response, error) {
|
||||
pbQuery := &pluginv2.TransformRequest{
|
||||
TimeRange: &pluginv2.TimeRange{
|
||||
FromRaw: query.TimeRange.From,
|
||||
ToRaw: query.TimeRange.To,
|
||||
ToEpochMs: query.TimeRange.GetToAsMsEpoch(),
|
||||
FromEpochMs: query.TimeRange.GetFromAsMsEpoch(),
|
||||
},
|
||||
Queries: []*pluginv2.TransformQuery{},
|
||||
pbQuery := &pluginv2.DataQueryRequest{
|
||||
Config: &pluginv2.PluginConfig{},
|
||||
Queries: []*pluginv2.DataQuery{},
|
||||
}
|
||||
|
||||
for _, q := range query.Queries {
|
||||
@ -151,61 +95,44 @@ func (tw *TransformWrapper) Transform(ctx context.Context, query *tsdb.TsdbQuery
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbQuery.Queries = append(pbQuery.Queries, &pluginv2.TransformQuery{
|
||||
ModelJson: string(modelJSON),
|
||||
IntervalMs: q.IntervalMs,
|
||||
pbQuery.Queries = append(pbQuery.Queries, &pluginv2.DataQuery{
|
||||
Json: modelJSON,
|
||||
IntervalMS: q.IntervalMs,
|
||||
RefId: q.RefId,
|
||||
MaxDataPoints: q.MaxDataPoints,
|
||||
TimeRange: &pluginv2.TimeRange{
|
||||
ToEpochMS: query.TimeRange.GetToAsMsEpoch(),
|
||||
FromEpochMS: query.TimeRange.GetFromAsMsEpoch(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pbres, err := tw.TransformPlugin.Transform(ctx, pbQuery, tw.api)
|
||||
|
||||
pbRes, err := tw.TransformPlugin.DataQuery(ctx, pbQuery, tw.callback)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := &tsdb.Response{
|
||||
Results: map[string]*tsdb.QueryResult{},
|
||||
}
|
||||
|
||||
for _, r := range pbres.Results {
|
||||
qr := &tsdb.QueryResult{
|
||||
RefId: r.RefId,
|
||||
}
|
||||
|
||||
if r.Error != "" {
|
||||
qr.Error = errors.New(r.Error)
|
||||
qr.ErrorString = r.Error
|
||||
}
|
||||
|
||||
if r.MetaJson != "" {
|
||||
metaJSON, err := simplejson.NewJson([]byte(r.MetaJson))
|
||||
if err != nil {
|
||||
tw.logger.Error("Error parsing JSON Meta field: " + err.Error())
|
||||
}
|
||||
qr.Meta = metaJSON
|
||||
}
|
||||
qr.Dataframes = r.Dataframes
|
||||
|
||||
res.Results[r.RefId] = qr
|
||||
}
|
||||
|
||||
return res, nil
|
||||
return &tsdb.Response{
|
||||
Results: map[string]*tsdb.QueryResult{
|
||||
"": {
|
||||
Dataframes: pbRes.Frames,
|
||||
Meta: simplejson.NewFromAny(pbRes.Metadata),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
type grafanaAPI struct {
|
||||
type transformCallback struct {
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func (s *grafanaAPI) QueryDatasource(ctx context.Context, req *pluginv2.QueryDatasourceRequest) (*pluginv2.QueryDatasourceResponse, error) {
|
||||
func (s *transformCallback) DataQuery(ctx context.Context, req *pluginv2.DataQueryRequest) (*pluginv2.DataQueryResponse, error) {
|
||||
if len(req.Queries) == 0 {
|
||||
return nil, fmt.Errorf("zero queries found in datasource request")
|
||||
}
|
||||
|
||||
getDsInfo := &models.GetDataSourceByIdQuery{
|
||||
Id: req.DatasourceId,
|
||||
OrgId: req.OrgId,
|
||||
Id: req.Config.Id,
|
||||
OrgId: req.Config.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(getDsInfo); err != nil {
|
||||
@ -215,20 +142,22 @@ func (s *grafanaAPI) QueryDatasource(ctx context.Context, req *pluginv2.QueryDat
|
||||
// Convert plugin-model (datasource) queries to tsdb queries
|
||||
queries := make([]*tsdb.Query, len(req.Queries))
|
||||
for i, query := range req.Queries {
|
||||
sj, err := simplejson.NewJson([]byte(query.ModelJson))
|
||||
sj, err := simplejson.NewJson(query.Json)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
queries[i] = &tsdb.Query{
|
||||
RefId: query.RefId,
|
||||
IntervalMs: query.IntervalMs,
|
||||
IntervalMs: query.IntervalMS,
|
||||
MaxDataPoints: query.MaxDataPoints,
|
||||
DataSource: getDsInfo.Result,
|
||||
Model: sj,
|
||||
}
|
||||
}
|
||||
|
||||
timeRange := tsdb.NewTimeRange(req.TimeRange.FromRaw, req.TimeRange.ToRaw)
|
||||
// For now take Time Range from first query.
|
||||
timeRange := tsdb.NewTimeRange(strconv.FormatInt(req.Queries[0].TimeRange.FromEpochMS, 10), strconv.FormatInt(req.Queries[0].TimeRange.ToEpochMS, 10))
|
||||
|
||||
tQ := &tsdb.TsdbQuery{
|
||||
TimeRange: timeRange,
|
||||
Queries: queries,
|
||||
@ -241,37 +170,33 @@ func (s *grafanaAPI) QueryDatasource(ctx context.Context, req *pluginv2.QueryDat
|
||||
}
|
||||
// Convert tsdb results (map) to plugin-model/datasource (slice) results.
|
||||
// Only error, tsdb.Series, and encoded Dataframes responses are mapped.
|
||||
results := make([]*pluginv2.DatasourceQueryResult, 0, len(tsdbRes.Results))
|
||||
|
||||
encodedFrames := [][]byte{}
|
||||
for refID, res := range tsdbRes.Results {
|
||||
qr := &pluginv2.DatasourceQueryResult{
|
||||
RefId: refID,
|
||||
}
|
||||
|
||||
if res.Error != nil {
|
||||
qr.Error = res.ErrorString
|
||||
results = append(results, qr)
|
||||
// TODO add Errors property to Frame
|
||||
encodedFrames = append(encodedFrames, nil)
|
||||
continue
|
||||
}
|
||||
|
||||
if res.Dataframes != nil {
|
||||
qr.Dataframes = append(qr.Dataframes, res.Dataframes...)
|
||||
results = append(results, qr)
|
||||
encodedFrames = append(encodedFrames, res.Dataframes...)
|
||||
continue
|
||||
}
|
||||
|
||||
encodedFrames := make([][]byte, len(res.Series))
|
||||
for sIdx, series := range res.Series {
|
||||
for _, series := range res.Series {
|
||||
frame, err := tsdb.SeriesToFrame(series)
|
||||
frame.RefID = refID
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encodedFrames[sIdx], err = dataframe.MarshalArrow(frame)
|
||||
encFrame, err := dataframe.MarshalArrow(frame)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encodedFrames = append(encodedFrames, encFrame)
|
||||
}
|
||||
qr.Dataframes = encodedFrames
|
||||
results = append(results, qr)
|
||||
}
|
||||
return &pluginv2.QueryDatasourceResponse{Results: results}, nil
|
||||
return &pluginv2.DataQueryResponse{Frames: encodedFrames}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user