Skip to content

Commit

Permalink
Add silences filtering
Browse files Browse the repository at this point in the history
Filter silences through `?filter=` query string.
  • Loading branch information
stuartnelson3 committed Mar 15, 2017
1 parent 714d106 commit 049fbbd
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,19 @@ func (api *API) listSilences(w http.ResponseWriter, r *http.Request) {
return
}

var sils []*types.Silence
matchers := []*labels.Matcher{}
if filter := r.FormValue("filter"); filter != "" {
matchers, err = parse.Matchers(filter)
if err != nil {
respondError(w, apiError{
typ: errorBadData,
err: err,
}, nil)
return
}
}

sils := []*types.Silence{}
for _, ps := range psils {
s, err := silenceFromProto(ps)
if err != nil {
Expand All @@ -465,12 +477,30 @@ func (api *API) listSilences(w http.ResponseWriter, r *http.Request) {
}, nil)
return
}

if !matchesFilterLabels(s, matchers) {
continue
}
sils = append(sils, s)
}

respond(w, sils)
}

func matchesFilterLabels(s *types.Silence, matchers []*labels.Matcher) bool {
sms := map[string]string{}
for _, m := range s.Matchers {
sms[m.Name] = m.Value
}
for _, m := range matchers {
if v, prs := sms[m.Name]; !prs || !m.Matches(v) {
return false
}
}

return true
}

func silenceToProto(s *types.Silence) (*silencepb.Silence, error) {
startsAt, err := ptypes.TimestampProto(s.StartsAt)
if err != nil {
Expand Down

0 comments on commit 049fbbd

Please sign in to comment.