Skip to content

Commit

Permalink
Show muted alerts in the Alert Groups API
Browse files Browse the repository at this point in the history
This commit updates /api/v2/alerts/groups to show if an alert is
suppressed from one or more active or mute time intervals. It does
not update the /api/v2/alerts API because that API does not
take aggregation or routing into consideration.

It also updates the UI to support filtering Muted alerts via the
Muted checkbox.

Signed-off-by: George Robinson <george.robinson@grafana.com>
  • Loading branch information
grobinson-grafana committed May 13, 2024
1 parent c4a763c commit 0cd1216
Show file tree
Hide file tree
Showing 36 changed files with 1,309 additions and 81 deletions.
19 changes: 13 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ type API struct {
inFlightSem chan struct{}
}

// Options for the creation of an API object. Alerts, Silences, and StatusFunc
// Options for the creation of an API object. Alerts, Silences, and AlertStatusFunc
// are mandatory to set. The zero value for everything else is a safe default.
type Options struct {
// Alerts to be used by the API. Mandatory.
Alerts provider.Alerts
// Silences to be used by the API. Mandatory.
Silences *silence.Silences
// StatusFunc is used be the API to retrieve the AlertStatus of an
// AlertStatusFunc is used be the API to retrieve the AlertStatus of an
// alert. Mandatory.
StatusFunc func(model.Fingerprint) types.AlertStatus
AlertStatusFunc func(model.Fingerprint) types.AlertStatus
// GroupMutedFunc is used be the API to know if an alert is muted.
// Mandatory.
GroupMutedFunc func(routeID, groupKey string) ([]string, bool)
// Peer from the gossip cluster. If nil, no clustering will be used.
Peer cluster.ClusterPeer
// Timeout for all HTTP connections. The zero value (and negative
Expand Down Expand Up @@ -83,8 +86,11 @@ func (o Options) validate() error {
if o.Silences == nil {
return errors.New("mandatory field Silences not set")
}
if o.StatusFunc == nil {
return errors.New("mandatory field StatusFunc not set")
if o.AlertStatusFunc == nil {
return errors.New("mandatory field AlertStatusFunc not set")
}
if o.GroupMutedFunc == nil {
return errors.New("mandatory field GroupMutedFunc not set")
}
if o.GroupFunc == nil {
return errors.New("mandatory field GroupFunc not set")
Expand Down Expand Up @@ -113,7 +119,8 @@ func New(opts Options) (*API, error) {
v2, err := apiv2.NewAPI(
opts.Alerts,
opts.GroupFunc,
opts.StatusFunc,
opts.AlertStatusFunc,
opts.GroupMutedFunc,
opts.Silences,
opts.Peer,
log.With(l, "version", "v2"),
Expand Down
17 changes: 13 additions & 4 deletions api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type API struct {
alerts provider.Alerts
alertGroups groupsFn
getAlertStatus getAlertStatusFn
groupMutedFunc groupMutedFunc
uptime time.Time

// mtx protects alertmanagerConfig, setAlertStatus and route.
Expand All @@ -78,6 +79,7 @@ type API struct {

type (
groupsFn func(func(*dispatch.Route) bool, func(*types.Alert, time.Time) bool) (dispatch.AlertGroups, map[prometheus_model.Fingerprint][]string)
groupMutedFunc func(routeID, groupKey string) ([]string, bool)
getAlertStatusFn func(prometheus_model.Fingerprint) types.AlertStatus
setAlertStatusFn func(prometheus_model.LabelSet)
)
Expand All @@ -86,16 +88,18 @@ type (
func NewAPI(
alerts provider.Alerts,
gf groupsFn,
sf getAlertStatusFn,
asf getAlertStatusFn,
gmf groupMutedFunc,
silences *silence.Silences,
peer cluster.ClusterPeer,
l log.Logger,
r prometheus.Registerer,
) (*API, error) {
api := API{
alerts: alerts,
getAlertStatus: sf,
getAlertStatus: asf,
alertGroups: gf,
groupMutedFunc: gmf,
peer: peer,
silences: silences,
logger: l,
Expand Down Expand Up @@ -407,17 +411,22 @@ func (api *API) getAlertGroupsHandler(params alertgroup_ops.GetAlertGroupsParams
res := make(open_api_models.AlertGroups, 0, len(alertGroups))

for _, alertGroup := range alertGroups {
mutedBy, isMuted := api.groupMutedFunc(alertGroup.RouteID, alertGroup.GroupKey)
if !*params.Muted && isMuted {
continue
}

ag := &open_api_models.AlertGroup{
Receiver: &open_api_models.Receiver{Name: &alertGroup.Receiver},
Labels: ModelLabelSetToAPILabelSet(alertGroup.Labels),
Alerts: make([]*open_api_models.GettableAlert, 0, len(alertGroup.Alerts)),
Alerts: make([]*open_api_models.ExtendedGettableAlert, 0, len(alertGroup.Alerts)),
}

for _, alert := range alertGroup.Alerts {
fp := alert.Fingerprint()
receivers := allReceivers[fp]
status := api.getAlertStatus(fp)
apiAlert := AlertToOpenAPIAlert(alert, status, receivers)
apiAlert := AlertToExtendedOpenAPIAlert(alert, status, receivers, isMuted, mutedBy)
ag.Alerts = append(ag.Alerts, apiAlert)
}
res = append(res, ag)
Expand Down
39 changes: 39 additions & 0 deletions api/v2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,44 @@ func TestAlertToOpenAPIAlert(t *testing.T) {
Alert: open_api_models.Alert{
Labels: open_api_models.LabelSet{"severity": "critical", "alertname": "alert1"},
},
StartsAt: convertDateTime(start),
EndsAt: convertDateTime(time.Time{}),
UpdatedAt: convertDateTime(updated),
Fingerprint: &fp,
Receivers: []*open_api_models.Receiver{
{Name: &receivers[0]},
{Name: &receivers[1]},
},
Status: &open_api_models.AlertStatus{
State: &active,
InhibitedBy: []string{},
SilencedBy: []string{},
},
}, openAPIAlert)
}

func TestAlertToExtendedOpenAPIAlert(t *testing.T) {
var (
start = time.Now().Add(-time.Minute)
updated = time.Now()
active = "active"
fp = "0223b772b51c29e1"
receivers = []string{"receiver1", "receiver2"}

alert = &types.Alert{
Alert: model.Alert{
Labels: model.LabelSet{"severity": "critical", "alertname": "alert1"},
StartsAt: start,
},
UpdatedAt: updated,
}
)
openAPIAlert := AlertToExtendedOpenAPIAlert(alert, types.AlertStatus{State: types.AlertStateActive}, receivers, false, nil)
require.Equal(t, &open_api_models.ExtendedGettableAlert{
Annotations: open_api_models.LabelSet{},
Alert: open_api_models.Alert{
Labels: open_api_models.LabelSet{"severity": "critical", "alertname": "alert1"},
},
StartsAt: convertDateTime(start),
EndsAt: convertDateTime(time.Time{}),
UpdatedAt: convertDateTime(updated),
Expand All @@ -429,6 +462,12 @@ func TestAlertToOpenAPIAlert(t *testing.T) {
{Name: &receivers[0]},
{Name: &receivers[1]},
},
Status: &open_api_models.ExtendedAlertStatus{
State: &active,
InhibitedBy: []string{},
SilencedBy: []string{},
MutedBy: []string{},
},
}, openAPIAlert)
}

Expand Down
39 changes: 39 additions & 0 deletions api/v2/client/alertgroup/get_alert_groups_parameters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions api/v2/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,56 @@ func AlertToOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers
return aa
}

func AlertToExtendedOpenAPIAlert(alert *types.Alert, status types.AlertStatus, receivers []string, isMuted bool, mutedBy []string) *open_api_models.ExtendedGettableAlert {
aa := AlertToOpenAPIAlert(alert, status, receivers)

as := aa.Status
// aa.Status should never be nil, but add a check to avoid panics
// if it somehow is nil.
if as == nil {
as = &open_api_models.AlertStatus{}
}

// Turn the AlertStatus into an ExtendedAlertStatus.
es := &open_api_models.ExtendedAlertStatus{
State: as.State,
SilencedBy: as.SilencedBy,
InhibitedBy: as.InhibitedBy,
MutedBy: mutedBy,
}

// If the alert is muted, change the state to suppressed.
if isMuted {
state := open_api_models.AlertStatusStateSuppressed
es.State = &state
}

ea := &open_api_models.ExtendedGettableAlert{
Annotations: aa.Annotations,
EndsAt: aa.EndsAt,
Fingerprint: aa.Fingerprint,
Receivers: aa.Receivers,
StartsAt: aa.StartsAt,
Status: es,
UpdatedAt: aa.UpdatedAt,
Alert: aa.Alert,
}

if ea.Status.SilencedBy == nil {
ea.Status.SilencedBy = []string{}
}

if ea.Status.InhibitedBy == nil {
ea.Status.InhibitedBy = []string{}
}

if ea.Status.MutedBy == nil {
ea.Status.MutedBy = []string{}
}

return ea
}

// OpenAPIAlertsToAlerts converts open_api_models.PostableAlerts to []*types.Alert.
func OpenAPIAlertsToAlerts(apiAlerts open_api_models.PostableAlerts) []*types.Alert {
alerts := []*types.Alert{}
Expand Down
2 changes: 1 addition & 1 deletion api/v2/models/alert_group.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0cd1216

Please sign in to comment.