2018-05-24 08:26:27 -05:00
|
|
|
package rendering
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-04 06:33:49 -05:00
|
|
|
"encoding/json"
|
2020-11-19 07:47:17 -06:00
|
|
|
"errors"
|
2018-09-04 06:42:55 -05:00
|
|
|
"fmt"
|
2018-05-24 08:26:27 -05:00
|
|
|
"io"
|
2021-05-12 10:16:57 -05:00
|
|
|
"io/fs"
|
|
|
|
"mime"
|
2018-05-24 08:26:27 -05:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var netTransport = &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
2019-05-08 03:37:48 -05:00
|
|
|
Timeout: 30 * time.Second,
|
2018-05-24 08:26:27 -05:00
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
2018-09-04 06:42:55 -05:00
|
|
|
var netClient = &http.Client{
|
|
|
|
Transport: netTransport,
|
|
|
|
}
|
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
func (rs *RenderingService) renderViaHTTP(ctx context.Context, renderKey string, opts Opts) (*RenderResult, error) {
|
|
|
|
filePath, err := rs.getNewFilePath(RenderPNG)
|
2019-10-23 03:40:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-24 08:26:27 -05:00
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
rendererURL, err := url.Parse(rs.Cfg.RendererUrl)
|
2018-05-24 08:26:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
queryParams := rendererURL.Query()
|
2018-05-24 08:26:27 -05:00
|
|
|
queryParams.Add("url", rs.getURL(opts.Path))
|
2019-10-23 03:40:12 -05:00
|
|
|
queryParams.Add("renderKey", renderKey)
|
2018-05-24 08:26:27 -05:00
|
|
|
queryParams.Add("width", strconv.Itoa(opts.Width))
|
|
|
|
queryParams.Add("height", strconv.Itoa(opts.Height))
|
2018-09-04 06:42:55 -05:00
|
|
|
queryParams.Add("domain", rs.domain)
|
2018-05-24 08:26:27 -05:00
|
|
|
queryParams.Add("timezone", isoTimeOffsetToPosixTz(opts.Timezone))
|
|
|
|
queryParams.Add("encoding", opts.Encoding)
|
|
|
|
queryParams.Add("timeout", strconv.Itoa(int(opts.Timeout.Seconds())))
|
2020-04-21 09:16:41 -05:00
|
|
|
queryParams.Add("deviceScaleFactor", fmt.Sprintf("%f", opts.DeviceScaleFactor))
|
2018-05-24 08:26:27 -05:00
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
rendererURL.RawQuery = queryParams.Encode()
|
|
|
|
|
|
|
|
// gives service some additional time to timeout and return possible errors.
|
|
|
|
reqContext, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp, err := rs.doRequest(reqContext, rendererURL, opts.Headers)
|
2018-05-24 08:26:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
// save response to file
|
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
rs.log.Warn("Failed to close response body", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-12-09 02:11:40 -06:00
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
err = rs.readFileResponse(reqContext, resp, filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-21 09:16:41 -05:00
|
|
|
}
|
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
return &RenderResult{FilePath: filePath}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RenderingService) renderCSVViaHTTP(ctx context.Context, renderKey string, opts CSVOpts) (*RenderCSVResult, error) {
|
|
|
|
filePath, err := rs.getNewFilePath(RenderCSV)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rendererURL, err := url.Parse(rs.Cfg.RendererUrl + "/csv")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
queryParams := rendererURL.Query()
|
|
|
|
queryParams.Add("url", rs.getURL(opts.Path))
|
|
|
|
queryParams.Add("renderKey", renderKey)
|
|
|
|
queryParams.Add("domain", rs.domain)
|
|
|
|
queryParams.Add("timezone", isoTimeOffsetToPosixTz(opts.Timezone))
|
|
|
|
queryParams.Add("encoding", opts.Encoding)
|
|
|
|
queryParams.Add("timeout", strconv.Itoa(int(opts.Timeout.Seconds())))
|
|
|
|
|
|
|
|
rendererURL.RawQuery = queryParams.Encode()
|
|
|
|
|
2020-01-17 05:07:16 -06:00
|
|
|
// gives service some additional time to timeout and return possible errors.
|
2018-09-04 06:42:55 -05:00
|
|
|
reqContext, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
resp, err := rs.doRequest(reqContext, rendererURL, opts.Headers)
|
2018-05-24 08:26:27 -05:00
|
|
|
if err != nil {
|
2021-05-12 10:16:57 -05:00
|
|
|
return nil, err
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// save response to file
|
2020-12-15 02:32:06 -06:00
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
rs.log.Warn("Failed to close response body", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
2018-09-04 06:42:55 -05:00
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
_, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
downloadFileName := params["filename"]
|
|
|
|
|
|
|
|
err = rs.readFileResponse(reqContext, resp, filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RenderCSVResult{FilePath: filePath, FileName: downloadFileName}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RenderingService) doRequest(ctx context.Context, url *url.URL, headers map[string][]string) (*http.Response, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("User-Agent", fmt.Sprintf("Grafana/%s", rs.Cfg.BuildVersion))
|
|
|
|
for k, v := range headers {
|
|
|
|
req.Header[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
rs.log.Debug("calling remote rendering service", "url", url)
|
|
|
|
|
|
|
|
// make request to renderer server
|
|
|
|
resp, err := netClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
rs.log.Error("Failed to send request to remote rendering service", "error", err)
|
|
|
|
return nil, fmt.Errorf("failed to send request to remote rendering service: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RenderingService) readFileResponse(ctx context.Context, resp *http.Response, filePath string) error {
|
2018-09-04 06:42:55 -05:00
|
|
|
// check for timeout first
|
2021-05-12 10:16:57 -05:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2018-09-04 06:42:55 -05:00
|
|
|
rs.log.Info("Rendering timed out")
|
2021-05-12 10:16:57 -05:00
|
|
|
return ErrTimeout
|
2018-09-04 06:42:55 -05:00
|
|
|
}
|
|
|
|
|
2018-09-21 04:51:26 -05:00
|
|
|
// if we didn't get a 200 response, something went wrong.
|
2018-09-04 06:42:55 -05:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
rs.log.Error("Remote rendering request failed", "error", resp.Status)
|
2021-05-12 10:16:57 -05:00
|
|
|
return fmt.Errorf("remote rendering request failed, status code: %d, status: %s", resp.StatusCode,
|
2020-11-05 04:57:20 -06:00
|
|
|
resp.Status)
|
2018-09-04 06:42:55 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 08:26:27 -05:00
|
|
|
out, err := os.Create(filePath)
|
|
|
|
if err != nil {
|
2021-05-12 10:16:57 -05:00
|
|
|
return err
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
2021-05-12 10:16:57 -05:00
|
|
|
|
2020-12-03 03:11:14 -06:00
|
|
|
defer func() {
|
2021-05-12 10:16:57 -05:00
|
|
|
if err := out.Close(); err != nil && !errors.Is(err, fs.ErrClosed) {
|
2020-12-03 03:11:14 -06:00
|
|
|
// We already close the file explicitly in the non-error path, so shouldn't be a problem
|
|
|
|
rs.log.Warn("Failed to close file", "path", filePath, "err", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-05-12 10:16:57 -05:00
|
|
|
|
2018-09-04 06:42:55 -05:00
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
if err != nil {
|
2018-09-21 04:51:26 -05:00
|
|
|
// check that we didn't timeout while receiving the response.
|
2021-05-12 10:16:57 -05:00
|
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
2018-09-04 06:42:55 -05:00
|
|
|
rs.log.Info("Rendering timed out")
|
2021-05-12 10:16:57 -05:00
|
|
|
return ErrTimeout
|
2018-09-04 06:42:55 -05:00
|
|
|
}
|
2021-05-12 10:16:57 -05:00
|
|
|
|
2018-09-04 06:42:55 -05:00
|
|
|
rs.log.Error("Remote rendering request failed", "error", err)
|
2021-05-12 10:16:57 -05:00
|
|
|
return fmt.Errorf("remote rendering request failed: %w", err)
|
2018-09-04 06:42:55 -05:00
|
|
|
}
|
2020-12-03 03:11:14 -06:00
|
|
|
if err := out.Close(); err != nil {
|
2021-05-12 10:16:57 -05:00
|
|
|
return fmt.Errorf("failed to write to %q: %w", filePath, err)
|
2020-12-03 03:11:14 -06:00
|
|
|
}
|
2018-05-24 08:26:27 -05:00
|
|
|
|
2021-05-12 10:16:57 -05:00
|
|
|
return nil
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
2021-06-04 06:33:49 -05:00
|
|
|
|
|
|
|
func (rs *RenderingService) getRemotePluginVersion() (string, error) {
|
|
|
|
rendererURL, err := url.Parse(rs.Cfg.RendererUrl + "/version")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
headers := make(map[string][]string)
|
|
|
|
resp, err := rs.doRequest(context.Background(), rendererURL, headers)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
rs.log.Warn("Failed to close response body", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("remote rendering request to get version failed, status code: %d, status: %s", resp.StatusCode,
|
|
|
|
resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
var info struct {
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return info.Version, nil
|
|
|
|
}
|