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

Allow using precise floats in logs #2005

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kasmarian
Copy link
Member

@kasmarian kasmarian commented Jan 10, 2025

Description

This PR allows using precise floats in logs.

Several JSON body filters in Logbook use JsonGenerator to reconstruct the payload.
generator.copyCurrentEvent method has a flaw when writing float numbers. This may result in logging a different float value than the actual value that was in the payload.

With this change, I'm adding an optional boolean flag to all JSON body filters to use copyCurrentEventExact instaed. That may come with a performance impact (not sure how significant).

Motivation and Context

#1993

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@kasmarian kasmarian added the bugfix Bug fixes and patches label Jan 10, 2025
Copy link
Collaborator

@msdousti msdousti left a comment

Choose a reason for hiding this comment

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

Meta comment: Jackson can encode floats as double, BigDecimal or String. The last option might be both efficient and precise.

> Filters for JSON bodies are using Jackson, which comes with a defect of dropping off precision from floating point
> numbers (see [FasterXML/jackson-core/issues/984](https://github.com/FasterXML/jackson-core/issues/984)).
>
> This can be changed by setting the `usePreciseFloats` flag to true in the filter respective filters.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe point out herr that using this setting can impose a performance penalty on the application due to encoding floats as BigDecimal.

@@ -27,7 +38,11 @@ public String compact(final String json) throws IOException {
final JsonGenerator generator = factory.createGenerator(output)) {

while (parser.nextToken() != null) {
generator.copyCurrentEvent(parser);
if (usePreciseFloats) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this logic be refactored?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Bug fixes and patches
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants