Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing comments in silence.go #3774

Open
grobinson-grafana opened this issue Mar 22, 2024 · 0 comments
Open

Confusing comments in silence.go #3774

grobinson-grafana opened this issue Mar 22, 2024 · 0 comments

Comments

@grobinson-grafana
Copy link
Contributor

What did you do?

I was looking at the code in silence.go and – unless I'm being obtuse – I think some of the comments and tests are documented as working the opposite way from how they actually work.

For example, the comment for silenceFilter is:

// 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)

The important phrase:

returns true if a silence should be dropped from a result set

And the tests for some of the filters support this:

func TestQMatches(t *testing.T) {
	...
	cases := []struct {
		sil  *pb.Silence
		drop bool
	}{
		{
			sil: &pb.Silence{
				Matchers: []*pb.Matcher{
					{Name: "job", Pattern: "test", Type: pb.Matcher_EQUAL},
				},
			},
			drop: true,
		},
	...
}

However, if I look in query where these functions are used:

func (s *Silences) query(q *query, now time.Time) ([]*pb.Silence, int, error) {
	...
		for _, f := range q.filters {
			ok, err := f(sil, s, now)
			if err != nil {
				return nil, s.version, err
			}
			if !ok {
				remove = true
				break
			}
	...
}

it seems that if the filter returns true, then it is instead included in the result set, and removed if it returns false. This is the opposite of what is in the comment:

returns true if a silence should be dropped from a result set

The tests for querying seem to support this too:

func TestSilencesQuery(t *testing.T) {
	...
		{
			// Retrieve all and filter
			q: &query{
				filters: []silenceFilter{
					func(sil *pb.Silence, _ *Silences, _ time.Time) (bool, error) {
						return sil.Id == "1" || sil.Id == "2", nil
					},
				},
			},
			exp: []*pb.Silence{
				{Id: "1"},
				{Id: "2"},
			},
		},
	}
...
}

because if "returns true if a silence should be dropped from a result set" were true, then the IDs 1 and 2 would be excluded from the result, rather than expected.

I just wanted to check that I'm not confused before I open a PR to fix this.

grobinson-grafana added a commit to grobinson-grafana/alertmanager that referenced this issue Mar 22, 2024
grobinson-grafana added a commit to grobinson-grafana/alertmanager that referenced this issue Mar 22, 2024
Signed-off-by: George Robinson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant