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

util/log: allow bool fields to be emitted as false in json format #113757

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

xinhaoz
Copy link
Member

@xinhaoz xinhaoz commented Nov 3, 2023

Previously with the json logging format, it was not possible to emit boolean fields that were false. Even if the boolean field is marked as 'includeempty', it will be emitted as field: true in the event. While always not including false boolean fields is more space efficient, with certain fields it's helpful to have the field emitted in all instances and explicitly state its 'false' value.

This patch makes it possible for fieldName: false to be emitted in the json logging format when the field is marked as includeempty.

Epic: none
Part of: #108284

Release note: None

Previously with the json logging format, it was not possible to emit
boolean fields that were false. Even if the boolean field is marked as
'includeempty', it will be emitted as `field: true` in the event.
While always not including false boolean fields is more space efficient,
with certain fields it's helpful to have the field emitted in all
instances and explicitly state its 'false' value.

This patch makes it possible for `fieldName: false` to be emitted
in the json logging format when the field is marked as `includeempty`.

Epic: none
Part of: cockroachdb#108284

Release note: None
@xinhaoz xinhaoz requested review from a team and abarganier and removed request for a team November 3, 2023 15:49
Copy link

blathers-crl bot commented Nov 3, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@xinhaoz
Copy link
Member Author

xinhaoz commented Nov 6, 2023

Note that while while the previous behaviour is technically a bug (any boolean fields that were marked as 'includemepty' would always show as true, even if the value is false), it is not a user visible bug because we don't actually have any boolean fields at this point that are marked as 'includemempty'. At this time, all bool fields are simply omitted if they are false.

Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier and @xinhaoz)


pkg/util/log/eventpb/eventpbgen/gen.go line 599 at r1 (raw file):

     if printComma { b = append(b, ',')}; printComma = true
     b = append(b, "\"{{.FieldName}}\":"...)
     b = strconv.AppendBool(b, m.{{.FieldName}})

is there any test that can be done to check for other types? just in case this is also happening in other cases

@xinhaoz
Copy link
Member Author

xinhaoz commented Nov 6, 2023

pkg/util/log/eventpb/eventpbgen/gen.go line 599 at r1 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

is there any test that can be done to check for other types? just in case this is also happening in other cases

I haven't seen any testing specifically for the code generation and we don't currently have boolean any fields in existing protobufs that are marked as includeempty to validate :/. @abarganier did I miss anything in terms of existing testing? Should I create a testing protobuf with that field type right now and test emitting that?

@xinhaoz
Copy link
Member Author

xinhaoz commented Nov 6, 2023

pkg/util/log/eventpb/eventpbgen/gen.go line 599 at r1 (raw file):

Previously, xinhaoz (Xin Hao Zhang) wrote…

I haven't seen any testing specifically for the code generation and we don't currently have boolean any fields in existing protobufs that are marked as includeempty to validate :/. @abarganier did I miss anything in terms of existing testing? Should I create a testing protobuf with that field type right now and test emitting that?

Ah sorry, your question was specifically for other types. We seem to be using includeempty very sparingly in our protobufs already. I didn't see any other cases of types exhibiting this behaviour in gen.go, I think it was easier for this case specifically because there are only 2 values for a boolean, I'd imagine it'd be more obvious if other types were hard coding a value or had special behaviour for the zero value. In any case, q about testing remains.

@xinhaoz xinhaoz requested a review from a team November 8, 2023 18:16
Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @abarganier)

@xinhaoz
Copy link
Member Author

xinhaoz commented Nov 9, 2023

TFTR! Let me know if you have any remaining comments on testing the code generation!
bors r+

@craig
Copy link
Contributor

craig bot commented Nov 9, 2023

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Nov 9, 2023

Build succeeded:

@craig craig bot merged commit 38a8a45 into cockroachdb:master Nov 9, 2023
3 checks passed
@xinhaoz xinhaoz deleted the json-logs-allow-false-bools branch November 10, 2023 20:52
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.

3 participants