Skip to main content

alert.go

alert.go - Overview

  1. 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.

  2. Detailed Documentation

Type Aliases:

  • AlertModel:
    • Purpose: Alias for models.Alert from the github.com/prometheus/alertmanager/api/v2/models package.
  • Alert:
    • Purpose: Alias for types.Alert from the github.com/prometheus/alertmanager/types package.
  • PostableAlert:
    • Purpose: Alias for models.PostableAlert from the github.com/prometheus/alertmanager/api/v2/models package.
  • PostableAlerts:
    • Purpose: Alias for []*models.PostableAlert.
  • GettableAlert:
    • Purpose: Alias for models.GettableAlert from the github.com/prometheus/alertmanager/api/v2/models package.
  • GettableAlerts:
    • Purpose: Alias for models.GettableAlerts.

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 of DeprecatedGettableAlert.
  • Parameters:
    • gettableAlerts: GettableAlerts - The slice of GettableAlert to convert.
  • Returns:
    • DeprecatedGettableAlerts: The converted slice of DeprecatedGettableAlert.

Function: NewPostableAlertsFromAlerts

  • Purpose: Converts a slice of Alert to a slice of PostableAlert.
  • Parameters:
    • alerts: []*types.Alert - The slice of Alert to convert.
  • Returns:
    • PostableAlerts: The converted slice of PostableAlert.

Function: NewAlertsFromPostableAlerts

  • Purpose: Converts a slice of PostableAlert to a slice of Alert.
  • Parameters:
    • postableAlerts: PostableAlerts - The slice of PostableAlert to convert.
    • resolveTimeout: time.Duration - Timeout duration for resolving alerts.
    • now: time.Time - The current time.
  • Returns:
    • []*types.Alert: The converted slice of Alert.
    • []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 created Alert object.

Function: NewGettableAlertsParams

  • Purpose: Creates a new GettableAlertsParams from an HTTP request.
  • Parameters:
    • req: *http.Request - The HTTP request.
  • Returns:
    • GettableAlertsParams: The created GettableAlertsParams.
    • 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 (type Config 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 of GettableAlert.
    • 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 of labels.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 of labels.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 of labels.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.
  1. 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.
  1. Clarity and Accuracy The documentation reflects the code accurately, avoiding assumptions.

Include in Getting Started: NO