Backend plugins: Implement support for resources (#21805)
Implements initial support for resources using v0.14.0 of SDK. Ref #21512
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
@@ -14,7 +15,6 @@ import (
|
||||
|
||||
datasourceV1 "github.com/grafana/grafana-plugin-model/go/datasource"
|
||||
rendererV1 "github.com/grafana/grafana-plugin-model/go/renderer"
|
||||
backend "github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/collector"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
@@ -31,7 +31,8 @@ type BackendPlugin struct {
|
||||
client *plugin.Client
|
||||
logger log.Logger
|
||||
startFns PluginStartFuncs
|
||||
diagnostics backend.DiagnosticsPlugin
|
||||
diagnostics DiagnosticsPlugin
|
||||
core CorePlugin
|
||||
}
|
||||
|
||||
func (p *BackendPlugin) start(ctx context.Context) error {
|
||||
@@ -61,20 +62,21 @@ func (p *BackendPlugin) start(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if rawDiagnostics != nil {
|
||||
if plugin, ok := rawDiagnostics.(backend.DiagnosticsPlugin); ok {
|
||||
if plugin, ok := rawDiagnostics.(DiagnosticsPlugin); ok {
|
||||
p.diagnostics = plugin
|
||||
}
|
||||
}
|
||||
|
||||
client = &Client{}
|
||||
if rawBackend != nil {
|
||||
if plugin, ok := rawBackend.(backend.BackendPlugin); ok {
|
||||
client.BackendPlugin = plugin
|
||||
if plugin, ok := rawBackend.(CorePlugin); ok {
|
||||
p.core = plugin
|
||||
client.DatasourcePlugin = plugin
|
||||
}
|
||||
}
|
||||
|
||||
if rawTransform != nil {
|
||||
if plugin, ok := rawTransform.(backend.TransformPlugin); ok {
|
||||
if plugin, ok := rawTransform.(TransformPlugin); ok {
|
||||
client.TransformPlugin = plugin
|
||||
}
|
||||
}
|
||||
@@ -194,6 +196,47 @@ func (p *BackendPlugin) checkHealth(ctx context.Context) (*pluginv2.CheckHealth_
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (p *BackendPlugin) callResource(ctx context.Context, req CallResourceRequest) (*CallResourceResult, error) {
|
||||
p.logger.Debug("Calling resource", "path", req.Path, "method", req.Method)
|
||||
|
||||
reqHeaders := map[string]*pluginv2.CallResource_StringList{}
|
||||
for k, v := range req.Headers {
|
||||
reqHeaders[k] = &pluginv2.CallResource_StringList{Values: v}
|
||||
}
|
||||
|
||||
protoReq := &pluginv2.CallResource_Request{
|
||||
Config: &pluginv2.PluginConfig{},
|
||||
Path: req.Path,
|
||||
Method: req.Method,
|
||||
Url: req.URL,
|
||||
Headers: reqHeaders,
|
||||
Body: req.Body,
|
||||
}
|
||||
protoResp, err := p.core.CallResource(ctx, protoReq)
|
||||
if err != nil {
|
||||
if st, ok := status.FromError(err); ok {
|
||||
if st.Code() == codes.Unimplemented {
|
||||
return &CallResourceResult{
|
||||
Status: http.StatusNotImplemented,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errutil.Wrap("Failed to call resource", err)
|
||||
}
|
||||
|
||||
respHeaders := map[string][]string{}
|
||||
for key, values := range protoResp.Headers {
|
||||
respHeaders[key] = values.Values
|
||||
}
|
||||
|
||||
return &CallResourceResult{
|
||||
Headers: respHeaders,
|
||||
Body: protoResp.Body,
|
||||
Status: int(protoResp.Code),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// convertMetricFamily converts metric family to prometheus.Metric.
|
||||
// Copied from https://github.com/prometheus/node_exporter/blob/3ddc82c2d8d11eec53ed5faa8db969a1bb81f8bb/collector/textfile.go#L66-L165
|
||||
func convertMetricFamily(pluginID string, metricFamily *dto.MetricFamily, ch chan<- prometheus.Metric, logger log.Logger) {
|
||||
|
||||
Reference in New Issue
Block a user