Documentation
¶
Index ¶
- Variables
- func DeepCopyWithTemplate(value any, tmplTextFunc TemplateFunc) (any, error)
- func MarkRouteLabelsRendered(d *Data)
- type Alert
- type Alerts
- type Data
- type FuncMap
- type KV
- type Option
- type Pair
- type Pairs
- type Strings
- type Template
- func (t *Template) Data(recv string, groupLabels, routeLabels model.LabelSet, ...) *Data
- func (t *Template) ExecuteHTMLString(html string, data any) (string, error)
- func (t *Template) ExecuteTextString(text string, data any) (string, error)
- func (t *Template) FromGlob(path string) error
- func (t *Template) Parse(r io.Reader) error
- func (t *Template) RouteLabelRenderer(data *Data) func(name string) (string, error)
- type TemplateFunc
Constants ¶
This section is empty.
Variables ¶
var DefaultFuncs = FuncMap{ "toUpper": strings.ToUpper, "toLower": strings.ToLower, "title": func(text string) string { return cases.Title(language.AmericanEnglish).String(text) }, "trimSpace": strings.TrimSpace, "join": func(sep string, s []string) string { return strings.Join(s, sep) }, "match": regexp.MatchString, "safeHtml": func(text string) tmplhtml.HTML { return tmplhtml.HTML(text) }, "safeUrl": func(text string) tmplhtml.URL { return tmplhtml.URL(text) }, "urlUnescape": url.QueryUnescape, "reReplaceAll": func(pattern, repl, text string) string { re := regexp.MustCompile(pattern) return re.ReplaceAllString(text, repl) }, "stringSlice": func(s ...string) []string { return s }, "routeLabels": func(name string) (string, error) { return "", nil }, "date": func(fmt string, t time.Time) string { return t.Format(fmt) }, "tz": func(name string, t time.Time) (time.Time, error) { loc, err := time.LoadLocation(name) if err != nil { return time.Time{}, err } return t.In(loc), nil }, "now": time.Now, "since": time.Since, "humanizeDuration": commonTemplates.HumanizeDuration, "toDate": func(layout, s string) time.Time { t, _ := time.ParseInLocation(layout, s, time.UTC) return t }, "mustToDate": func(layout, s string) (time.Time, error) { return time.ParseInLocation(layout, s, time.UTC) }, "toJson": func(v any) (string, error) { bytes, err := json.Marshal(v) if err != nil { return "", err } return string(bytes), nil }, "base64encode": func(text string) string { return base64.URLEncoding.EncodeToString([]byte(text)) }, "base64decode": func(text string) (string, error) { decoded, err := base64.URLEncoding.DecodeString(text) if err != nil { return "", err } return string(decoded), nil }, "list": func(args ...any) ([]any, error) { if args == nil { return []any{}, nil } return args, nil }, "append": func(slice []any, args ...any) []any { return append(slice, args...) }, "dict": func(values ...any) (map[string]any, error) { if len(values)%2 != 0 { return nil, fmt.Errorf("dict requires an even number of arguments") } res := make(map[string]any, len(values)/2) for i := 0; i < len(values); i += 2 { key, ok := values[i].(string) if !ok { return nil, fmt.Errorf("dict keys must be strings") } res[key] = values[i+1] } return res, nil }, }
Functions ¶
func DeepCopyWithTemplate ¶ added in v0.30.0
func DeepCopyWithTemplate(value any, tmplTextFunc TemplateFunc) (any, error)
DeepCopyWithTemplate returns a deep copy of a map/slice/array/string/int/bool or combination thereof, executing the provided template (with the provided data) on all string keys or values. All maps are connverted to map[string]any, with all non-string keys discarded.
func MarkRouteLabelsRendered ¶ added in v0.34.0
func MarkRouteLabelsRendered(d *Data)
MarkRouteLabelsRendered marks d.RouteLabels as already-rendered, so the routeLabels template function returns the values verbatim rather than executing them a second time.
Types ¶
type Alert ¶
type Alert struct {
Status string `json:"status"`
Labels KV `json:"labels"`
Annotations KV `json:"annotations"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Fingerprint string `json:"fingerprint"`
}
Alert holds one alert for notification templates.
type Alerts ¶
type Alerts []Alert
Alerts is a list of Alert objects.
type Data ¶
type Data struct {
Receiver string `json:"receiver"`
Status string `json:"status"`
Alerts Alerts `json:"alerts"`
NotificationReason string `json:"notification_reason"`
GroupLabels KV `json:"groupLabels"`
CommonLabels KV `json:"commonLabels"`
CommonAnnotations KV `json:"commonAnnotations"`
RouteLabels KV `json:"routeLabels"`
ExternalURL string `json:"externalURL"`
// contains filtered or unexported fields
}
Data is the data passed to notification templates and webhook pushes.
End-users should not be exposed to Go's type system, as this will confuse them and prevent simple things like simple equality checks to fail. Map everything to float64/string.
type KV ¶
KV is a set of key/value string pairs.
func (KV) SortedPairs ¶
SortedPairs returns a sorted list of key/value pairs.
type Option ¶ added in v0.26.0
Option is generic modifier of the text and html templates used by a Template.
type Strings ¶ added in v0.34.0
type Strings []string
Strings is a list of strings exposed to templates.
type Template ¶
Template bundles a text and a html template instance.
func FromGlobs ¶
FromGlobs calls ParseGlob on all path globs provided and returns the resulting Template.
func New ¶ added in v0.26.0
New returns a new Template with the DefaultFuncs added. The DefaultFuncs have precedence over any added custom functions. Options allow customization of the text and html templates in given order.
func (*Template) Data ¶
func (t *Template) Data(recv string, groupLabels, routeLabels model.LabelSet, notificationReason string, alerts ...*types.Alert) *Data
Data assembles data for template expansion.
func (*Template) ExecuteHTMLString ¶
ExecuteHTMLString needs a meaningful doc comment (TODO(fabxc)).
func (*Template) ExecuteTextString ¶
ExecuteTextString needs a meaningful doc comment (TODO(fabxc)).
func (*Template) FromGlob ¶ added in v0.26.0
FromGlob calls ParseGlob on given path glob provided and parses into the template.
func (*Template) RouteLabelRenderer ¶ added in v0.34.0
RouteLabelRenderer returns a function that renders a route label by name from data.RouteLabels. All returned renders share a single resolver, so each label — and any label it cross-references via {{ routeLabels "x" }} — is rendered at most once per call to RouteLabelRenderer (and cycles are still detected). It is used by the dispatch-time expansion of a group's route labels, where many labels are rendered against the same data and may reference each other.