2022-05-22 09:33:49 -05:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-08 16:37:49 -06:00
|
|
|
"encoding/base64"
|
2022-05-22 09:33:49 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-09-21 04:25:07 -05:00
|
|
|
"golang.org/x/sync/singleflight"
|
2022-05-22 09:33:49 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/imguploader"
|
2022-09-21 04:25:07 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-05-22 09:33:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-09-21 04:25:07 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2022-05-22 09:33:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
|
|
|
"github.com/grafana/grafana/pkg/services/screenshot"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2022-06-29 14:30:13 -05:00
|
|
|
const (
|
2022-11-22 01:09:15 -06:00
|
|
|
screenshotCacheTTL = time.Minute
|
2022-06-29 14:30:13 -05:00
|
|
|
)
|
2022-05-22 09:33:49 -05:00
|
|
|
|
2022-08-09 09:28:36 -05:00
|
|
|
// DeleteExpiredService is a service to delete expired images.
|
|
|
|
type DeleteExpiredService struct {
|
|
|
|
store store.ImageAdminStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DeleteExpiredService) DeleteExpired(ctx context.Context) (int64, error) {
|
|
|
|
return s.store.DeleteExpiredImages(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProvideDeleteExpiredService(store *store.DBstore) *DeleteExpiredService {
|
|
|
|
return &DeleteExpiredService{store: store}
|
|
|
|
}
|
|
|
|
|
2022-06-29 14:30:13 -05:00
|
|
|
type ImageService interface {
|
|
|
|
// NewImage returns a new image for the alert instance.
|
2022-09-21 04:25:07 -05:00
|
|
|
NewImage(ctx context.Context, r *models.AlertRule) (*models.Image, error)
|
2022-06-29 14:30:13 -05:00
|
|
|
}
|
2022-05-22 09:33:49 -05:00
|
|
|
|
2022-06-29 14:30:13 -05:00
|
|
|
// ScreenshotImageService takes screenshots of the alert rule and saves the
|
|
|
|
// image in the store. The image contains a unique token that can be passed
|
|
|
|
// as an annotation or label to the Alertmanager. This service cannot take
|
|
|
|
// screenshots of alert rules that are not associated with a dashboard panel.
|
2022-05-22 09:33:49 -05:00
|
|
|
type ScreenshotImageService struct {
|
2023-01-05 10:07:46 -06:00
|
|
|
cache CacheService
|
|
|
|
limiter screenshot.RateLimiter
|
|
|
|
logger log.Logger
|
|
|
|
screenshots screenshot.ScreenshotService
|
|
|
|
screenshotTimeout time.Duration
|
|
|
|
singleflight singleflight.Group
|
|
|
|
store store.ImageStore
|
|
|
|
uploads *UploadingService
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|
|
|
|
|
2022-09-21 04:25:07 -05:00
|
|
|
// NewScreenshotImageService returns a new ScreenshotImageService.
|
|
|
|
func NewScreenshotImageService(
|
2022-11-08 19:52:16 -06:00
|
|
|
cache CacheService,
|
2022-09-21 04:25:07 -05:00
|
|
|
limiter screenshot.RateLimiter,
|
|
|
|
logger log.Logger,
|
|
|
|
screenshots screenshot.ScreenshotService,
|
2023-01-05 10:07:46 -06:00
|
|
|
screenshotTimeout time.Duration,
|
2022-09-21 04:25:07 -05:00
|
|
|
store store.ImageStore,
|
|
|
|
uploads *UploadingService) ImageService {
|
2022-05-22 09:33:49 -05:00
|
|
|
return &ScreenshotImageService{
|
2023-01-05 10:07:46 -06:00
|
|
|
cache: cache,
|
|
|
|
limiter: limiter,
|
|
|
|
logger: logger,
|
|
|
|
screenshots: screenshots,
|
|
|
|
screenshotTimeout: screenshotTimeout,
|
|
|
|
store: store,
|
|
|
|
uploads: uploads,
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewScreenshotImageServiceFromCfg returns a new ScreenshotImageService
|
|
|
|
// from the configuration.
|
2022-09-21 04:25:07 -05:00
|
|
|
func NewScreenshotImageServiceFromCfg(cfg *setting.Cfg, db *store.DBstore, ds dashboards.DashboardService,
|
|
|
|
rs rendering.Service, r prometheus.Registerer) (ImageService, error) {
|
|
|
|
var (
|
2023-01-05 10:07:46 -06:00
|
|
|
cache CacheService = &NoOpCacheService{}
|
|
|
|
limiter screenshot.RateLimiter = &screenshot.NoOpRateLimiter{}
|
|
|
|
screenshots screenshot.ScreenshotService = &screenshot.ScreenshotUnavailableService{}
|
|
|
|
screenshotTimeout time.Duration = 0
|
|
|
|
uploads *UploadingService = nil
|
2022-09-21 04:25:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// If screenshots are enabled
|
|
|
|
if cfg.UnifiedAlerting.Screenshots.Capture {
|
2022-11-08 19:52:16 -06:00
|
|
|
cache = NewInmemCacheService(screenshotCacheTTL, r)
|
2022-09-21 04:25:07 -05:00
|
|
|
limiter = screenshot.NewTokenRateLimiter(cfg.UnifiedAlerting.Screenshots.MaxConcurrentScreenshots)
|
2024-01-23 05:36:22 -06:00
|
|
|
screenshots = screenshot.NewHeadlessScreenshotService(cfg, ds, rs, r)
|
2023-01-05 10:07:46 -06:00
|
|
|
screenshotTimeout = cfg.UnifiedAlerting.Screenshots.CaptureTimeout
|
2022-09-21 04:25:07 -05:00
|
|
|
|
|
|
|
// Image uploading is an optional feature
|
|
|
|
if cfg.UnifiedAlerting.Screenshots.UploadExternalImageStorage {
|
2024-01-23 05:36:22 -06:00
|
|
|
m, err := imguploader.NewImageUploader(cfg)
|
2022-09-21 04:25:07 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize uploading screenshot service: %w", err)
|
|
|
|
}
|
|
|
|
uploads = NewUploadingService(m, r)
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|
|
|
|
}
|
2022-09-21 04:25:07 -05:00
|
|
|
|
2023-01-05 10:07:46 -06:00
|
|
|
return NewScreenshotImageService(cache, limiter, log.New("ngalert.image"),
|
|
|
|
screenshots, screenshotTimeout, db, uploads), nil
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|
|
|
|
|
2022-06-29 14:30:13 -05:00
|
|
|
// NewImage returns a screenshot of the alert rule or an error.
|
|
|
|
//
|
|
|
|
// The alert rule must be associated with a dashboard panel for a screenshot to be
|
|
|
|
// taken. If the alert rule does not have a Dashboard UID in its annotations,
|
2023-01-05 10:07:46 -06:00
|
|
|
// or the dashboard does not exist, a models.ErrNoDashboard error is returned. If the
|
2022-06-29 14:30:13 -05:00
|
|
|
// alert rule has a Dashboard UID and the dashboard exists, but does not have a
|
2023-01-05 10:07:46 -06:00
|
|
|
// Panel ID in its annotations then a models.ErrNoPanel error is returned.
|
2022-09-21 04:25:07 -05:00
|
|
|
func (s *ScreenshotImageService) NewImage(ctx context.Context, r *models.AlertRule) (*models.Image, error) {
|
2022-11-10 03:41:31 -06:00
|
|
|
logger := s.logger.FromContext(ctx)
|
|
|
|
|
2022-11-10 03:58:38 -06:00
|
|
|
dashboardUID := r.GetDashboardUID()
|
|
|
|
if dashboardUID == "" {
|
2022-11-10 03:41:31 -06:00
|
|
|
logger.Debug("Cannot take screenshot for alert rule as it is not associated with a dashboard")
|
2022-11-09 15:06:49 -06:00
|
|
|
return nil, models.ErrNoDashboard
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|
2022-09-21 04:25:07 -05:00
|
|
|
|
2022-11-10 03:58:38 -06:00
|
|
|
panelID := r.GetPanelID()
|
|
|
|
if panelID <= 0 {
|
2022-11-10 03:41:31 -06:00
|
|
|
logger.Debug("Cannot take screenshot for alert rule as it is not associated with a panel")
|
2022-11-09 15:06:49 -06:00
|
|
|
return nil, models.ErrNoPanel
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|
|
|
|
|
2022-11-10 03:58:38 -06:00
|
|
|
logger = logger.New("dashboard", dashboardUID, "panel", panelID)
|
|
|
|
|
2022-11-09 11:01:48 -06:00
|
|
|
opts := screenshot.ScreenshotOptions{
|
2023-02-09 14:23:01 -06:00
|
|
|
OrgID: r.OrgID,
|
2022-11-10 03:58:38 -06:00
|
|
|
DashboardUID: dashboardUID,
|
|
|
|
PanelID: panelID,
|
2023-01-05 10:07:46 -06:00
|
|
|
Timeout: s.screenshotTimeout,
|
2022-11-09 11:01:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent concurrent screenshots of the same dashboard panel we use singleflight,
|
|
|
|
// deduplicated on a base64 hash of the screenshot options.
|
|
|
|
optsHash := base64.StdEncoding.EncodeToString(opts.Hash())
|
|
|
|
|
2022-11-08 19:52:16 -06:00
|
|
|
// If there is an image is in the cache return it instead of taking another screenshot
|
2022-11-09 11:01:48 -06:00
|
|
|
if image, ok := s.cache.Get(ctx, optsHash); ok {
|
2022-11-09 10:32:58 -06:00
|
|
|
logger.Debug("Found cached image", "token", image.Token)
|
2022-11-08 19:52:16 -06:00
|
|
|
return &image, nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:32:58 -06:00
|
|
|
logger.Debug("Requesting screenshot")
|
|
|
|
|
2023-08-30 10:46:47 -05:00
|
|
|
result, err, _ := s.singleflight.Do(optsHash, func() (any, error) {
|
2023-01-05 10:07:46 -06:00
|
|
|
// We create both a context with timeout and set a timeout in ScreenshotOptions. The timeout
|
|
|
|
// in the context is used for both database queries and the request to the rendering service,
|
|
|
|
// while the timeout in ScreenshotOptions is passed to the rendering service where it is used as
|
|
|
|
// a client timeout. It is not recommended to pass a context without a deadline and the context
|
|
|
|
// deadline should be at least as long as the timeout in ScreenshotOptions.
|
|
|
|
screenshotCtx, cancelFunc := context.WithTimeout(ctx, s.screenshotTimeout)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-11-08 19:52:16 -06:00
|
|
|
// Once deduplicated concurrent screenshots are then rate-limited
|
2023-01-05 10:07:46 -06:00
|
|
|
screenshot, err := s.limiter.Do(screenshotCtx, opts, s.screenshots.Take)
|
2022-09-21 04:25:07 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, dashboards.ErrDashboardNotFound) {
|
2022-11-09 15:06:49 -06:00
|
|
|
return nil, models.ErrNoDashboard
|
2022-09-21 04:25:07 -05:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-08 19:52:16 -06:00
|
|
|
|
2022-11-09 10:32:58 -06:00
|
|
|
logger.Debug("Took screenshot", "path", screenshot.Path)
|
2022-09-21 04:25:07 -05:00
|
|
|
image := models.Image{Path: screenshot.Path}
|
2022-11-08 19:52:16 -06:00
|
|
|
|
|
|
|
// Uploading images is optional
|
2022-09-21 04:25:07 -05:00
|
|
|
if s.uploads != nil {
|
|
|
|
if image, err = s.uploads.Upload(ctx, image); err != nil {
|
2022-11-09 10:32:58 -06:00
|
|
|
logger.Warn("Failed to upload image", "error", err)
|
|
|
|
} else {
|
|
|
|
logger.Debug("Uploaded image", "url", image.URL)
|
2022-09-21 04:25:07 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-08 19:52:16 -06:00
|
|
|
|
2022-09-21 04:25:07 -05:00
|
|
|
if err := s.store.SaveImage(ctx, &image); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to save image: %w", err)
|
|
|
|
}
|
2022-11-09 10:32:58 -06:00
|
|
|
logger.Debug("Saved image", "token", image.Token)
|
|
|
|
|
2022-09-21 04:25:07 -05:00
|
|
|
return image, nil
|
2022-05-22 09:33:49 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-06-29 03:39:24 -05:00
|
|
|
return nil, err
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|
|
|
|
|
2022-09-21 04:25:07 -05:00
|
|
|
image := result.(models.Image)
|
2022-11-09 11:01:48 -06:00
|
|
|
if err = s.cache.Set(ctx, optsHash, image); err != nil {
|
2022-11-09 10:32:58 -06:00
|
|
|
s.logger.Warn("Failed to cache image",
|
|
|
|
"token", image.Token,
|
|
|
|
"error", err)
|
2022-11-08 19:52:16 -06:00
|
|
|
}
|
|
|
|
|
2022-09-21 04:25:07 -05:00
|
|
|
return &image, nil
|
2022-05-22 09:33:49 -05:00
|
|
|
}
|