silence

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 35 Imported by: 163

Documentation

Overview

Package silence provides a storage for silences, which can share its state over a mesh network and snapshot it.

Copyright The Prometheus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidState = errors.New("invalid state")

ErrInvalidState is returned if the state isn't valid.

View Source
var ErrNotFound = errors.New("silence not found")

ErrNotFound is returned if a silence was not found.

Functions

func HasLabelMatchers added in v0.35.0

func HasLabelMatchers(sil *pb.Silence, matchers []*labels.Matcher) bool

HasLabelMatchers reports whether one stored matcher set contains every requested label matcher with the same name, type, and value. It compares definitions without evaluating label values. A silence with no matcher sets does not match. Nil matchers do not match.

func HasMatchers added in v0.35.0

func HasMatchers(sil *pb.Silence, sets ...*pb.MatcherSet) bool

HasMatchers reports whether one stored matcher set contains every matcher from one of the requested sets. Matchers must have the same name, type, and pattern. No requested sets means every silence matches. A nil or empty requested set also matches every silence. Nil matchers do not match.

func ValidateMatcher added in v0.24.0

func ValidateMatcher(m *pb.Matcher) error

ValidateMatcher applies the silence store's rules for matcher definitions. Label names follow the configured matcher parser mode.

Types

type Limits added in v0.28.0

type Limits struct {
	// MaxSilences limits the maximum number of silences, including expired
	// silences.
	MaxSilences func() int
	// MaxSilenceSizeBytes is the maximum size of an individual silence as
	// stored on disk.
	MaxSilenceSizeBytes func() int
}

Limits contains the limits for silences.

type MaintenanceFunc added in v0.24.0

type MaintenanceFunc func() (int64, error)

MaintenanceFunc represents the function to run as part of the periodic maintenance for silences. It returns the size of the snapshot taken or an error if it failed.

type Options

type Options struct {
	// A snapshot file or reader from which the initial state is loaded.
	// None or only one of them must be set.
	SnapshotFile   string
	SnapshotReader io.Reader

	// Retention time for newly created Silences. Silences may be
	// garbage collected after the given duration after they ended.
	Retention time.Duration
	Limits    Limits

	Logging bool

	// A logger used by background processing.
	Logger  *slog.Logger
	Metrics prometheus.Registerer

	// EventRecorder records silence-related events to the event recorder.
	EventRecorder eventrecorder.Recorder
}

Options exposes configuration options for creating a new Silences object. Its zero value is a safe default.

type QueryParam

type QueryParam func(*query) error

QueryParam expresses parameters along which silences are queried.

func QHasMatchers added in v0.35.0

func QHasMatchers(sets ...*pb.MatcherSet) QueryParam

QHasMatchers filters silences by matcher definitions using HasMatchers. It compares names, types, and patterns without evaluating label values.

func QIDs

func QIDs(ids ...string) QueryParam

QIDs configures a query to select the given silence IDs.

func QMatches

func QMatches(set model.LabelSet) QueryParam

QMatches returns silences that match the given label set.

func QSince added in v0.30.0

func QSince(version int) QueryParam

QSince filters silences to those created after the provided version. This can be used to scan all silences which have been added after the provided version to incrementally update a cache.

func QState

func QState(states ...SilenceState) QueryParam

QState filters queried silences by the given states.

type SilenceState

type SilenceState string
const (
	SilenceStateActive  SilenceState = "active"
	SilenceStateExpired SilenceState = "expired"
	SilenceStatePending SilenceState = "pending"
)

func CurrentState added in v0.32.0

func CurrentState(start, end time.Time) SilenceState

CurrentState returns the SilenceState that a silence with the given start and end time would have right now.

func StateAt added in v0.35.0

func StateAt(start, end, at time.Time) SilenceState

StateAt returns the silence state at the given time. The start is inclusive. The end is exclusive.

func StateFromProtoAt added in v0.35.0

