Skip to content

cmetrics: prevent appending .0 to scientific notation in bucket_value_to_string#11654

Open
Pigueiras wants to merge 1 commit intofluent:masterfrom
Pigueiras:fix11651
Open

cmetrics: prevent appending .0 to scientific notation in bucket_value_to_string#11654
Pigueiras wants to merge 1 commit intofluent:masterfrom
Pigueiras:fix11651

Conversation

@Pigueiras
Copy link
Copy Markdown

@Pigueiras Pigueiras commented Apr 1, 2026

bucket_value_to_string used %g to format histogram bucket boundaries, which switches to scientific notation for values >= 1e6. The subsequent check for a missing decimal point would then append ".0", producing malformed labels like "1e+06.0" that Prometheus rejects with:

  bucket label "le" is missing or has a malformed value of "1e+08.0"

Skip the ".0" suffix when the string already contains 'e', since scientific notation is valid in Prometheus le labels without it.

Fixes #11651

Testing
Before we can approve your change; please submit the following in a comment:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed Prometheus metric encoding so numeric bucket values no longer get an unnecessary ".0" suffix when rendered in scientific notation. This change ensures decimal suffixes are applied only when appropriate, improving metric readability and compatibility with downstream consumers.

Copilot AI review requested due to automatic review settings April 1, 2026 19:45
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 1, 2026

📝 Walkthrough

Walkthrough

Adjusted Prometheus bucket value formatting: bucket_value_to_string() now appends .0 only when the %g output lacks a decimal point and does not contain an 'e' (scientific notation), preventing malformed values like 1e+06.0.

Changes

Cohort / File(s) Summary
Scientific Notation Fix
lib/cmetrics/src/cmt_encode_prometheus.c
Modified bucket_value_to_string() to detect 'e' in the formatted string and avoid appending .0 for scientific-notation outputs, fixing malformed Prometheus le labels like 1e+06.0.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested labels

backport to v4.2.x

Suggested reviewers

  • edsiper
  • cosmo0920

Poem

🐰 I hop through buckets, tidy and spry,
No stray “.0” on numbers that fly.
Scientific forms now wear no extra dot,
Metrics tidy — oh what a spot! 🌷

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The code changes directly address issue #11651 by modifying bucket_value_to_string to skip adding .0 suffix when scientific notation (containing 'e') is present, fixing the malformed Prometheus labels.
Out of Scope Changes check ✅ Passed The changes are focused solely on the bucket_value_to_string function formatting logic, directly addressing the reported issue with no extraneous modifications.
Title check ✅ Passed The title clearly and specifically describes the main fix: preventing .0 suffix appending to scientific notation in the bucket_value_to_string function, which directly addresses the core issue.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Prometheus histogram bucket label formatting by preventing bucket_value_to_string() from appending a trailing .0 when %g emits scientific notation (e.g., avoiding invalid labels like 1e+08.0).

Changes:

  • Update bucket_value_to_string() to skip appending .0 when the formatted value is in scientific notation (...e...).
  • Add clarifying inline comments around the formatting logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +376 to +377
// Only append .0 if it's already a decimal or the number
// is NOT in scientific notation
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

The new inline comment is inaccurate and inconsistent with the surrounding style: the condition checks for the absence of a decimal point, but the comment says "append .0 if it's already a decimal". Consider switching to the file’s existing /* ... */ comment style and rewording to match the actual condition (append .0 only when there is no decimal point and the value is not in scientific notation).

Suggested change
// Only append .0 if it's already a decimal or the number
// is NOT in scientific notation
/* Append .0 only when there is no decimal point and the number
* is not in scientific notation.
*/

Copilot uses AI. Check for mistakes.
Comment on lines 373 to 380
len = snprintf(str, 64, "%g", val);
cfl_sds_len_set(str, len);

