Files
grafana/pkg/services/rendering/plugin_mode.go
T

108 lines
2.9 KiB
Go
Raw Normal View History

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"
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
2018-05-24 15:26:27 +02:00
)
func (rs *RenderingService) startPlugin(ctx context.Context) error {
return rs.pluginInfo.Start(ctx)
2018-05-24 15:26:27 +02:00
}
func (rs *RenderingService) renderViaPlugin(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
// gives plugin some additional time to timeout and return possible errors.
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
defer cancel()
2021-05-12 17:16:57 +02:00
filePath, err := rs.getNewFilePath(RenderPNG)
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),
2021-05-12 17:16:57 +02:00
FilePath: filePath,
Timeout: int32(opts.Timeout.Seconds()),
RenderKey: renderKey,
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
Domain: rs.domain,
Headers: headers,
AuthToken: rs.Cfg.RendererAuthToken,
}
rs.log.Debug("Calling renderer plugin", "req", req)
rsp, err := rs.pluginInfo.Renderer.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
}
2021-05-12 17:16:57 +02:00
return &RenderResult{FilePath: filePath}, err
}
func (rs *RenderingService) renderCSVViaPlugin(ctx context.Context, renderKey string, opts CSVOpts) (*RenderCSVResult, error) {
// gives plugin some additional time to timeout and return possible errors.
ctx, cancel := context.WithTimeout(ctx, getRequestTimeout(opts.TimeoutOpts))
2021-05-12 17:16:57 +02:00
defer cancel()
filePath, err := rs.getNewFilePath(RenderCSV)
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.RenderCSVRequest{
Url: rs.getURL(opts.Path),
FilePath: filePath,
RenderKey: renderKey,
Domain: rs.domain,
Timeout: int32(opts.Timeout.Seconds()),
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
Headers: headers,
AuthToken: rs.Cfg.RendererAuthToken,
2021-05-12 17:16:57 +02:00
}
rs.log.Debug("Calling renderer plugin", "req", req)
rsp, err := rs.pluginInfo.Renderer.RenderCSV(ctx, req)
2021-05-12 17:16:57 +02:00
if err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
rs.log.Info("Rendering timed out")
return nil, ErrTimeout
}
return nil, err
}
if rsp.Error != "" {
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
}
return &RenderCSVResult{FilePath: filePath, FileName: rsp.FileName}, nil
2018-05-24 15:26:27 +02:00
}