alert.go
alert.go - Overview
-
Overview This file defines types and functions for handling alerts, including conversion between different alert representations (internal, postable, gettable), filtering, and validation. It provides compatibility with the Alertmanager API and Prometheus data models.
-
Detailed Documentation
Type Aliases:
AlertModel
:- Purpose: Alias for
models.Alert
from thegithub.com/prometheus/alertmanager/api/v2/models
package.
- Purpose: Alias for
Alert
:- Purpose: Alias for
types.Alert
from thegithub.com/prometheus/alertmanager/types
package.
- Purpose: Alias for
PostableAlert
:- Purpose: Alias for
models.PostableAlert
from thegithub.com/prometheus/alertmanager/api/v2/models
package.
- Purpose: Alias for
PostableAlerts
:- Purpose: Alias for
[]*models.PostableAlert
.
- Purpose: Alias for
GettableAlert
:- Purpose: Alias for
models.GettableAlert
from thegithub.com/prometheus/alertmanager/api/v2/models
package.
- Purpose: Alias for
GettableAlerts
:- Purpose: Alias for
models.GettableAlerts
.
- Purpose: Alias for
Type: DeprecatedGettableAlert
- Purpose: Represents a gettable alert with a structure compatible with older versions.
- Fields:
Alert
:*model.Alert
- The core alert information.Status
:types.AlertStatus
- The status of the alert.Receivers
:[]string
- List of receiver names.Fingerprint
:string
- Unique identifier for the alert.
Type: DeprecatedGettableAlerts
- Purpose: Represents a slice of
DeprecatedGettableAlert
.
Type: GettableAlertsParams
- Purpose: Encapsulates parameters for fetching gettable alerts, including filter and raw query.
- Fields:
GetAlertsParams
:alert.GetAlertsParams
- Parameters from the Alertmanager API.RawQuery
:string
- Raw query string from the HTTP request.
Function: NewDeprecatedGettableAlertsFromGettableAlerts
- Purpose: Converts a slice of
GettableAlert
to a slice ofDeprecatedGettableAlert
. - Parameters:
gettableAlerts
:GettableAlerts
- The slice ofGettableAlert
to convert.
- Returns:
DeprecatedGettableAlerts
: The converted slice ofDeprecatedGettableAlert
.
Function: NewPostableAlertsFromAlerts
- Purpose: Converts a slice of
Alert
to a slice ofPostableAlert
. - Parameters:
alerts
:[]*types.Alert
- The slice ofAlert
to convert.
- Returns:
PostableAlerts
: The converted slice ofPostableAlert
.
Function: NewAlertsFromPostableAlerts
- Purpose: Converts a slice of
PostableAlert
to a slice ofAlert
. - Parameters:
postableAlerts
:PostableAlerts
- The slice ofPostableAlert
to convert.resolveTimeout
:time.Duration
- Timeout duration for resolving alerts.now
:time.Time
- The current time.
- Returns:
[]*types.Alert
: The converted slice ofAlert
.[]error
: A slice of errors encountered during validation.
Function: NewTestAlert
- Purpose: Creates a new test alert.
- Parameters:
receiver
:Receiver
- The receiver for the alert.Receiver
type is not defined in the provided code.startsAt
:time.Time
- The start time of the alert.updatedAt
:time.Time
- The last updated time of the alert.
- Returns:
*Alert
: A pointer to the newly createdAlert
object.
Function: NewGettableAlertsParams
- Purpose: Creates a new
GettableAlertsParams
from an HTTP request. - Parameters:
req
:*http.Request
- The HTTP request.
- Returns:
GettableAlertsParams
: The createdGettableAlertsParams
.error
: An error if binding the request fails.
Function: NewGettableAlertsFromAlertProvider
- Purpose: Retrieves and filters alerts from an
Alerts
provider. - Parameters:
alerts
:provider.Alerts
- The alert provider.cfg
:*Config
- Configuration object (typeConfig
not defined in the code).getAlertStatusFunc
:func(model.Fingerprint) types.AlertStatus
- Function to get the status of an alert.setAlertStatusFunc
:func(model.LabelSet)
- Function to set the status of an alert.params
:GettableAlertsParams
- Parameters for filtering alerts.
- Returns:
GettableAlerts
: The filtered slice ofGettableAlert
.error
: An error if parsing the filter or retrieving alerts fails.
Function: alertFilter
- Purpose: Creates a filter function for alerts based on status, labels, and other criteria.
- Parameters:
getAlertStatusFunc
:func(model.Fingerprint) types.AlertStatus
- Function to get alert status.setAlertStatusFunc
:func(model.LabelSet)
- Function to set alert status.matchers
:[]*labels.Matcher
- List of label matchers.silenced
:bool
- Whether to include silenced alerts.inhibited
:bool
- Whether to include inhibited alerts.active
:bool
- Whether to include active alerts.
- Returns:
func(a *types.Alert, now time.Time) bool
: A filter function that takes an alert and the current time as input and returns a boolean indicating whether the alert should be included.
Function: parseFilter
- Purpose: Parses a slice of filter strings into a slice of
labels.Matcher
. - Parameters:
filter
:[]string
- The slice of filter strings.
- Returns:
[]*labels.Matcher
: The parsed slice oflabels.Matcher
.error
: An error if parsing a matcher fails.
Function: alertMatchesFilterLabels
- Purpose: Checks if an alert's labels match a set of filter labels.
- Parameters:
a
:*model.Alert
- The alert to check.matchers
:[]*labels.Matcher
- The slice oflabels.Matcher
to use for filtering.
- Returns:
bool
: True if the alert's labels match the filter labels, false otherwise.
Function: matchFilterLabels
- Purpose: Checks if a map of labels matches a set of filter labels.
- Parameters:
matchers
:[]*labels.Matcher
- The slice oflabels.Matcher
to use for filtering.sms
:map[string]string
- The map of labels to check.
- Returns:
bool
: True if the labels match the filter labels, false otherwise.
Function: receiversMatchFilter
- Purpose: Checks if any of the receivers match the provided filter.
- Parameters:
receivers
:[]string
- Slice of receiver names.filter
:*regexp.Regexp
- Regular expression to match against receiver names.
- Returns:
bool
:true
if any receiver matches the filter,false
otherwise.
- Code Examples
// Example of creating a new test alert:
const startsAt = new Date();
const updatedAt = new Date();
// Assuming 'receiver' is an instance of a Receiver struct (not defined in this code). Replace with a valid Receiver instance.
// const receiver = new Receiver{Name: "test-receiver"};
// const testAlert = NewTestAlert(receiver, startsAt, updatedAt);
// This example will not work without a valid Receiver instance.
- Clarity and Accuracy The documentation reflects the code accurately, avoiding assumptions.
Include in Getting Started: NO