if (!strchr(str, '.')) {
// Only append .0 if it's already a decimal or the number
// is NOT in scientific notation
if (!strchr(str, '.') && !strchr(str, 'e')) {
cfl_sds_cat_safe(&str, ".0", 2);
}
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

Please add a regression unit test to cover this formatting behavior (large histogram bucket boundaries that are rendered in scientific notation should not get a trailing ".0" in the exported Prometheus le label). This helps prevent reintroducing malformed labels like "1e+08.0".

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks great but we need to register changes which intent to cmetrics into https://github.com/fluent/cmetrics. This is because under lib directory is handled as bundled libraries.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
lib/cmetrics/src/cmt_encode_prometheus.c (1)

373-380: Consider unifying float-to-string formatting across encoders to prevent drift.

bucket_value_to_string() is now fixed, but similar logic in lib/cmetrics/src/cmt_encode_splunk_hec.c:34-57 still appends .0 based only on '.' presence. A shared helper (or parity patch) would avoid repeating this bug class.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/cmetrics/src/cmt_encode_prometheus.c` around lines 373 - 380, The
Prometheus encoder appends ".0" by checking only for '.' and 'e' which
duplicates logic present in cmt_encode_splunk_hec.c and can drift; extract this
into a shared helper (e.g., cmt_float_to_string or bucket_value_to_string as a
common utility) that takes the double, writes it to an SDS/string buffer using
snprintf("%g"), ensures cfl_sds_len_set is applied, and appends ".0" only when
there is no '.' and no 'e' (i.e., integer not in scientific notation). Replace
the inline formatting in cmt_encode_prometheus.c (the snippet using
snprintf/strchr) and the corresponding logic in
lib/cmetrics/src/cmt_encode_splunk_hec.c (lines ~34-57) to call the new helper
so both encoders use the exact same formatting routine.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@lib/cmetrics/src/cmt_encode_prometheus.c`:
- Around line 376-377: Replace the two // comment lines with a /* ... */
block-style comment and tighten the wording to clearly state the condition: e.g.
"/* Append \".0\" only when the numeric string contains a decimal point or does
not use scientific notation. */". Apply this block comment immediately above the
logic that decides whether to append ".0" in cmt_encode_prometheus.c so the
intent is unambiguous and the comment follows the project's block-comment
guideline.

---

Nitpick comments:
In `@lib/cmetrics/src/cmt_encode_prometheus.c`:
- Around line 373-380: The Prometheus encoder appends ".0" by checking only for
'.' and 'e' which duplicates logic present in cmt_encode_splunk_hec.c and can
drift; extract this into a shared helper (e.g., cmt_float_to_string or
bucket_value_to_string as a common utility) that takes the double, writes it to
an SDS/string buffer using snprintf("%g"), ensures cfl_sds_len_set is applied,
and appends ".0" only when there is no '.' and no 'e' (i.e., integer not in
scientific notation). Replace the inline formatting in cmt_encode_prometheus.c
(the snippet using snprintf/strchr) and the corresponding logic in
lib/cmetrics/src/cmt_encode_splunk_hec.c (lines ~34-57) to call the new helper
so both encoders use the exact same formatting routine.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c4622f9f-e3bb-4bd2-9139-ceb5d4cb38a9

📥 Commits

Reviewing files that changed from the base of the PR and between 66ffbe4 and ea204e5.

📒 Files selected for processing (1)
  • lib/cmetrics/src/cmt_encode_prometheus.c

@Pigueiras Pigueiras force-pushed the fix11651 branch 2 times, most recently from 63863ec to bc16164 Compare April 1, 2026 20:02
…bels

`bucket_value_to_string` used %g to format histogram bucket boundaries,
which switches to scientific notation for values >= 1e6. The subsequent
check for a missing decimal point would then append ".0", producing
malformed labels like "1e+06.0" that Prometheus rejects with:

```
  bucket label "le" is missing or has a malformed value of "1e+08.0"
```

Skip the ".0" suffix when the string already contains 'e', since
scientific notation is valid in Prometheus `le` labels without it.

Fixes fluent#11651

Signed-off-by: Luis Pigueiras <luis.pigueiras@cern.ch>
@Pigueiras Pigueiras changed the title fix(cmetrics): prevent appending .0 to scientific notation in bucket_value_to_string cmetrics: prevent appending .0 to scientific notation in bucket_value_to_string Apr 1, 2026
@edsiper
Copy link
Copy Markdown
Member

edsiper commented Apr 2, 2026

@Pigueiras thanks for this PR. please submit the fix to https://github.com/fluent/cmetrics for review

@Pigueiras
Copy link
Copy Markdown
Author

@Pigueiras thanks for this PR. please submit the fix to https://github.com/fluent/cmetrics for review

Sorry @edsiper @cosmo0920, I didn't know it was a separate repository. I did the PR in fluent/cmetrics#261, let me know if you need anything else. Thanks a lot for your review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Log to Metrics generates histogram buckets with invalid scientific notation for large values

4 participants