func StateFromProtoAt(sil *silencepb.Silence, at time.Time) SilenceState

StateFromProtoAt returns the silence state at the given time.

type Silencer added in v0.17.0

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

Silencer holds Silences and implements the Muter interface.

func NewSilencer added in v0.17.0

func NewSilencer(silences *Silences, logger *slog.Logger, recorder eventrecorder.Recorder) *Silencer

NewSilencer returns a new Silencer.

func (*Silencer) Mutes added in v0.17.0

func (s *Silencer) Mutes(ctx context.Context, lset labelset.LabelSet) bool

Mutes implements the Muter interface.

func (*Silencer) PostDelete added in v0.32.0

func (s *Silencer) PostDelete(_ *alert.Alert)

func (*Silencer) PostGC added in v0.32.0

func (s *Silencer) PostGC(ff model.Fingerprints)

func (*Silencer) PostStore added in v0.32.0

func (s *Silencer) PostStore(_ *alert.Alert, _ bool)

func (*Silencer) PreStore added in v0.32.0

func (s *Silencer) PreStore(_ *alert.Alert, _ bool) error

The following methods implement mem.AlertStoreCallback.

type Silences

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

Silences holds a silence state that can be modified, queried, and snapshot.

func New

func New(o Options) (*Silences, error)

New returns a new Silences object with the given configuration.

func (*Silences) CountState added in v0.10.0

func (s *Silences) CountState(ctx context.Context, states ...SilenceState) (int, error)

CountState counts silences by state.

func (*Silences) Expire

func (s *Silences) Expire(ctx context.Context, id string) error

Expire the silence with the given ID immediately.

func (*Silences) GC

func (s *Silences) GC() (int, error)

GC runs a garbage collection that removes silences that have ended longer than the configured retention time ago.

func (*Silences) Maintenance

func (s *Silences) Maintenance(interval time.Duration, snapf string, stopc <-chan struct{}, override MaintenanceFunc)

Maintenance garbage collects the silence state at the given interval. If the snapshot file is set, a snapshot is written to it afterwards. Terminates on receiving from stopc. If not nil, the last argument is an override for what to do as part of the maintenance - for advanced usage.

func (*Silences) MarshalBinary added in v0.15.0

func (s *Silences) MarshalBinary() ([]byte, error)

MarshalBinary serializes all silences.

func (*Silences) Merge added in v0.15.0

func (s *Silences) Merge(b []byte) error

Merge merges silence state received from the cluster with the local state.

func (*Silences) Query

func (s *Silences) Query(ctx context.Context, params ...QueryParam) ([]*pb.Silence, int, error)

Query for silences based on the given query parameters. It returns the resulting silences and the state version the result is based on.

func (*Silences) QueryAt added in v0.35.0

func (s *Silences) QueryAt(ctx context.Context, at time.Time, params ...QueryParam) ([]*pb.Silence, int, error)

QueryAt evaluates query filters at the given time. It reads the current stored records, not a historical snapshot.

func (*Silences) QueryOne added in v0.7.0

func (s *Silences) QueryOne(ctx context.Context, params ...QueryParam) (*pb.Silence, error)

QueryOne queries with the given parameters and returns the first result. Returns ErrNotFound if the query result is empty.

func (*Silences) Set added in v0.7.0

func (s *Silences) Set(ctx context.Context, sil *pb.Silence) error

Set the specified silence. If a silence with the ID already exists and the modification modifies history, the old silence gets expired and a new one is created.

func (*Silences) SetBroadcast added in v0.15.0

func (s *Silences) SetBroadcast(f func([]byte))

SetBroadcast sets the provided function as the one creating data to be broadcast.

func (*Silences) Snapshot

func (s *Silences) Snapshot(w io.Writer) (int64, error)

Snapshot writes the full internal state into the writer and returns the number of bytes written.

func (*Silences) Version added in v0.17.0

func (s *Silences) Version() int

Version of the silence state.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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