From b372406a61797094fa3bf9495365755dcd6307ac Mon Sep 17 00:00:00 2001 From: George Robinson Date: Thu, 10 Nov 2022 11:06:50 +0000 Subject: [PATCH] Alerting: Log when alert rule cannot be screenshot to help debugging (#58537) (#58563) (cherry picked from commit 68600c224b36c4668cec75b7acc30369ac8f4eec) --- pkg/services/ngalert/image/service.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/services/ngalert/image/service.go b/pkg/services/ngalert/image/service.go index f3382d338c4..5b004f615de 100644 --- a/pkg/services/ngalert/image/service.go +++ b/pkg/services/ngalert/image/service.go @@ -124,11 +124,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 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, 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, ErrNoPanel } @@ -138,14 +142,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)