alerttemplate

package
v0.0.96 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package alerttemplate renders configurable GeneratorURL values for promxy's alerts. By default promxy emits the same Prometheus-style GeneratorURL as upstream (a link to promxy's own graph page for the alert expression). That is not useful when promxy is the central evaluation point for many backends and operators triage alerts elsewhere (Grafana alerting, an incident manager, a per-tenant dashboard, ...). This package lets an operator configure Go templates, selected per-alert by label, that produce the GeneratorURL instead. It is entirely opt-in: with no configuration the caller falls back to the built-in URL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Default is used when no rule matches. It is either an inline template
	// body or the name of one of the Named templates. When empty, promxy uses
	// its built-in Prometheus-style GeneratorURL.
	Default string `yaml:"default,omitempty"`

	// Named holds reusable inline templates, addressable by name from Default
	// and from a rule's Template.
	Named map[string]string `yaml:"named,omitempty"`

	// Rules select a template based on alert labels. They are evaluated
	// top-to-bottom and the first match wins.
	Rules []Rule `yaml:"rules,omitempty"`
}

Config is the promxy alert-template configuration, nested under `promxy.alert_templates` in the config file.

type Data

type Data struct {
	// ExternalURL is promxy's configured external URL.
	ExternalURL string
	// Expr is the PromQL expression that triggered the alert.
	Expr string
	// Labels are the alert's labels.
	Labels map[string]string
	// Annotations are the alert's annotations.
	Annotations map[string]string
	// AlertName is a convenience for the value of the "alertname" label.
	AlertName string
}

Data is the value passed to a GeneratorURL template during execution.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager holds the compiled templates and renders GeneratorURLs. Its zero value is not usable; construct one with NewManager. It is safe for concurrent use: Apply swaps the compiled template set under a write lock while GeneratorURL reads it under a read lock.

func NewManager

func NewManager() *Manager

NewManager returns an empty Manager. Until Apply installs a configuration, GeneratorURL always reports that no template applies.

func (*Manager) Apply

func (m *Manager) Apply(cfg Config) error

Apply compiles cfg and atomically installs it. It returns an error (leaving the previous configuration in place) if any template fails to parse, so a bad template fails the config reload loudly rather than silently disabling alert URLs.

func (*Manager) GeneratorURL

func (m *Manager) GeneratorURL(alert *rules.Alert, expr, externalURL string) (url string, ok bool)

GeneratorURL renders the GeneratorURL for alert. ok is false when no template is configured for the alert or when rendering fails (the failure is logged), in which case the caller should fall back to the default GeneratorURL.

type Rule

type Rule struct {
	// MatchLabels must all be present on the alert with the given value for the
	// rule to match. An empty MatchLabels never matches (use Default for a
	// catch-all).
	MatchLabels map[string]string `yaml:"match_labels"`

	// Template is an inline template body or the name of a Named template.
	Template string `yaml:"template"`
}

Rule selects a template when every entry in MatchLabels equals the corresponding alert label.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL