remove the global log error/warn etc functions (#41404)
* remove the global log error/warn etc functions and use request context logger whenever possible
This commit is contained in:
@@ -24,7 +24,7 @@ type EvalContext struct {
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Rule *Rule
|
||||
log log.Logger
|
||||
Log log.Logger
|
||||
|
||||
dashboardRef *models.DashboardRef
|
||||
|
||||
@@ -46,7 +46,7 @@ func NewEvalContext(alertCtx context.Context, rule *Rule, requestValidator model
|
||||
Rule: rule,
|
||||
Logs: make([]*ResultLogEntry, 0),
|
||||
EvalMatches: make([]*EvalMatch, 0),
|
||||
log: log.New("alerting.evalContext"),
|
||||
Log: log.New("alerting.evalContext"),
|
||||
PrevAlertState: rule.State,
|
||||
RequestValidator: requestValidator,
|
||||
}
|
||||
@@ -152,7 +152,7 @@ func (c *EvalContext) GetNewState() models.AlertStateType {
|
||||
|
||||
func getNewStateInternal(c *EvalContext) models.AlertStateType {
|
||||
if c.Error != nil {
|
||||
c.log.Error("Alert Rule Result Error",
|
||||
c.Log.Error("Alert Rule Result Error",
|
||||
"ruleId", c.Rule.ID,
|
||||
"name", c.Rule.Name,
|
||||
"error", c.Error,
|
||||
@@ -169,7 +169,7 @@ func getNewStateInternal(c *EvalContext) models.AlertStateType {
|
||||
}
|
||||
|
||||
if c.NoDataFound {
|
||||
c.log.Info("Alert Rule returned no data",
|
||||
c.Log.Info("Alert Rule returned no data",
|
||||
"ruleId", c.Rule.ID,
|
||||
"name", c.Rule.Name,
|
||||
"changing state to", c.Rule.NoDataState.ToAlertState())
|
||||
|
||||
@@ -239,22 +239,22 @@ func generateImageCaption(evalContext *alerting.EvalContext, ruleURL string, met
|
||||
|
||||
if len(ruleURL) > 0 {
|
||||
urlLine := fmt.Sprintf("\nURL: %s", ruleURL)
|
||||
message = appendIfPossible(message, urlLine, captionLengthLimit)
|
||||
message = appendIfPossible(evalContext.Log, message, urlLine, captionLengthLimit)
|
||||
}
|
||||
|
||||
if metrics != "" {
|
||||
metricsLines := fmt.Sprintf("\n\nMetrics:%s", metrics)
|
||||
message = appendIfPossible(message, metricsLines, captionLengthLimit)
|
||||
message = appendIfPossible(evalContext.Log, message, metricsLines, captionLengthLimit)
|
||||
}
|
||||
|
||||
return message
|
||||
}
|
||||
|
||||
func appendIfPossible(message string, extra string, sizeLimit int) string {
|
||||
func appendIfPossible(tlog log.Logger, message string, extra string, sizeLimit int) string {
|
||||
if len(extra)+len(message) <= sizeLimit {
|
||||
return message + extra
|
||||
}
|
||||
log.Debug("Line too long for image caption.", "value", extra)
|
||||
tlog.Debug("Line too long for image caption.", "value", extra)
|
||||
return message
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user