alertmanager

package
v0.0.0-...-9d732f8 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: AGPL-3.0 Imports: 100 Imported by: 0

Documentation

Overview

Mostly taken from http://github.com/grafana/grafana/main/pkg/services/notifications/webhook.go

Index

Constants

View Source
const (
	// RingKey is the key under which we store the alertmanager ring in the KVStore.
	RingKey = "alertmanager"

	// RingNameForServer is the name of the ring used by the alertmanager server.
	RingNameForServer = "alertmanager"

	// RingNumTokens is a safe default instead of exposing to config option to the user
	// in order to simplify the config.
	RingNumTokens = 128
)

Variables

View Source
var ErrInvalidMethod = errors.New("webhook only supports HTTP methods PUT or POST")
View Source
var RingOp = ring.NewOp([]ring.InstanceState{ring.ACTIVE}, func(s ring.InstanceState) bool {

	return s != ring.ACTIVE
})

RingOp is the operation used for reading/writing to the alertmanagers.

View Source
var SyncRingOp = ring.NewOp([]ring.InstanceState{ring.ACTIVE, ring.JOINING}, func(s ring.InstanceState) bool {
	return s != ring.ACTIVE
})

SyncRingOp is the operation used for checking if a user is owned by an alertmanager.

Functions

func ComputeFallbackConfig

func ComputeFallbackConfig(fallbackConfigFile string) ([]byte, error)

ComputeFallbackConfig will load, validate and return the provided fallbackConfigFile or return an valid empty default configuration if none is provided.

func WithCustomFunctions

func WithCustomFunctions(userID string) template.Option

WithCustomFunctions returns template.Option which adds additional template functions to the default ones.

Types

type Alertmanager

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

An Alertmanager manages the alerts for one user.

func New

func New(cfg *Config, reg *prometheus.Registry) (*Alertmanager, error)

New creates a new Alertmanager.

func (*Alertmanager) ApplyConfig

func (am *Alertmanager) ApplyConfig(conf *definition.PostableApiAlertingConfig, tmpls []alertingTemplates.TemplateDefinition, rawCfg string, tmplExternalURL *url.URL, staticHeaders map[string]string) error

ApplyConfig applies a new configuration to an Alertmanager.

func (*Alertmanager) GetReceiversHandler

func (am *Alertmanager) GetReceiversHandler(w http.ResponseWriter, _ *http.Request)

func (*Alertmanager) Stop

func (am *Alertmanager) Stop()

Stop stops the Alertmanager.

func (*Alertmanager) StopAndWait

func (am *Alertmanager) StopAndWait()

func (*Alertmanager) TestReceiversHandler

func (am *Alertmanager) TestReceiversHandler(w http.ResponseWriter, r *http.Request)

func (*Alertmanager) TestTemplatesHandler

func (am *Alertmanager) TestTemplatesHandler(w http.ResponseWriter, r *http.Request)

func (*Alertmanager) WaitInitialStateSync

func (am *Alertmanager) WaitInitialStateSync(ctx context.Context) error

type Client

type Client interface {
	alertmanagerpb.AlertmanagerClient

	// RemoteAddress returns the address of the remote alertmanager and is used to uniquely
	// identify an alertmanager instance.
	RemoteAddress() string
}

Client is the interface that should be implemented by any client used to read/write data to an alertmanager via GRPC.

type ClientConfig

type ClientConfig struct {
	RemoteTimeout    time.Duration     `yaml:"remote_timeout" category:"advanced"`
	GRPCClientConfig grpcclient.Config `yaml:",inline"`
}

ClientConfig is the configuration struct for the alertmanager client.

func (*ClientConfig) RegisterFlagsWithPrefix

func (cfg *ClientConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)

RegisterFlagsWithPrefix registers flags with prefix.

type ClientsPool

type ClientsPool interface {
	// GetClientFor returns the alertmanager client for the given address.
	GetClientFor(addr string) (Client, error)
}

ClientsPool is the interface used to get the client from the pool for a specified address.

type Config

