Skip to content

Commit

Permalink
Fix issue prometheus#3774
Browse files Browse the repository at this point in the history
Signed-off-by: George Robinson <[email protected]>
  • Loading branch information
grobinson-grafana committed Mar 22, 2024
1 parent 14cbe63 commit c58ae75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
9 changes: 4 additions & 5 deletions silence/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,12 @@ type QueryParam func(*query) error

type query struct {
ids []string
filters []silenceFilter
filters []filterFunc
}

// silenceFilter is a function that returns true if a silence
// should be dropped from a result set for a given time.
type silenceFilter func(*pb.Silence, *Silences, time.Time) (bool, error)
// filterFunc is a function to filter silences in a result set. It should
// return true if the silence is included, otherwise false.
type filterFunc func(*pb.Silence, *Silences, time.Time) (bool, error)

// QIDs configures a query to select the given silence IDs.
func QIDs(ids ...string) QueryParam {
Expand Down Expand Up @@ -726,7 +726,6 @@ func QState(states ...types.SilenceState) QueryParam {
return func(q *query) error {
f := func(sil *pb.Silence, _ *Silences, now time.Time) (bool, error) {
s := getState(sil, now)

for _, ps := range states {
if s == ps {
return true, nil
Expand Down
22 changes: 11 additions & 11 deletions silence/silence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,24 +591,24 @@ func TestQMatches(t *testing.T) {
f := q.filters[0]

cases := []struct {
sil *pb.Silence
drop bool
sil *pb.Silence
expected bool
}{
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
{Name: "job", Pattern: "test", Type: pb.Matcher_EQUAL},
},
},
drop: true,
expected: true,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
{Name: "job", Pattern: "test", Type: pb.Matcher_NOT_EQUAL},
},
},
drop: false,
expected: false,
},
{
sil: &pb.Silence{
Expand All @@ -617,7 +617,7 @@ func TestQMatches(t *testing.T) {
{Name: "method", Pattern: "POST", Type: pb.Matcher_EQUAL},
},
},
drop: false,
expected: false,
},
{
sil: &pb.Silence{
Expand All @@ -626,23 +626,23 @@ func TestQMatches(t *testing.T) {
{Name: "method", Pattern: "POST", Type: pb.Matcher_NOT_EQUAL},
},
},
drop: true,
expected: true,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
{Name: "path", Pattern: "/user/.+", Type: pb.Matcher_REGEXP},
},
},
drop: true,
expected: true,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
{Name: "path", Pattern: "/user/.+", Type: pb.Matcher_NOT_REGEXP},
},
},
drop: false,
expected: false,
},
{
sil: &pb.Silence{
Expand All @@ -651,13 +651,13 @@ func TestQMatches(t *testing.T) {
{Name: "path", Pattern: "/nothing/.+", Type: pb.Matcher_REGEXP},
},
},
drop: false,
expected: false,
},
}
for _, c := range cases {
drop, err := f(c.sil, &Silences{mc: matcherCache{}, st: state{}}, time.Time{})
actual, err := f(c.sil, &Silences{mc: matcherCache{}, st: state{}}, time.Time{})
require.NoError(t, err)
require.Equal(t, c.drop, drop, "unexpected filter result")
require.Equal(t, c.expected, actual, "unexpected filter result")
}
}

Expand Down

0 comments on commit c58ae75

Please sign in to comment.