forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin_mode.go
113 lines (95 loc) · 3 KB
/
plugin_mode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package rendering
import (
"context"
"errors"
"fmt"
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
)
func (rs *RenderingService) renderViaPlugin(ctx context.Context, renderType RenderType, 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()
filePath, err := rs.getNewFilePath(renderType)
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.getGrafanaCallbackURL(opts.Path),
Width: int32(opts.Width),
Height: int32(opts.Height),
DeviceScaleFactor: float32(opts.DeviceScaleFactor),
FilePath: filePath,
Timeout: int32(opts.Timeout.Seconds()),
RenderKey: renderKey,
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
Domain: rs.domain,
Headers: headers,
AuthToken: rs.Cfg.RendererAuthToken,
Encoding: string(renderType),
}
rs.log.Debug("Calling renderer plugin", "req", req)
rc, err := rs.plugin.Client()
if err != nil {
return nil, err
}
rsp, err := rc.Render(ctx, req)
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
rs.log.Info("Rendering timed out")
return nil, ErrTimeout
}
if err != nil {
return nil, err
}
if rsp.Error != "" {
return nil, fmt.Errorf("rendering failed: %s", rsp.Error)
}
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))
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.getGrafanaCallbackURL(opts.Path),
FilePath: filePath,
RenderKey: renderKey,
Domain: rs.domain,
Timeout: int32(opts.Timeout.Seconds()),
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
Headers: headers,
AuthToken: rs.Cfg.RendererAuthToken,
}
rs.log.Debug("Calling renderer plugin", "req", req)
rc, err := rs.plugin.Client()
if err != nil {
return nil, err
}
rsp, err := rc.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
}