2018-05-24 15:26:27 +02:00
|
|
|
package rendering
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-11-19 14:47:17 +01:00
|
|
|
"errors"
|
2018-05-24 15:26:27 +02:00
|
|
|
"fmt"
|
2020-01-17 12:07:16 +01:00
|
|
|
"time"
|
2018-05-24 15:26:27 +02:00
|
|
|
|
|
|
|
|
pluginModel "github.com/grafana/grafana-plugin-model/go/renderer"
|
2020-04-21 16:16:41 +02:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2018-05-24 15:26:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (rs *RenderingService) startPlugin(ctx context.Context) error {
|
2020-01-08 17:43:28 +01:00
|
|
|
return rs.pluginInfo.Start(ctx)
|
2018-05-24 15:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 19:47:39 +01:00
|
|
|
func (rs *RenderingService) renderViaPlugin(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
|
2020-04-21 16:16:41 +02:00
|
|
|
// gives plugin some additional time to timeout and return possible errors.
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
if rs.pluginInfo.GrpcPluginV2 != nil {
|
|
|
|
|
return rs.renderViaPluginV2(ctx, renderKey, opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rs.renderViaPluginV1(ctx, renderKey, opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rs *RenderingService) renderViaPluginV1(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
|
2019-10-23 10:40:12 +02:00
|
|
|
pngPath, err := rs.getFilePathForNewImage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 15:06:59 +01:00
|
|
|
req := &pluginModel.RenderRequest{
|
2018-05-24 15:26:27 +02:00
|
|
|
Url: rs.getURL(opts.Path),
|
|
|
|
|
Width: int32(opts.Width),
|
|
|
|
|
Height: int32(opts.Height),
|
|
|
|
|
FilePath: pngPath,
|
|
|
|
|
Timeout: int32(opts.Timeout.Seconds()),
|
2019-10-23 10:40:12 +02:00
|
|
|
RenderKey: renderKey,
|
2018-05-24 15:26:27 +02:00
|
|
|
Encoding: opts.Encoding,
|
|
|
|
|
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
2018-09-04 19:42:55 +08:00
|
|
|
Domain: rs.domain,
|
2019-12-11 15:06:59 +01:00
|
|
|
}
|
|
|
|
|
rs.log.Debug("calling renderer plugin", "req", req)
|
|
|
|
|
|
2020-04-21 16:16:41 +02:00
|
|
|
rsp, err := rs.pluginInfo.GrpcPluginV1.Render(ctx, req)
|
2020-11-19 14:47:17 +01:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2020-04-27 15:29:46 +03:00
|
|
|
rs.log.Info("Rendering timed out")
|
|
|
|
|
return nil, ErrTimeout
|
|
|
|
|
}
|
2020-04-21 16:16:41 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if rsp.Error != "" {
|
|
|
|
|
return nil, fmt.Errorf("rendering failed: %v", rsp.Error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &RenderResult{FilePath: pngPath}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rs *RenderingService) renderViaPluginV2(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
|
|
|
|
|
pngPath, err := rs.getFilePathForNewImage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headers := map[string]*pluginextensionv2.StringList{}
|
|
|
|
|
|
|
|
|
|
for k, values := range opts.Headers {
|
|
|
|
|
headers[k] = &pluginextensionv2.StringList{
|
|
|
|
|
Values: values,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := &pluginextensionv2.RenderRequest{
|
|
|
|
|
Url: rs.getURL(opts.Path),
|
|
|
|
|
Width: int32(opts.Width),
|
|
|
|
|
Height: int32(opts.Height),
|
|
|
|
|
DeviceScaleFactor: float32(opts.DeviceScaleFactor),
|
|
|
|
|
FilePath: pngPath,
|
|
|
|
|
Timeout: int32(opts.Timeout.Seconds()),
|
|
|
|
|
RenderKey: renderKey,
|
|
|
|
|
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
|
|
|
|
Domain: rs.domain,
|
|
|
|
|
Headers: headers,
|
|
|
|
|
}
|
|
|
|
|
rs.log.Debug("Calling renderer plugin", "req", req)
|
|
|
|
|
|
|
|
|
|
rsp, err := rs.pluginInfo.GrpcPluginV2.Render(ctx, req)
|
2020-11-19 14:47:17 +01:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2020-04-27 15:29:46 +03:00
|
|
|
rs.log.Info("Rendering timed out")
|
|
|
|
|
return nil, ErrTimeout
|
|
|
|
|
}
|
2018-05-24 15:26:27 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if rsp.Error != "" {
|
2020-11-05 11:57:20 +01:00
|
|
|
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
|
2018-05-24 15:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &RenderResult{FilePath: pngPath}, err
|
|
|
|
|
}
|