mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Change templateCaptureValue to support using template functions (#38766)
* Change templateCaptureValue to support using template functions This commit changes templateCaptureValue to use float64 for the value instead of *float64. This change means that annotations and labels can use the float64 value with functions such as printf and avoid having to check for nil. It also means that absent values are now printed as 0. * Use math.NaN() instead of 0 for absent value
This commit is contained in:
@@ -3,6 +3,7 @@ package state
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -110,16 +111,13 @@ func (c *cache) expandRuleLabelsAndAnnotations(alertRule *ngModels.AlertRule, la
|
||||
// and labels template.
|
||||
type templateCaptureValue struct {
|
||||
Labels map[string]string
|
||||
Value *float64
|
||||
Value float64
|
||||
}
|
||||
|
||||
// String implements the Stringer interface to print the value of each RefID
|
||||
// in the template via {{ $values.A }} rather than {{ $values.A.Value }}.
|
||||
func (v templateCaptureValue) String() string {
|
||||
if v.Value != nil {
|
||||
return strconv.FormatFloat(*v.Value, 'f', -1, 64)
|
||||
}
|
||||
return "null"
|
||||
return strconv.FormatFloat(v.Value, 'f', -1, 64)
|
||||
}
|
||||
|
||||
func expandTemplate(name, text string, labels map[string]string, alertInstance eval.Result) (result string, resultErr error) {
|
||||
@@ -148,23 +146,31 @@ func expandTemplate(name, text string, labels map[string]string, alertInstance e
|
||||
Value string
|
||||
}{
|
||||
Labels: labels,
|
||||
Values: func() map[string]templateCaptureValue {
|
||||
m := make(map[string]templateCaptureValue)
|
||||
for k, v := range alertInstance.Values {
|
||||
m[k] = templateCaptureValue{
|
||||
Labels: v.Labels,
|
||||
Value: v.Value,
|
||||
}
|
||||
}
|
||||
return m
|
||||
}(),
|
||||
Value: alertInstance.EvaluationString,
|
||||
Values: newTemplateCaptureValues(alertInstance.Values),
|
||||
Value: alertInstance.EvaluationString,
|
||||
}); err != nil {
|
||||
return "", fmt.Errorf("error executing template %v: %s", name, err.Error())
|
||||
}
|
||||
return buffer.String(), nil
|
||||
}
|
||||
|
||||
func newTemplateCaptureValues(values map[string]eval.NumberValueCapture) map[string]templateCaptureValue {
|
||||
m := make(map[string]templateCaptureValue)
|
||||
for k, v := range values {
|
||||
var f float64
|
||||
if v.Value != nil {
|
||||
f = *v.Value
|
||||
} else {
|
||||
f = math.NaN()
|
||||
}
|
||||
m[k] = templateCaptureValue{
|
||||
Labels: v.Labels,
|
||||
Value: f,
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (c *cache) set(entry *State) {
|
||||
c.mtxStates.Lock()
|
||||
defer c.mtxStates.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user