Add error options for rendering to return errors on failure (#48864)

This commit is contained in:
George Robinson 2022-05-09 19:11:24 +01:00 committed by GitHub
parent 7b9929fffe
commit 897db011eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 0 deletions

View File

@ -41,6 +41,7 @@ func getRequestTimeout(opt TimeoutOpts) time.Duration {
type Opts struct {
TimeoutOpts
AuthOpts
ErrorOpts
Width int
Height int
Path string
@ -52,6 +53,15 @@ type Opts struct {
Theme models.Theme
}
type ErrorOpts struct {
// ErrorConcurrentLimitReached returns an ErrConcurrentLimitReached
// error instead of a rendering limit exceeded image.
ErrorConcurrentLimitReached bool
// ErrorRenderUnavailable returns an ErrRunderUnavailable error
// instead of a rendering unavailable image.
ErrorRenderUnavailable bool
}
type CSVOpts struct {
TimeoutOpts
AuthOpts

View File

@ -235,6 +235,9 @@ func (rs *RenderingService) Render(ctx context.Context, opts Opts, session Sessi
func (rs *RenderingService) render(ctx context.Context, opts Opts, renderKeyProvider renderKeyProvider) (*RenderResult, error) {
if int(atomic.LoadInt32(&rs.inProgressCount)) > opts.ConcurrentLimit {
rs.log.Warn("Could not render image, hit the currency limit", "concurrencyLimit", opts.ConcurrentLimit, "path", opts.Path)
if opts.ErrorConcurrentLimitReached {
return nil, ErrConcurrentLimitReached
}
theme := models.ThemeDark
if opts.Theme != "" {
@ -250,6 +253,9 @@ func (rs *RenderingService) render(ctx context.Context, opts Opts, renderKeyProv
rs.log.Warn("Could not render image, no image renderer found/installed. " +
"For image rendering support please install the grafana-image-renderer plugin. " +
"Read more at https://grafana.com/docs/grafana/latest/administration/image_rendering/")
if opts.ErrorRenderUnavailable {
return nil, ErrRenderUnavailable
}
return rs.renderUnavailableImage(), nil
}

View File

@ -11,6 +11,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -102,6 +103,22 @@ func TestRenderErrorImage(t *testing.T) {
})
}
type unavailableRendererManager struct{}
func (m unavailableRendererManager) Renderer() *plugins.Plugin { return nil }
func TestRenderUnavailableError(t *testing.T) {
rs := RenderingService{
Cfg: &setting.Cfg{},
log: log.New("test"),
RendererPluginManager: unavailableRendererManager{},
}
opts := Opts{ErrorOpts: ErrorOpts{ErrorRenderUnavailable: true}}
result, err := rs.Render(context.Background(), opts, nil)
assert.Equal(t, ErrRenderUnavailable, err)
assert.Nil(t, result)
}
func TestRenderLimitImage(t *testing.T) {
path, err := filepath.Abs("../../../")
require.NoError(t, err)
@ -146,6 +163,22 @@ func TestRenderLimitImage(t *testing.T) {
}
}
func TestRenderLimitImageError(t *testing.T) {
rs := RenderingService{
Cfg: &setting.Cfg{},
inProgressCount: 2,
log: log.New("test"),
}
opts := Opts{
ErrorOpts: ErrorOpts{ErrorConcurrentLimitReached: true},
ConcurrentLimit: 1,
Theme: models.ThemeDark,
}
result, err := rs.Render(context.Background(), opts, nil)
assert.Equal(t, ErrConcurrentLimitReached, err)
assert.Nil(t, result)
}
func TestRenderingServiceGetRemotePluginVersion(t *testing.T) {
cfg := setting.NewCfg()
rs := &RenderingService{