app

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 45 Imported by: 0

Documentation

Overview

Package app contains the Alertmanager process logic extracted from cmd/alertmanager so that tests and other binaries can embed Alertmanager in-process instead of shelling out to a compiled binary. See https://github.com/prometheus/alertmanager/issues/406.

Index

Constants

View Source
const (
	DefaultConfigFile                  = "alertmanager.yml"
	DefaultDataDir                     = "data/"
	DefaultRetention                   = 120 * time.Hour
	DefaultMaintenanceInterval         = 15 * time.Minute
	DefaultAlertGCInterval             = 30 * time.Minute
	DefaultDispatchMaintenanceInterval = 30 * time.Second
)

Default storage and lifecycle values, mirroring the kingpin flag defaults in cmd/alertmanager/main.go so embedders that start from DefaultOptions behave like the binary.

View Source
const DefaultClusterAddr = "0.0.0.0:9094"

DefaultClusterAddr is the default listen address used when the operator does not pass --cluster.listen-address.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, opts Options) error

Run starts an Alertmanager instance using opts and blocks until ctx is cancelled or an unrecoverable error occurs. It is a thin wrapper over New + Start + serveLoop + Stop intended for callers that don't need the richer lifecycle API.

The deferred Stop also ensures cleanup runs on panic, matching the implicit panic-safety of the original defer-based implementation.

Types

type App

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

App is a running (or runnable) Alertmanager instance built from Options.

Compared to the top-level Run function, App exposes lifecycle hooks (Start, Stop, Addr, Reload) so callers — typically tests — can drive an instance without OS signals and discover the actually-bound HTTP address (useful when listening on ":0").

Construct an App with New, then call Start to begin serving HTTP. The caller is responsible for calling Stop, ideally via a deferred call so teardown also runs on panic. An App is single-use: calling Start more than once is an error.

func New

func New(opts Options) (*App, error)

New wires every Alertmanager subsystem according to opts but does not start serving HTTP yet. On error, partial setup is rolled back via the same cleanup stack that Stop would drain on success.

func (*App) Addr

func (a *App) Addr() string

Addr returns the address of the first bound listener, suitable for dialing a single-listener instance (the common case for tests that bind ":0"). Use Addrs if configured with multiple listen addresses.

func (*App) Addrs

func (a *App) Addrs() []string

Addrs returns all bound listener addresses in the order given by Options.WebConfig.WebListenAddresses.

func (*App) Reload

func (a *App) Reload() error

Reload triggers a configuration reload (the programmatic equivalent of SIGHUP). Safe to call concurrently with the running App. The reload is synchronous and not cancellable, so it takes no context.

It takes mtx for the duration of the reload so it cannot interleave with Stop: a reload swaps in (and starts) a new dispatcher/inhibitor, whereas Stop's cleanup tears the live ones down. Without this coupling a reload racing Stop could start a fresh dispatcher/inhibitor *after* Stop tore down the old ones, leaking those goroutines. Holding mtx also lets us refuse outright once Stop has begun. (The SIGHUP/HTTP reload paths route through reloadRouter, which Stop drains before running cleanups, so they are already safe; this guards the directly-callable entry point.)

func (*App) Start

func (a *App) Start() error

Start begins serving HTTP traffic on the listeners established by New. It returns immediately; the listen goroutine signals any error via the channel drained by serveLoop. Subsequent calls are no-ops.

func (*App) Stop

func (a *App) Stop(ctx context.Context) error

Stop gracefully shuts down the App, draining cleanups in reverse registration order so that teardown ordering matches the original defer chain in Run. Safe to call multiple times; safe to call before Start (it will then merely roll back what setup registered).

It returns an aggregated error combining the graceful HTTP shutdown failure (if any) with any errors returned by the teardown steps. Each failing step is also logged with its name; one failing step does not prevent the others from running.

type Options

type Options struct {
	// Storage and lifecycle.
	ConfigFile                  string
	DataDir                     string
	Retention                   time.Duration
	MaintenanceInterval         time.Duration
	MaxSilences                 int
	MaxSilenceSizeBytes         int
	SilenceLogging              bool
	AlertGCInterval             time.Duration
	PerAlertNameLimit           int
	DispatchMaintenanceInterval time.Duration
	DispatchStartDelay          time.Duration

	// Web server.
	WebConfig      *web.FlagConfig
	ExternalURL    string
	RoutePrefix    string
	GetConcurrency int
	HTTPTimeout    time.Duration

	// Cluster.
	ClusterBindAddr        string
	ClusterAdvertiseAddr   string
	ClusterPeerName        string
	Peers                  []string
	PeerTimeout            time.Duration
	PeersResolveTimeout    time.Duration
	GossipInterval         time.Duration
	PushPullInterval       time.Duration
	TCPTimeout             time.Duration
	ProbeTimeout           time.Duration
	ProbeInterval          time.Duration
	SettleTimeout          time.Duration
	ReconnectInterval      time.Duration
	PeerReconnectTimeout   time.Duration
	TLSConfigFile          string
	AllowInsecureAdvertise bool
	Label                  string

	// Injected dependencies.
	Logger     *slog.Logger
	Registerer prometheus.Registerer
	Flagger    featurecontrol.Flagger

	// Reload triggers a configuration reload each time it receives a
	// value. The binary translates SIGHUP into sends on this channel;
	// callers can also drive reloads programmatically. A nil channel
	// disables external reloads (the /-/reload HTTP endpoint still works).
	Reload <-chan struct{}
}

Options carries the resolved configuration for a single Alertmanager instance. Field names follow the kingpin flags in cmd/alertmanager/main.go so that mapping between the two is straightforward.

Logger, Registerer and Flagger are required dependencies; the remaining fields default to their zero value (which generally matches the kingpin flag default).

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns an Options value pre-populated with the same defaults as the cmd/alertmanager kingpin flags. Clustering is disabled (ClusterBindAddr empty) because enabling a gossip listener by default would surprise embedders; the cluster timeouts are still seeded so that setting ClusterBindAddr is all that's needed to enable HA.

Callers must still supply the required dependencies (Logger, Registerer, Flagger) and a WebConfig before passing the result to New or Run.

Jump to

Keyboard shortcuts

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