From 68600c224b36c4668cec75b7acc30369ac8f4eec Mon Sep 17 00:00:00 2001 From: George Robinson Date: Thu, 10 Nov 2022 09:41:31 +0000 Subject: [PATCH] Alerting: Log when alert rule cannot be screenshot to help debugging (#58537) --- 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 5051051c2de..051019def28 100644 --- a/pkg/services/ngalert/image/service.go +++ b/pkg/services/ngalert/image/service.go @@ -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)