type Config struct {
	UserID                            string
	Logger                            log.Logger
	PeerTimeout                       time.Duration
	Retention                         time.Duration
	MaxConcurrentGetRequestsPerTenant int
	ExternalURL                       *url.URL
	Limits                            Limits
	Features                          featurecontrol.Flagger

	// Tenant-specific local directory where AM can store its state (notifications, silences, templates). When AM is stopped, entire dir is removed.
	TenantDataDir string

	ShardingEnabled   bool
	ReplicationFactor int
	Replicator        Replicator
	Store             alertstore.AlertStore
	PersisterConfig   PersisterConfig

	GrafanaAlertmanagerCompatibility bool
}

Config configures an Alertmanager.

type Distributor

type Distributor struct {
	services.Service
	// contains filtered or unexported fields
}

Distributor forwards requests to individual alertmanagers.

func NewDistributor

func NewDistributor(cfg ClientConfig, maxRecvMsgSize int64, alertmanagersRing *ring.Ring, alertmanagerClientsPool ClientsPool, logger log.Logger, reg prometheus.Registerer) (d *Distributor, err error)

NewDistributor constructs a new Distributor

func (*Distributor) DistributeRequest

func (d *Distributor) DistributeRequest(w http.ResponseWriter, r *http.Request)

DistributeRequest shards the writes and returns as soon as the quorum is satisfied. In case of reads, it proxies the request to one of the alertmanagers.

type GrafanaAlertmanagerConfig

type GrafanaAlertmanagerConfig struct {
	Templates          map[string]string                    `json:"template_files"`
	AlertmanagerConfig definition.PostableApiAlertingConfig `json:"alertmanager_config"`
}

type Limits

type Limits interface {
	// AlertmanagerReceiversBlockCIDRNetworks returns the list of network CIDRs that should be blocked
	// in the Alertmanager receivers for the given user.
	AlertmanagerReceiversBlockCIDRNetworks(user string) []flagext.CIDR

	// AlertmanagerReceiversBlockPrivateAddresses returns true if private addresses should be blocked
	// in the Alertmanager receivers for the given user.
	AlertmanagerReceiversBlockPrivateAddresses(user string) bool

	// NotificationRateLimit methods return limit used by rate-limiter for given integration.
	// If set to 0, no notifications are allowed.
	// rate.Inf = all notifications are allowed.
	//
	// Note that when negative or zero values specified by user are translated to rate.Limit by Overrides,
	// and may have different meaning there.
	NotificationRateLimit(tenant string, integration string) rate.Limit

	// NotificationBurstSize returns burst-size for rate limiter for given integration type. If 0, no notifications are allowed except
	// when limit == rate.Inf.
	NotificationBurstSize(tenant string, integration string) int

	// AlertmanagerMaxGrafanaConfigSize returns max size of the grafana configuration file that user is allowed to upload. If 0, there is no limit.
	AlertmanagerMaxGrafanaConfigSize(tenant string) int

	// AlertmanagerMaxConfigSize returns max size of configuration file that user is allowed to upload. If 0, there is no limit.
	AlertmanagerMaxConfigSize(tenant string) int

	// AlertmanagerMaxGrafanaStateSize returns the max size of the grafana state in bytes. If 0, there is no limit.
	AlertmanagerMaxGrafanaStateSize(tenant string) int

	// AlertmanagerMaxSilencesCount returns the max number of silences, including expired silences. If negative or 0, there is no limit.
	AlertmanagerMaxSilencesCount(tenant string) int

	// AlertmanagerMaxSilenceSizeBytes returns the max silence size in bytes. If negative or 0, there is no limit.
	AlertmanagerMaxSilenceSizeBytes(tenant string) int

	// AlertmanagerMaxTemplatesCount returns max number of templates that tenant can use in the configuration. 0 = no limit.
	AlertmanagerMaxTemplatesCount(tenant string) int

	// AlertmanagerMaxTemplateSize returns max size of individual template. 0 = no limit.
	AlertmanagerMaxTemplateSize(tenant string) int

	// AlertmanagerMaxDispatcherAggregationGroups returns maximum number of aggregation groups in Alertmanager's dispatcher that a tenant can have.
	// Each aggregation group consumes single goroutine. 0 = unlimited.
	AlertmanagerMaxDispatcherAggregationGroups(t string) int

	// AlertmanagerMaxAlertsCount returns max number of alerts that tenant can have active at the same time. 0 = no limit.
	AlertmanagerMaxAlertsCount(tenant string) int

	// AlertmanagerMaxAlertsSizeBytes returns total max size of alerts that tenant can have active at the same time. 0 = no limit.
	// Size of the alert is computed from alert labels, annotations and generator URL.
	AlertmanagerMaxAlertsSizeBytes(tenant string) int
}

