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

pkg/rules/rulespb: fix JSON marshaling for nil slices #2888

Merged
merged 1 commit into from
Jul 13, 2020

Conversation

simonpasquier
Copy link
Contributor

Go's encoding/json turns nil slices into null instead of []. This
is problematic for consumers expecting that the alerts field is a JSON
array. The same is true for empty rule groups.

  • I added CHANGELOG entry for this change.
  • Change is not relevant to the end user (the feature hasn't been released yet).

Changes

The solution is to enforce that nil slices are converted to zero-length
slices when encoding.

Verification

I've updated the unit tests to cover the issue.

Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very valid indeed. I think we were trying to fix this by returning make([]string) from various algorithms instead of nil which is kind of wrong (unnecessary allocs). I don't remember if it was actually merged at the end but I would see approach like yours to see more in future as a way to go 🤗
Thanks!

@bwplotka
Copy link
Member

Tests are failing for a good reason I think (:

Go's `encoding/json` turns nil slices into `null` instead of `[]`. This
is problematic for consumers expecting that the `alerts` field is a JSON
array. The same is true for empty rule groups.

The solution is to enforce that nil slices are converted to zero-length
slices when encoding.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
@simonpasquier
Copy link
Contributor Author

@bwplotka I've fixed tests in pkg/query/api/v1_test.go indeed. The e2e failure is more cryptic for me...

2020-07-13T09:51:15.5978723Z ##[error]    TestQueryExternalPrefixWithoutReverseProxy: query_test.go:278: �[31mquery_test.go:278:
2020-07-13T09:51:15.5979200Z         
2020-07-13T09:51:15.5980293Z          unexpected error: some network requests failed: net::ERR_NETWORK_CHANGED; net::ERR_NETWORK_CHANGED; net::ERR_NETWORK_CHANGED; net::ERR_NETWORK_CHANGED; net::ERR_NETWORK_CHANGED; net::ERR_NETWORK_CHANGED; net::ERR_NETWORK_CHANGED�[39m
2020-07-13T09:51:15.5980510Z         
2020-07-13T09:51:15.5980809Z Killing querier-1
2020-07-13T09:51:15.5980982Z Error: No such network: e2e_test_query
2020-07-13T09:51:15.5981064Z 
2020-07-13T09:51:15.5981286Z Unable to remove docker network e2e_test_query : exit status 1

@bwplotka
Copy link
Member

Looks like flak, retried

@bwplotka bwplotka merged commit 6fbe57c into thanos-io:master Jul 13, 2020
@bwplotka
Copy link
Member

Thanks!

@simonpasquier simonpasquier deleted the fix-nil-alerts-slice branch July 13, 2020 12:13
paulfantom pushed a commit to paulfantom/thanos that referenced this pull request Jul 13, 2020
Go's `encoding/json` turns nil slices into `null` instead of `[]`. This
is problematic for consumers expecting that the `alerts` field is a JSON
array. The same is true for empty rule groups.

The solution is to enforce that nil slices are converted to zero-length
slices when encoding.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
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

Successfully merging this pull request may close these issues.

2 participants