Make templateData a struct

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov@grafana.com>
This commit is contained in:
Dimitar Dimitrov 2024-12-24 11:09:33 +02:00
parent 5b2cfc5e45
commit 8b45384a44
No known key found for this signature in database
GPG key ID: 4541B04E6C90EBC3
2 changed files with 23 additions and 7 deletions

View file

@ -380,7 +380,7 @@ func (r *AlertingRule) Eval(ctx context.Context, queryOffset time.Duration, ts t
result, err := tmpl.Expand()
if err != nil {
result = fmt.Sprintf("<error expanding template: %s>", err)
r.logger.Warn("Expanding alert template failed", "err", err, "data", fmt.Sprintf("%#v", tmplData))
r.logger.Warn("Expanding alert template failed", "err", err, "data", tmplData)
}
return result
}

View file

@ -280,14 +280,30 @@ func NewTemplateExpander(
}
}
// AlertTemplateData returns the interface to be used in expanding the template.
func AlertTemplateData(labels, externalLabels map[string]string, externalURL string, smpl promql.Sample) interface{} {
res := struct {
type templateData struct {
Labels map[string]string
ExternalLabels map[string]string
ExternalURL string
Value interface{}
}{
}
// String implements fmt.Stringer interface.
func (t templateData) String() string {
labelsString := func(labels map[string]string) string {
// model.LabelSet has a ready String() method, that we can use.
labelSet := make(model.LabelSet, len(t.Labels))
for k, v := range t.Labels {
labelSet[model.LabelName(k)] = model.LabelValue(v)
}
return labelSet.String()
}
return fmt.Sprintf("<Labels: %s, ExternalLabels: %s, ExternalURL: %s, Value: %v>", labelsString(t.Labels), labelsString(t.ExternalLabels), t.ExternalURL, t.Value)
}
// AlertTemplateData returns the interface to be used in expanding the template.
func AlertTemplateData(labels, externalLabels map[string]string, externalURL string, smpl promql.Sample) interface{} {
res := templateData{
Labels: labels,
ExternalLabels: externalLabels,
ExternalURL: externalURL,