Limits defines limits used by Alertmanager.

type MultitenantAlertmanager

type MultitenantAlertmanager struct {
	services.Service
	// contains filtered or unexported fields
}

A MultitenantAlertmanager manages Alertmanager instances for multiple organizations.

func NewMultitenantAlertmanager

func NewMultitenantAlertmanager(cfg *MultitenantAlertmanagerConfig, store alertstore.AlertStore, limits Limits, features featurecontrol.Flagger, logger log.Logger, registerer prometheus.Registerer) (*MultitenantAlertmanager, error)

NewMultitenantAlertmanager creates a new MultitenantAlertmanager.

func (*MultitenantAlertmanager) DeleteUserConfig

func (am *MultitenantAlertmanager) DeleteUserConfig(w http.ResponseWriter, r *http.Request)

DeleteUserConfig is exposed via user-visible API (if enabled, uses DELETE method), but also as an internal endpoint using POST method. Note that if no config exists for a user, StatusOK is returned.

func (*MultitenantAlertmanager) DeleteUserGrafanaConfig

func (am *MultitenantAlertmanager) DeleteUserGrafanaConfig(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) DeleteUserGrafanaState

func (am *MultitenantAlertmanager) DeleteUserGrafanaState(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) GetPositionForUser

func (am *MultitenantAlertmanager) GetPositionForUser(userID string) int

GetPositionForUser returns the position this Alertmanager instance holds in the ring related to its other replicas for an specific user.

func (*MultitenantAlertmanager) GetUserConfig

func (am *MultitenantAlertmanager) GetUserConfig(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) GetUserGrafanaConfig

func (am *MultitenantAlertmanager) GetUserGrafanaConfig(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) GetUserGrafanaState

func (am *MultitenantAlertmanager) GetUserGrafanaState(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) HandleRequest

HandleRequest implements gRPC Alertmanager service, which receives request from AlertManager-Distributor.

func (*MultitenantAlertmanager) ListAllConfigs

func (am *MultitenantAlertmanager) ListAllConfigs(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) ReadFullStateForUser

func (am *MultitenantAlertmanager) ReadFullStateForUser(ctx context.Context, userID string) ([]*clusterpb.FullState, error)

ReadFullStateForUser attempts to read the full state from each replica for user. Note that it will try to obtain and return state from all replicas, but will consider it a success if state is obtained from at least one replica.

func (*MultitenantAlertmanager) ReadState

ReadState implements the Alertmanager service.

func (*MultitenantAlertmanager) ReplicateStateForUser

func (am *MultitenantAlertmanager) ReplicateStateForUser(ctx context.Context, userID string, part *clusterpb.Part) error

ReplicateStateForUser attempts to replicate a partial state sent by an alertmanager to its other replicas through the ring.

func (*MultitenantAlertmanager) RingHandler

func (am *MultitenantAlertmanager) RingHandler(w http.ResponseWriter, req *http.Request)

func (*MultitenantAlertmanager) ServeHTTP

func (am *MultitenantAlertmanager) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP serves the Alertmanager's web UI and API.

func (*MultitenantAlertmanager) SetUserConfig

func (am *MultitenantAlertmanager) SetUserConfig(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) SetUserGrafanaConfig

func (am *MultitenantAlertmanager) SetUserGrafanaConfig(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) SetUserGrafanaState

