Alerting: Log when alert rule cannot be screenshot to help debugging (#58537)

This commit is contained in:
George Robinson 2022-11-10 09:41:31 +00:00 committed by GitHub
parent bf5a08e039
commit 68600c224b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -113,11 +113,15 @@ func NewScreenshotImageServiceFromCfg(cfg *setting.Cfg, db *store.DBstore, ds da
// alert rule has a Dashboard UID and the dashboard exists, but does not have a
// Panel ID in its annotations then an models.ErrNoPanel error is returned.
func (s *ScreenshotImageService) NewImage(ctx context.Context, r *models.AlertRule) (*models.Image, error) {
logger := s.logger.FromContext(ctx)
if r.DashboardUID == nil || *r.DashboardUID == "" {
logger.Debug("Cannot take screenshot for alert rule as it is not associated with a dashboard")
return nil, models.ErrNoDashboard
}
if r.PanelID == nil || *r.PanelID == 0 {
logger.Debug("Cannot take screenshot for alert rule as it is not associated with a panel")
return nil, models.ErrNoPanel
}
@ -127,14 +131,12 @@ func (s *ScreenshotImageService) NewImage(ctx context.Context, r *models.AlertRu
Timeout: screenshotTimeout,
}
logger = logger.New("dashboard", opts.DashboardUID, "panel", opts.PanelID)
// 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())
logger := s.logger.FromContext(ctx).New(
"dashboard", opts.DashboardUID,
"panel", opts.PanelID)
// If there is an image is in the cache return it instead of taking another screenshot
if image, ok := s.cache.Get(ctx, optsHash); ok {
logger.Debug("Found cached image", "token", image.Token)