mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 14:52:16 +08:00
Rendering: add CSV support (#33729)
* Rendering: add CSV rendering support * Rendering: save csv files into a separate folder * add missing field * Renderer: get filename from renderer plugin * apply PR suggestions * Rendering: remove old PhantomJS error * Rendering: separate RenderCSV and Render functions * fix alerting test * Rendering: fix handling error in HTTP mode * apply PR feedback * Update pkg/services/rendering/http_mode.go Co-authored-by: Joan López de la Franca Beltran <joanjan14@gmail.com> * apply PR feedback * Update rendering metrics with type label * Rendering: return error if not able to parse header * Rendering: update grpc generated file * Rendering: use context.WithTimeout to render CSV too Co-authored-by: Joan López de la Franca Beltran <joanjan14@gmail.com>
This commit is contained in:
@ -27,7 +27,7 @@ func (rs *RenderingService) renderViaPlugin(ctx context.Context, renderKey strin
|
||||
}
|
||||
|
||||
func (rs *RenderingService) renderViaPluginV1(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
|
||||
pngPath, err := rs.getFilePathForNewImage()
|
||||
filePath, err := rs.getNewFilePath(RenderPNG)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -36,7 +36,7 @@ func (rs *RenderingService) renderViaPluginV1(ctx context.Context, renderKey str
|
||||
Url: rs.getURL(opts.Path),
|
||||
Width: int32(opts.Width),
|
||||
Height: int32(opts.Height),
|
||||
FilePath: pngPath,
|
||||
FilePath: filePath,
|
||||
Timeout: int32(opts.Timeout.Seconds()),
|
||||
RenderKey: renderKey,
|
||||
Encoding: opts.Encoding,
|
||||
@ -57,11 +57,11 @@ func (rs *RenderingService) renderViaPluginV1(ctx context.Context, renderKey str
|
||||
return nil, fmt.Errorf("rendering failed: %v", rsp.Error)
|
||||
}
|
||||
|
||||
return &RenderResult{FilePath: pngPath}, nil
|
||||
return &RenderResult{FilePath: filePath}, nil
|
||||
}
|
||||
|
||||
func (rs *RenderingService) renderViaPluginV2(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
|
||||
pngPath, err := rs.getFilePathForNewImage()
|
||||
filePath, err := rs.getNewFilePath(RenderPNG)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -79,7 +79,7 @@ func (rs *RenderingService) renderViaPluginV2(ctx context.Context, renderKey str
|
||||
Width: int32(opts.Width),
|
||||
Height: int32(opts.Height),
|
||||
DeviceScaleFactor: float32(opts.DeviceScaleFactor),
|
||||
FilePath: pngPath,
|
||||
FilePath: filePath,
|
||||
Timeout: int32(opts.Timeout.Seconds()),
|
||||
RenderKey: renderKey,
|
||||
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
|
||||
@ -100,5 +100,50 @@ func (rs *RenderingService) renderViaPluginV2(ctx context.Context, renderKey str
|
||||
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
|
||||
}
|
||||
|
||||
return &RenderResult{FilePath: pngPath}, err
|
||||
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, opts.Timeout+time.Second*2)
|
||||
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,
|
||||
}
|
||||
rs.log.Debug("Calling renderer plugin", "req", req)
|
||||
|
||||
rsp, err := rs.pluginInfo.GrpcPluginV2.RenderCSV(ctx, req)
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user