func (am *MultitenantAlertmanager) SetUserGrafanaState(w http.ResponseWriter, r *http.Request)

func (*MultitenantAlertmanager) StatusHandler

func (am *MultitenantAlertmanager) StatusHandler(w http.ResponseWriter, _ *http.Request)

StatusHandler serves the status of the alertmanager.

func (*MultitenantAlertmanager) UpdateState

UpdateState implements the Alertmanager service.

type MultitenantAlertmanagerConfig

type MultitenantAlertmanagerConfig struct {
	DataDir        string           `yaml:"data_dir"`
	Retention      time.Duration    `yaml:"retention" category:"advanced"`
	ExternalURL    flagext.URLValue `yaml:"external_url"`
	PollInterval   time.Duration    `yaml:"poll_interval" category:"advanced"`
	MaxRecvMsgSize int64            `yaml:"max_recv_msg_size" category:"advanced"`

	// Sharding confiuration for the Alertmanager
	ShardingRing RingConfig `yaml:"sharding_ring"`

	FallbackConfigFile string `yaml:"fallback_config_file"`

	PeerTimeout time.Duration `yaml:"peer_timeout" category:"advanced"`

	EnableAPI                               bool `yaml:"enable_api" category:"advanced"`
	GrafanaAlertmanagerCompatibilityEnabled bool `yaml:"grafana_alertmanager_compatibility_enabled" category:"experimental"`

	MaxConcurrentGetRequestsPerTenant int `yaml:"max_concurrent_get_requests_per_tenant" category:"advanced"`

	// For distributor.
	AlertmanagerClient ClientConfig `yaml:"alertmanager_client"`

	// For the state persister.
	Persister PersisterConfig `yaml:",inline"`

	// Allow disabling of full_state object cleanup.
	EnableStateCleanup bool `yaml:"enable_state_cleanup" category:"advanced"`

	// Enable UTF-8 strict mode. When enabled, Alertmanager uses the new UTF-8 parser
	// when parsing label matchers in tenant configurations and HTTP requests, instead
	// of the old regular expression parser, referred to as classic mode.
	// Enable this mode once confident that all tenant configurations are forwards
	// compatible.
	UTF8StrictMode bool `yaml:"utf8_strict_mode" category:"experimental"`
	// Enables logging when parsing label matchers. If UTF-8 strict mode is enabled,
	// then the UTF-8 parser will be logged. If it is disabled, then the old regular
	// expression parser will be logged.
	LogParsingLabelMatchers bool `yaml:"log_parsing_label_matchers" category:"experimental"`
	UTF8MigrationLogging    bool `yaml:"utf8_migration_logging" category:"experimental"`
}

MultitenantAlertmanagerConfig is the configuration for a multitenant Alertmanager.

func (*MultitenantAlertmanagerConfig) CheckExternalURL

func (cfg *MultitenantAlertmanagerConfig) CheckExternalURL(alertmanagerHTTPPrefix string, logger log.Logger)

func (*MultitenantAlertmanagerConfig) RegisterFlags

func (cfg *MultitenantAlertmanagerConfig) RegisterFlags(f *flag.FlagSet, logger log.Logger)

RegisterFlags adds the features required to config this to the given FlagSet.

func (*MultitenantAlertmanagerConfig) Validate

func (cfg *MultitenantAlertmanagerConfig) Validate() error

Validate config and returns error on failure

type NilPeer

type NilPeer struct{}

NilPeer implements the Alertmanager cluster.ClusterPeer interface used by the API to expose cluster information. In a multi-tenant environment, we choose not to expose these to tenants and thus are not implemented.

func (*NilPeer) Name

func (p *NilPeer) Name() string

func (*NilPeer) Peers

func (p *NilPeer) Peers() []cluster.ClusterMember

func (*NilPeer) Status

func (p *NilPeer) Status() string

type PersistableState

type PersistableState interface {
	State
	GetFullState() (*clusterpb.FullState, error)
}

type PersisterConfig

type PersisterConfig struct {
	Interval time.Duration `yaml:"persist_interval" category:"advanced"`
}

