Alerting: Fix logging pointer address of DashboardUID and PanelID variables (#58539)

This commit is contained in:
George Robinson 2022-11-10 09:58:38 +00:00 committed by GitHub
parent 68600c224b
commit c5ae1bcfe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 10 deletions

View File

@ -115,24 +115,26 @@ func NewScreenshotImageServiceFromCfg(cfg *setting.Cfg, db *store.DBstore, ds da
func (s *ScreenshotImageService) NewImage(ctx context.Context, r *models.AlertRule) (*models.Image, error) { func (s *ScreenshotImageService) NewImage(ctx context.Context, r *models.AlertRule) (*models.Image, error) {
logger := s.logger.FromContext(ctx) logger := s.logger.FromContext(ctx)
if r.DashboardUID == nil || *r.DashboardUID == "" { dashboardUID := r.GetDashboardUID()
if dashboardUID == "" {
logger.Debug("Cannot take screenshot for alert rule as it is not associated with a dashboard") logger.Debug("Cannot take screenshot for alert rule as it is not associated with a dashboard")
return nil, models.ErrNoDashboard return nil, models.ErrNoDashboard
} }
if r.PanelID == nil || *r.PanelID == 0 { panelID := r.GetPanelID()
if panelID <= 0 {
logger.Debug("Cannot take screenshot for alert rule as it is not associated with a panel") logger.Debug("Cannot take screenshot for alert rule as it is not associated with a panel")
return nil, models.ErrNoPanel return nil, models.ErrNoPanel
} }
logger = logger.New("dashboard", dashboardUID, "panel", panelID)
opts := screenshot.ScreenshotOptions{ opts := screenshot.ScreenshotOptions{
DashboardUID: *r.DashboardUID, DashboardUID: dashboardUID,
PanelID: *r.PanelID, PanelID: panelID,
Timeout: screenshotTimeout, Timeout: screenshotTimeout,
} }
logger = logger.New("dashboard", opts.DashboardUID, "panel", opts.PanelID)
// To prevent concurrent screenshots of the same dashboard panel we use singleflight, // To prevent concurrent screenshots of the same dashboard panel we use singleflight,
// deduplicated on a base64 hash of the screenshot options. // deduplicated on a base64 hash of the screenshot options.
optsHash := base64.StdEncoding.EncodeToString(opts.Hash()) optsHash := base64.StdEncoding.EncodeToString(opts.Hash())

View File

@ -166,6 +166,22 @@ type AlertRule struct {
Labels map[string]string Labels map[string]string
} }
// GetDashboardUID returns the DashboardUID or "".
func (alertRule *AlertRule) GetDashboardUID() string {
if alertRule.DashboardUID != nil {
return *alertRule.DashboardUID
}
return ""
}
// GetPanelID returns the Panel ID or -1.
func (alertRule *AlertRule) GetPanelID() int64 {
if alertRule.PanelID != nil {
return *alertRule.PanelID
}
return -1
}
type LabelOption func(map[string]string) type LabelOption func(map[string]string)
func WithoutInternalLabels() LabelOption { func WithoutInternalLabels() LabelOption {

View File

@ -246,8 +246,8 @@ func (st *Manager) setNextState(ctx context.Context, alertRule *ngModels.AlertRu
image, err := takeImage(ctx, st.images, alertRule) image, err := takeImage(ctx, st.images, alertRule)
if err != nil { if err != nil {
logger.Warn("Failed to take an image", logger.Warn("Failed to take an image",
"dashboard", alertRule.DashboardUID, "dashboard", alertRule.GetDashboardUID(),
"panel", alertRule.PanelID, "panel", alertRule.GetPanelID(),
"error", err) "error", err)
} else if image != nil { } else if image != nil {
currentState.Image = image currentState.Image = image
@ -373,8 +373,8 @@ func (st *Manager) staleResultsHandler(ctx context.Context, logger log.Logger, r
image, err := takeImage(ctx, st.images, r) image, err := takeImage(ctx, st.images, r)
if err != nil { if err != nil {
logger.Warn("Failed to take an image", logger.Warn("Failed to take an image",
"dashboard", r.DashboardUID, "dashboard", r.GetDashboardUID(),
"panel", r.PanelID, "panel", r.GetPanelID(),
"error", err) "error", err)
} else if image != nil { } else if image != nil {
resolvedImage = image resolvedImage = image