mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-16 10:37:09 +00:00
Add 'silenced' query param to /alerts endpoint
This adds the silenced query parameter to the /alerts API endpoint. In addition it changes the default in the UI. Previously silenced alerts were displayed by default. This PR hides silenced alerts by default.
This commit is contained in:
parent
d73a655bf4
commit
b9acb7a63f
24
api/api.go
24
api/api.go
@ -256,8 +256,9 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
|
||||
err error
|
||||
// Initialize result slice to prevent api returning `null` when there
|
||||
// are no alerts present
|
||||
res = []*APIAlert{}
|
||||
matchers = []*labels.Matcher{}
|
||||
res = []*APIAlert{}
|
||||
matchers = []*labels.Matcher{}
|
||||
showSilenced = true
|
||||
)
|
||||
|
||||
if filter := r.FormValue("filter"); filter != "" {
|
||||
@ -271,6 +272,21 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if silencedParam := r.FormValue("silenced"); silencedParam != "" {
|
||||
if silencedParam == "false" {
|
||||
showSilenced = false
|
||||
} else if silencedParam != "true" {
|
||||
respondError(w, apiError{
|
||||
typ: errorBadData,
|
||||
err: fmt.Errorf(
|
||||
"parameter 'silenced' can either be 'true' or 'false', not '%v'",
|
||||
silencedParam,
|
||||
),
|
||||
}, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
alerts := api.alerts.GetPending()
|
||||
defer alerts.Close()
|
||||
|
||||
@ -286,6 +302,10 @@ func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
status := api.getAlertStatus(a.Fingerprint())
|
||||
|
||||
if !showSilenced && len(status.SilencedBy) != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
apiAlert := &APIAlert{
|
||||
Alert: &a.Alert,
|
||||
Status: status,
|
||||
|
@ -48,7 +48,7 @@ generateQueryString { receiver, showSilenced, text, group } =
|
||||
let
|
||||
-- TODO: Re-add receiver once it is parsed on the server side.
|
||||
parts =
|
||||
[ ( "silenced", Maybe.map (toString >> String.toLower) showSilenced )
|
||||
[ ( "silenced", Maybe.withDefault False showSilenced |> toString |> String.toLower |> Just )
|
||||
, ( "filter", emptyToNothing text )
|
||||
, ( "group", group )
|
||||
]
|
||||
|
@ -1,4 +1,4 @@
|
||||
module Views.AlertList.Filter exposing (silenced, matchers)
|
||||
module Views.AlertList.Filter exposing (matchers)
|
||||
|
||||
import Alerts.Types exposing (Alert, AlertGroup, Block)
|
||||
import Utils.Types exposing (Matchers)
|
||||
@ -15,30 +15,6 @@ matchers matchers alerts =
|
||||
alerts
|
||||
|
||||
|
||||
silenced : Maybe Bool -> List Alert -> List Alert
|
||||
silenced maybeShowSilenced alerts =
|
||||
let
|
||||
showSilenced =
|
||||
Maybe.withDefault False maybeShowSilenced
|
||||
in
|
||||
if showSilenced then
|
||||
alerts
|
||||
else
|
||||
List.filter (.silenced >> not) alerts
|
||||
|
||||
|
||||
filterAlertGroup : (String -> Bool) -> AlertGroup -> Maybe AlertGroup
|
||||
filterAlertGroup fn alertGroup =
|
||||
let
|
||||
blocks =
|
||||
List.filter (\b -> fn b.routeOpts.receiver) alertGroup.blocks
|
||||
in
|
||||
if not <| List.isEmpty blocks then
|
||||
Just { alertGroup | blocks = blocks }
|
||||
else
|
||||
Nothing
|
||||
|
||||
|
||||
alertsFromBlock : (Alert -> Bool) -> Block -> Maybe Block
|
||||
alertsFromBlock fn block =
|
||||
let
|
||||
@ -95,28 +71,6 @@ filterAlertGroupLabels matchers alertGroup =
|
||||
Nothing
|
||||
|
||||
|
||||
matchersToLabels : Utils.Types.Matchers -> Utils.Types.Labels
|
||||
matchersToLabels matchers =
|
||||
List.map (\m -> ( m.name, m.value )) matchers
|
||||
|
||||
|
||||
alertGroupsSilenced : AlertGroup -> Maybe AlertGroup
|
||||
alertGroupsSilenced alertGroup =
|
||||
let
|
||||
blocks =
|
||||
List.filterMap filterSilencedAlerts alertGroup.blocks
|
||||
in
|
||||
if not <| List.isEmpty blocks then
|
||||
Just { alertGroup | blocks = blocks }
|
||||
else
|
||||
Nothing
|
||||
|
||||
|
||||
filterSilencedAlerts : Block -> Maybe Block
|
||||
filterSilencedAlerts block =
|
||||
alertsFromBlock (.silenced >> not) block
|
||||
|
||||
|
||||
by : (a -> Maybe a) -> List a -> List a
|
||||
by fn groups =
|
||||
List.filterMap fn groups
|
||||
|
@ -11,7 +11,7 @@ import Utils.Types exposing (ApiResponse(Initial, Success, Loading, Failure), La
|
||||
import Utils.Views exposing (buttonLink, listButton)
|
||||
import Utils.List
|
||||
import Views.AlertList.AlertView as AlertView
|
||||
import Views.AlertList.Filter exposing (silenced, matchers)
|
||||
import Views.AlertList.Filter exposing (matchers)
|
||||
import Views.GroupBar.Types as GroupBar
|
||||
import Utils.Views exposing (buttonLink, listButton)
|
||||
import Views.AlertList.Types exposing (AlertListMsg(MsgForFilterBar, MsgForGroupBar, SetTab, ToggleSilenced), Model, Tab(..))
|
||||
@ -94,7 +94,6 @@ alertGroups filter groupBar alerts =
|
||||
let
|
||||
grouped =
|
||||
alerts
|
||||
|> silenced filter.showSilenced
|
||||
|> Utils.List.groupBy
|
||||
(.labels >> List.filter (\( key, _ ) -> List.member key groupBar.fields))
|
||||
in
|
||||
|
@ -34,7 +34,7 @@ update msg model =
|
||||
| silence = Success { silence | silencedAlerts = Loading }
|
||||
}
|
||||
, Alerts.Api.fetchAlerts
|
||||
({ nullFilter | text = Just (Utils.List.mjoin silence.matchers) })
|
||||
({ nullFilter | text = Just (Utils.List.mjoin silence.matchers), showSilenced = Just True })
|
||||
|> Cmd.map (AlertGroupsPreview >> MsgForSilence)
|
||||
)
|
||||
|
||||
|
@ -38,17 +38,21 @@ parseMatcher =
|
||||
generateQueryString : Test
|
||||
generateQueryString =
|
||||
describe "generateQueryString"
|
||||
[ test "should not render keys with Nothing value" <|
|
||||
[ test "should default silenced parameter to false if showSilenced is Nothing" <|
|
||||
\() ->
|
||||
Expect.equal ""
|
||||
Expect.equal "?silenced=false"
|
||||
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Nothing, showSilenced = Nothing })
|
||||
, test "should not render keys with Nothing value except the silenced parameter" <|
|
||||
\() ->
|
||||
Expect.equal "?silenced=false"
|
||||
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Nothing, showSilenced = Nothing })
|
||||
, test "should not render filter key with empty value" <|
|
||||
\() ->
|
||||
Expect.equal ""
|
||||
Expect.equal "?silenced=false"
|
||||
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Just "", showSilenced = Nothing })
|
||||
, test "should render filter key with values" <|
|
||||
\() ->
|
||||
Expect.equal "?filter=%7Bfoo%3D%22bar%22%2C%20baz%3D~%22quux.*%22%7D"
|
||||
Expect.equal "?silenced=false&filter=%7Bfoo%3D%22bar%22%2C%20baz%3D~%22quux.*%22%7D"
|
||||
(Utils.Filter.generateQueryString { receiver = Nothing, group = Nothing, text = Just "{foo=\"bar\", baz=~\"quux.*\"}", showSilenced = Nothing })
|
||||
, test "should render silenced key with bool" <|
|
||||
\() ->
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user