diff --git a/rules/alerting.go b/rules/alerting.go index 5fe438bd15..7e74c176aa 100644 --- a/rules/alerting.go +++ b/rules/alerting.go @@ -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("", 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 } diff --git a/template/template.go b/template/template.go index 0698c6c8ac..610ec722bb 100644 --- a/template/template.go +++ b/template/template.go @@ -280,14 +280,30 @@ func NewTemplateExpander( } } +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("", 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 := struct { - Labels map[string]string - ExternalLabels map[string]string - ExternalURL string - Value interface{} - }{ + res := templateData{ Labels: labels, ExternalLabels: externalLabels, ExternalURL: externalURL,