func (*PersisterConfig) RegisterFlagsWithPrefix

func (cfg *PersisterConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)

func (*PersisterConfig) Validate

func (cfg *PersisterConfig) Validate() error

type PostableUserGrafanaState

type PostableUserGrafanaState struct {
	UserGrafanaState
	Promoted bool `json:"promoted"`
}

func (*PostableUserGrafanaState) UnmarshalJSON

func (gs *PostableUserGrafanaState) UnmarshalJSON(data []byte) error

type Replicator

type Replicator interface {
	// ReplicateStateForUser writes the given partial state to the necessary replicas.
	ReplicateStateForUser(ctx context.Context, userID string, part *clusterpb.Part) error
	// The alertmanager replication protocol relies on a position related to other replicas.
	// This position is then used to identify who should notify about the alert first.
	GetPositionForUser(userID string) int
	// ReadFullStateForUser obtains the full state from other replicas in the cluster.
	// If all the replicas were successfully contacted, but the user was not found in
	// all the replicas, then errAllReplicasUserNotFound is returned.
	ReadFullStateForUser(context.Context, string) ([]*clusterpb.FullState, error)
}

Replicator is used to exchange state with peers via the ring when sharding is enabled.

type RingConfig

type RingConfig struct {
	// Common ring config used across components
	Common util.CommonRingConfig `yaml:",inline"`

	// Configuration specific to alertmanager rings
	ReplicationFactor    int    `yaml:"replication_factor" category:"advanced"`
	ZoneAwarenessEnabled bool   `yaml:"zone_awareness_enabled" category:"advanced"`
	InstanceZone         string `yaml:"instance_availability_zone" category:"advanced"`

	// Used for testing
	RingCheckPeriod time.Duration `yaml:"-"`
	SkipUnregister  bool          `yaml:"-"`
}

RingConfig masks the ring lifecycler config which contains many options not really required by the alertmanager ring. This config is used to strip down the config to the minimum, and avoid confusion to the user.

func (*RingConfig) RegisterFlags

func (cfg *RingConfig) RegisterFlags(f *flag.FlagSet, logger log.Logger)

RegisterFlags adds the flags required to config this to the given FlagSet

func (*RingConfig) ToLifecyclerConfig

func (cfg *RingConfig) ToLifecyclerConfig(logger log.Logger) (ring.BasicLifecyclerConfig, error)

ToLifecyclerConfig returns a LifecyclerConfig based on the alertmanager ring config.

type Sender

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

func NewSender

func NewSender(log log.Logger) *Sender

func (*Sender) SendWebhook

SendWebhook implements alertingReceivers.WebhookSender.

type State

type State interface {
	AddState(string, cluster.State, prometheus.Registerer) cluster.ClusterChannel
	Position() int
	WaitReady(context.Context) error
}

State helps with replication and synchronization of notifications and silences across several alertmanager replicas.

type UserConfig

type UserConfig struct {
	TemplateFiles      map[string]string `yaml:"template_files"`
	AlertmanagerConfig string            `yaml:"alertmanager_config"`
}

UserConfig is used to communicate a users alertmanager configs

type UserGrafanaConfig

type UserGrafanaConfig struct {
	GrafanaAlertmanagerConfig GrafanaAlertmanagerConfig `json:"configuration"`
	Hash                      string                    `json:"configuration_hash"`
	CreatedAt                 int64                     `json:"created"`
	Default                   bool                      `json:"default"`
	Promoted                  bool                      `json:"promoted"`
	ExternalURL               string                    `json:"external_url"`
	StaticHeaders             map[string]string         `json:"static_headers"`
}

func (*UserGrafanaConfig) UnmarshalJSON

func (gc *UserGrafanaConfig) UnmarshalJSON(data []byte) error

func (*UserGrafanaConfig) Validate

func (gc *UserGrafanaConfig) Validate() error

type UserGrafanaState

type UserGrafanaState struct {
	State string `json:"state"`
}

func (*UserGrafanaState) Validate

func (gs *UserGrafanaState) Validate() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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