Skip to content

pack: Use lowercase string for binary format string#11748

Open
cosmo0920 wants to merge 4 commits intomasterfrom
cosmo0920-use-lowercase-string-for-binary-format-string
Open

pack: Use lowercase string for binary format string#11748
cosmo0920 wants to merge 4 commits intomasterfrom
cosmo0920-use-lowercase-string-for-binary-format-string

Conversation

@cosmo0920
Copy link
Copy Markdown
Contributor

@cosmo0920 cosmo0920 commented Apr 27, 2026

For human readable encoding, we need to encode trace_id and span_id for sending as JSON payloads.
Without this hex encoding, we have to handle binary tofu-ed character to search relative traces and logs.

So, we need to use hex format encoding before sending as JSON.
For just peek the contents of otel's payloads, we don't need do that.

Closes #11630.


Enter [N/A] in the box, if an item is not applicable to your change.

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

  • New Features

    • OpenTelemetry: when present in log metadata, trace_id and span_id are emitted as lowercase hexadecimal strings in JSON logs (preserving other metadata entries).
  • Tests

    • Added an integration test validating OTLP JSON logs retain hex trace and span IDs.
    • Added a unit test verifying metadata trace/span ID hex encoding.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cf890b94-dc2f-43fa-bfa3-90ef162a41f5

📥 Commits

Reviewing files that changed from the base of the PR and between a7dca93 and 6976a62.

📒 Files selected for processing (1)
  • src/flb_pack.c
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/flb_pack.c

📝 Walkthrough

Walkthrough

Adds a conditional msgpack→JSON rewrite for log_metadata: when log_event.metadata is a MAP and contains an otlp nested map, convert trace_id and span_id BIN values to packed lowercase hexadecimal strings; otherwise pack metadata unchanged.

Changes

Cohort / File(s) Summary
Core MsgPack Encoding
src/flb_pack.c
Adds logic to detect log_metadata MAPs, repack metadata, and for an otlp nested MAP convert trace_id and span_id BIN entries into lowercase hex string encodings while leaving other entries unchanged.
Integration Test
tests/integration/scenarios/in_opentelemetry/tests/test_in_opentelemetry_001.py
Adds a test that POSTs an OTLP JSON resourceLogs payload with traceId/spanId, waits for stdout OTLP JSON, and asserts the emitted record contains the same IDs as hex strings.
Unit Test
tests/internal/pack.c
Adds a unit test that builds a log event with binary trace_id/span_id metadata, converts msgpack→JSON, and asserts the JSON contains the expected lowercase hex encodings.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

backport to v4.0.x

Suggested reviewers

  • fujimotos
  • patrick-stephens
  • koleini

Poem

🐰 I found some BINs that looked like stew,
I nibbled bytes and stitched them new,
From wiggle-words to hex so neat,
OTLP hops out clean and sweet 🥕

🚥 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.
Title check ✅ Passed The title refers to using lowercase string for binary format, directly aligning with the core change: converting binary trace_id/span_id to lowercase hex strings in JSON outputs.
Linked Issues check ✅ Passed The PR implements the key objective from issue #11630: encoding OpenTelemetry trace_id and span_id binary values as lowercase hexadecimal strings in JSON outputs to preserve human-readable identifiers and enable proper trace/log correlation.
Out of Scope Changes check ✅ Passed All changes directly address the issue: msgpack serialization logic for trace_id/span_id hex conversion, corresponding unit tests, and integration tests validating the fix. No unrelated modifications detected.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cosmo0920-use-lowercase-string-for-binary-format-string

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.

@cosmo0920 cosmo0920 added this to the Fluent Bit v5.0.4 milestone Apr 27, 2026
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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/flb_pack.c`:
- Around line 1346-1352: The loop that writes hex digits using msgpack_pack_char
is semantically wrong; instead allocate a temporary buffer of length
obj->via.bin.size * 2, fill it with the two hex characters per input byte using
the existing table and input (use the same index/byte logic currently in the
loop), then call msgpack_pack_str_body(pck, buffer, obj->via.bin.size * 2) and
free the buffer (or use a stack array if size is bounded). Replace the
msgpack_pack_char calls with this single msgpack_pack_str_body call while
keeping the preceding msgpack_pack_str(pck, obj->via.bin.size * 2) intact so the
hex string body is packed correctly.
🪄 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: 2cdfd4d3-da32-4e56-ab84-3e672ccda852

📥 Commits

Reviewing files that changed from the base of the PR and between 29deec9 and 5af5840.

📒 Files selected for processing (3)
  • src/flb_pack.c
  • tests/integration/scenarios/in_opentelemetry/tests/test_in_opentelemetry_001.py
  • tests/internal/pack.c

Comment thread src/flb_pack.c Outdated
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
…n JSON

Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/flb_pack.c`:
- Around line 1333-1366: flb_pack_msgpack_binary_to_hex currently treats a
zero-length BIN as an allocation error and can return -1 (corrupting the map);
modify flb_pack_msgpack_binary_to_hex so that when obj->via.bin.size == 0 it
does not call flb_malloc(0) but instead directly packs an empty string
(msgpack_pack_str with length 0 and no body) and returns 0; additionally, update
the caller flb_pack_msgpack_append_json_metadata to check the return value of
flb_pack_msgpack_binary_to_hex and, on error, pack a safe fallback value (e.g.,
empty string) so the map header count remains consistent and the msgpack stream
cannot become malformed.
🪄 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: 9116438b-b8e7-49f7-ae99-53a23d4c0135

📥 Commits

Reviewing files that changed from the base of the PR and between 5af5840 and a7dca93.

📒 Files selected for processing (3)
  • src/flb_pack.c
  • tests/integration/scenarios/in_opentelemetry/tests/test_in_opentelemetry_001.py
  • tests/internal/pack.c

Comment thread src/flb_pack.c
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
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.

OTEL trace_id and span_id are malformed

1 participant