Chore: Remove context.TODO (#43458)

* Remove context.TODO() from services

* Fix live test
This commit is contained in:
idafurjes
2021-12-28 10:26:18 +01:00
committed by GitHub
parent 56b3dc5445
commit 56c3875bb9
24 changed files with 81 additions and 77 deletions
+5 -4
View File
@@ -1,6 +1,7 @@
package state
import (
"context"
"fmt"
"net/url"
"strings"
@@ -32,14 +33,14 @@ func newCache(logger log.Logger, metrics *metrics.State, externalURL *url.URL) *
}
}
func (c *cache) getOrCreate(alertRule *ngModels.AlertRule, result eval.Result) *State {
func (c *cache) getOrCreate(ctx context.Context, alertRule *ngModels.AlertRule, result eval.Result) *State {
c.mtxStates.Lock()
defer c.mtxStates.Unlock()
// clone the labels so we don't change eval.Result
labels := result.Instance.Copy()
attachRuleLabels(labels, alertRule)
ruleLabels, annotations := c.expandRuleLabelsAndAnnotations(alertRule, labels, result)
ruleLabels, annotations := c.expandRuleLabelsAndAnnotations(ctx, alertRule, labels, result)
// if duplicate labels exist, alertRule label will take precedence
lbs := mergeLabels(ruleLabels, result.Instance)
@@ -88,11 +89,11 @@ func attachRuleLabels(m map[string]string, alertRule *ngModels.AlertRule) {
m[prometheusModel.AlertNameLabel] = alertRule.Title
}
func (c *cache) expandRuleLabelsAndAnnotations(alertRule *ngModels.AlertRule, labels map[string]string, alertInstance eval.Result) (map[string]string, map[string]string) {
func (c *cache) expandRuleLabelsAndAnnotations(ctx context.Context, alertRule *ngModels.AlertRule, labels map[string]string, alertInstance eval.Result) (map[string]string, map[string]string) {
expand := func(original map[string]string) map[string]string {
expanded := make(map[string]string, len(original))
for k, v := range original {
ev, err := expandTemplate(alertRule.Title, v, labels, alertInstance, c.externalURL)
ev, err := expandTemplate(ctx, alertRule.Title, v, labels, alertInstance, c.externalURL)
expanded[k] = ev
if err != nil {
c.log.Error("error in expanding template", "name", k, "value", v, "err", err.Error())
+3 -3
View File
@@ -116,8 +116,8 @@ func (st *Manager) Warm() {
}
}
func (st *Manager) getOrCreate(alertRule *ngModels.AlertRule, result eval.Result) *State {
return st.cache.getOrCreate(alertRule, result)
func (st *Manager) getOrCreate(ctx context.Context, alertRule *ngModels.AlertRule, result eval.Result) *State {
return st.cache.getOrCreate(ctx, alertRule, result)
}
func (st *Manager) set(entry *State) {
@@ -153,7 +153,7 @@ func (st *Manager) ProcessEvalResults(ctx context.Context, alertRule *ngModels.A
// Set the current state based on evaluation results
func (st *Manager) setNextState(ctx context.Context, alertRule *ngModels.AlertRule, result eval.Result) *State {
currentState := st.getOrCreate(alertRule, result)
currentState := st.getOrCreate(ctx, alertRule, result)
currentState.LastEvaluationTime = result.EvaluatedAt
currentState.EvaluationDuration = result.EvaluationDuration
+2 -2
View File
@@ -31,7 +31,7 @@ func (v templateCaptureValue) String() string {
return strconv.FormatFloat(v.Value, 'f', -1, 64)
}
func expandTemplate(name, text string, labels map[string]string, alertInstance eval.Result, externalURL *url.URL) (result string, resultErr error) {
func expandTemplate(ctx context.Context, name, text string, labels map[string]string, alertInstance eval.Result, externalURL *url.URL) (result string, resultErr error) {
name = "__alert_" + name
text = "{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}" + text
data := struct {
@@ -45,7 +45,7 @@ func expandTemplate(name, text string, labels map[string]string, alertInstance e
}
expander := template.NewTemplateExpander(
context.TODO(), // This context is only used with the `query()` function - which we don't support yet.
ctx, // This context is only used with the `query()` function - which we don't support yet.
text,
name,
data,
+2 -1
View File
@@ -1,6 +1,7 @@
package state
import (
"context"
"errors"
"net/url"
"testing"
@@ -403,7 +404,7 @@ func TestExpandTemplate(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
v, err := expandTemplate("test", c.text, c.labels, c.alertInstance, externalURL)
v, err := expandTemplate(context.Background(), "test", c.text, c.labels, c.alertInstance, externalURL)
if c.expectedError != nil {
require.NotNil(t, err)
require.EqualError(t, c.expectedError, err.Error())