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) {
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")
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")
return nil, models.ErrNoPanel
}
logger = logger.New("dashboard", dashboardUID, "panel", panelID)
opts := screenshot.ScreenshotOptions{
DashboardUID: *r.DashboardUID,
PanelID: *r.PanelID,
DashboardUID: dashboardUID,
PanelID: panelID,
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())

View File

@ -166,6 +166,22 @@ type AlertRule struct {
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)
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)
if err != nil {
logger.Warn("Failed to take an image",
"dashboard", alertRule.DashboardUID,
"panel", alertRule.PanelID,
"dashboard", alertRule.GetDashboardUID(),
"panel", alertRule.GetPanelID(),
"error", err)
} else if image != nil {
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)
if err != nil {
logger.Warn("Failed to take an image",
"dashboard", r.DashboardUID,
"panel", r.PanelID,
"dashboard", r.GetDashboardUID(),
"panel", r.GetPanelID(),
"error", err)
} else if image != nil {
resolvedImage = image