Skip to content

encoder: use protojson encoder instead of encoding/json#1099

Merged
willfindlay merged 4 commits into
mainfrom
pr/willfindlay/use-protojson-encoder
Jun 28, 2023
Merged

encoder: use protojson encoder instead of encoding/json#1099
willfindlay merged 4 commits into
mainfrom
pr/willfindlay/use-protojson-encoder

Conversation

@willfindlay

@willfindlay willfindlay commented Jun 19, 2023

Copy link
Copy Markdown
Contributor

The standard json encoder does not work well with protobuf messages. For example, we may
experience issues during encoding/decoding such as [0]. To fix this, we need to switch
over to protojson. See commits.
[0]: golang/protobuf#1433

@willfindlay
willfindlay force-pushed the pr/willfindlay/use-protojson-encoder branch from 8ae2290 to 0010ecc Compare June 20, 2023 18:39
@netlify

netlify Bot commented Jun 20, 2023

Copy link
Copy Markdown

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit 59c4ee2
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/649af3bd3b69ea00082fd821
😎 Deploy Preview https://deploy-preview-1099--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@willfindlay
willfindlay force-pushed the pr/willfindlay/use-protojson-encoder branch 3 times, most recently from 76aee9d to e771daf Compare June 26, 2023 14:39
@willfindlay
willfindlay marked this pull request as ready for review June 26, 2023 16:54
@willfindlay
willfindlay requested a review from a team as a code owner June 26, 2023 16:54
@willfindlay
willfindlay requested a review from olsajiri June 26, 2023 16:54
@willfindlay
willfindlay force-pushed the pr/willfindlay/use-protojson-encoder branch 2 times, most recently from 59c4ee2 to e760c51 Compare June 27, 2023 16:53
The standard json encoder does not work well with protobuf messages. For example, we may
experience issues during encoding/decoding such as [0]. To fix this, we need to switch
over to protojson and this patch prepares for the switch by implementing a simple encoder
that uses protojson.Marshal under the hood. It's not as fancy as the streaming encoder
from encoding/json, but I think it's good enough to at least fix the issue for now.
Subsequent patches will switch our primary JSON encoder over to this new protojson one.

[0]: golang/protobuf#1433

Signed-off-by: William Findlay <will@isovalent.com>
Make the switch from json.Encoder to encoder.ProtojsonEncoder which will handle protobuf
messages better and avoid buggy behaviour.

Signed-off-by: William Findlay <will@isovalent.com>
The new protojson encoder requires that the event type always be a part of the protobuf.
This broke rate_limit_info, so let's add it to the official API.

Signed-off-by: William Findlay <will@isovalent.com>
Introduce a fuzz test to ensure that the new protojson encoder is backward compatible with
the old json encoder. The test generates a random GetEventsResponse protomessage using
a seed value and verifies that it can be roundtripped through both serializers and produce
an equivalent result. The test comes with a seed corpus of a few randomly generated values
that can be used reliably in CI. I ran this for over an hour on my local workstation
without any issues.

Signed-off-by: William Findlay <will@isovalent.com>
@willfindlay
willfindlay force-pushed the pr/willfindlay/use-protojson-encoder branch from e760c51 to a1a0150 Compare June 27, 2023 18:10
} {
f.Add(n)
}
f.Fuzz(func(t *testing.T, seed int64) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

😻

@willfindlay
willfindlay merged commit fc082d1 into main Jun 28, 2023
@willfindlay
willfindlay deleted the pr/willfindlay/use-protojson-encoder branch June 28, 2023 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants