Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query conversion handler #1078

Merged
merged 9 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions backend/conversion_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,81 @@ package backend

import (
"context"
"encoding/json"
"fmt"

"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
)

type conversionSDKAdapter struct {
handler ConversionHandler
handler ConversionHandler
queryConversionHandler QueryConversionHandler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Would be good to document/note that this is optional (just for the sake of knowing not to skip nil checks if anyone is going to change any code here)

}

func newConversionSDKAdapter(handler ConversionHandler) *conversionSDKAdapter {
func newConversionSDKAdapter(handler ConversionHandler, queryConversionHandler QueryConversionHandler) *conversionSDKAdapter {
return &conversionSDKAdapter{
handler: handler,
handler: handler,
queryConversionHandler: queryConversionHandler,
}
}

func parseAsQueryRequest(req *ConversionRequest) ([]*QueryDataRequest, error) {
var requests []*QueryDataRequest
for _, obj := range req.Objects {
if obj.ContentType != "application/json" {
return nil, fmt.Errorf("unsupported content type %s", obj.ContentType)
}
input := &QueryDataRequest{}
err := json.Unmarshal(obj.Raw, input)
if err != nil {
return nil, fmt.Errorf("unmarshal: %w", err)
}
input.PluginContext = req.PluginContext
requests = append(requests, input)
}
return requests, nil
}

func (a *conversionSDKAdapter) ConvertQueryDataRequest(ctx context.Context, requests []*QueryDataRequest) (*ConversionResponse, error) {
resp := &ConversionResponse{}
convertedRequests := make([]QueryDataRequest, 0, len(requests))
for _, req := range requests {
res, err := a.queryConversionHandler.ConvertQuery(ctx, req)
if err != nil {
return nil, err
}
convertedRequests = append(convertedRequests, *res.QueryRequest)
}

for _, req := range convertedRequests {
newJSON, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("marshal: %w", err)
}
resp.Objects = append(resp.Objects, RawObject{
Raw: newJSON,
ContentType: "application/json",
})
}
return resp, nil
}

func (a *conversionSDKAdapter) ConvertObjects(ctx context.Context, req *pluginv2.ConversionRequest) (*pluginv2.ConversionResponse, error) {
ctx = setupContext(ctx, EndpointConvertObject)
parsedReq := FromProto().ConversionRequest(req)

var resp *ConversionResponse
resp := &ConversionResponse{}
err := wrapHandler(ctx, parsedReq.PluginContext, func(ctx context.Context) (RequestStatus, error) {
var innerErr error
if a.queryConversionHandler != nil {
// Try to parse it as a query data request
reqs, err := parseAsQueryRequest(parsedReq)
if err == nil {
resp, innerErr = a.ConvertQueryDataRequest(ctx, reqs)
return RequestStatusFromError(innerErr), innerErr
}
// The object cannot be parsed as a query data request, so we will try to convert it as a generic object
}
resp, innerErr = a.handler.ConvertObjects(ctx, parsedReq)
return RequestStatusFromError(innerErr), innerErr
})
Expand Down
14 changes: 8 additions & 6 deletions backend/datasource/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpt
}
handler := automanagement.NewManager(NewInstanceManager(instanceFactory))
return backend.Manage(pluginID, backend.ServeOpts{
CheckHealthHandler: handler,
CallResourceHandler: handler,
QueryDataHandler: handler,
StreamHandler: handler,
AdmissionHandler: opts.AdmissionHandler,
GRPCSettings: opts.GRPCSettings,
CheckHealthHandler: handler,
CallResourceHandler: handler,
QueryDataHandler: handler,
StreamHandler: handler,
QueryConversionHandler: handler,
AdmissionHandler: opts.AdmissionHandler,
GRPCSettings: opts.GRPCSettings,
ConversionHandler: opts.ConversionHandler,
})
}
20 changes: 20 additions & 0 deletions backend/query_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package backend

import "context"

// QueryConversionHandler is an EXPERIMENTAL service that allows converting queries between versions

type QueryConversionHandler interface {
// ConvertQuery is called to covert queries between different versions
ConvertQuery(ctx context.Context, req *QueryDataRequest) (*QueryConversionResponse, error)
andresmgot marked this conversation as resolved.
Show resolved Hide resolved
}

type ConvertQueryFunc func(ctx context.Context, req *QueryDataRequest) (*QueryConversionResponse, error)

type QueryConversionResponse struct {
// Converted queries. It should extend v0alpha1.Query
QueryRequest *QueryDataRequest `json:"query"`
andresmgot marked this conversation as resolved.
Show resolved Hide resolved
// Result contains extra details into why an conversion request was denied.
// +optional
Result *StatusResult `json:"result,omitempty"`
andresmgot marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 6 additions & 2 deletions backend/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ type ServeOpts struct {
// This is EXPERIMENTAL and is a subject to change till Grafana 12
ConversionHandler ConversionHandler

// QueryConversionHandler converts queries between versions
// This is EXPERIMENTAL and is a subject to change till Grafana 12
QueryConversionHandler QueryConversionHandler

// GRPCSettings settings for gPRC.
GRPCSettings GRPCSettings
}
Expand All @@ -90,8 +94,8 @@ func GRPCServeOpts(opts ServeOpts) grpcplugin.ServeOpts {
pluginOpts.AdmissionServer = newAdmissionSDKAdapter(opts.AdmissionHandler)
}

if opts.ConversionHandler != nil {
pluginOpts.ConversionServer = newConversionSDKAdapter(opts.ConversionHandler)
if opts.ConversionHandler != nil || opts.QueryConversionHandler != nil {
pluginOpts.ConversionServer = newConversionSDKAdapter(opts.ConversionHandler, opts.QueryConversionHandler)
}
return pluginOpts
}
Expand Down
11 changes: 11 additions & 0 deletions internal/automanagement/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,14 @@ func (m *Manager) RunStream(ctx context.Context, req *backend.RunStreamRequest,
}
return status.Error(codes.Unimplemented, "unimplemented")
}

func (m *Manager) ConvertQuery(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryConversionResponse, error) {
h, err := m.Get(ctx, req.PluginContext)
if err != nil {
return nil, err
}
if ds, ok := h.(backend.QueryConversionHandler); ok {
return ds.ConvertQuery(ctx, req)
}
return nil, status.Error(codes.Unimplemented, "unimplemented")
}