The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/plugins/backend_datasource.go

85 lines
2.0 KiB

package plugins
import (
"context"
"encoding/json"
"os/exec"
"path"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins/backend"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/log"
proto "github.com/grafana/grafana/pkg/tsdb/models"
shared "github.com/grafana/grafana/pkg/tsdb/models/proxy"
plugin "github.com/hashicorp/go-plugin"
)
type BackendDatasource struct {
*PluginBase
Executable string
log log.Logger
client *plugin.Client
}
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "GRAFANA_BACKEND_DATASOURCE",
MagicCookieValue: "55d2200a-6492-493a-9353-73b728d468aa",
}
func (p *BackendDatasource) initBackendPlugin(log log.Logger) error {
p.log = log.New("plugin-id", p.Id)
p.client = plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: handshakeConfig,
Plugins: map[string]plugin.Plugin{p.Id: &shared.TsdbPluginImpl{}},
Cmd: exec.Command(path.Join(p.PluginDir, p.Executable)),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
Logger: backend.LogWrapper{Logger: p.log},
})
rpcClient, err := p.client.Client()
if err != nil {
return err
}
raw, err := rpcClient.Dispense(p.Id)
if err != nil {
return err
}
plugin := raw.(shared.TsdbPlugin)
response, err := plugin.Query(context.Background(), &proto.TsdbQuery{})
if err != nil {
p.log.Error("Response from plugin. ", "response", response)
} else {
p.log.Info("Response from plugin. ", "response", response)
}
tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
return &shared.TsdbWrapper{TsdbPlugin: plugin}, nil
})
return nil
}
func (p *BackendDatasource) Kill() {
p.client.Kill()
}
func (p *BackendDatasource) Load(decoder *json.Decoder, pluginDir string) error {
if err := decoder.Decode(&p); err != nil {
return err
}
if err := p.registerPlugin(pluginDir); err != nil {
return err
}
BackendDatasources[p.Id] = p
return nil
}