Skip to content

out_logdna: stop duplicating promoted keys in line field - #12120

Open
locker95 wants to merge 1 commit into
fluent:masterfrom
locker95:fix/logdna-line-promoted-keys-11754
Open

out_logdna: stop duplicating promoted keys in line field#12120
locker95 wants to merge 1 commit into
fluent:masterfrom
locker95:fix/logdna-line-promoted-keys-11754

Conversation

@locker95

@locker95 locker95 commented Jul 20, 2026

Copy link
Copy Markdown

Summary

Fixes #11754.

The out_logdna plugin promotes primary keys (meta, level/severity, app, file) to top-level Mezmo ingest fields, but previously still serialized the full record into line. That produced duplicates (e.g. top-level meta plus _meta from the JSON inside line).

This change:

  1. Prefers message, then log, as the plain-text line value when present as a string (matches Mezmo's "Text of the log line" semantics).
  2. Defaults exclude_promoted_keys to true, so when there is no message/log text field the remaining non-primary keys are serialized into line without re-including promoted keys.
  3. Keeps legacy full-record-in-line behavior available with exclude_promoted_keys false (when no string message/log is used).

Example payload (with message + meta)

Before (duplication):

{
  "lines": [{
    "meta": {"source": "test"},
    "level": "info",
    "app": "myapp",
    "timestamp": 12345678,
    "line": "{\"message\":\"hello world\",\"meta\":{\"source\":\"test\"},\"level\":\"info\",\"app\":\"myapp\"}"
  }]
}

After:

{
  "lines": [{
    "meta": {"source": "test"},
    "level": "info",
    "app": "myapp",
    "timestamp": 12345678,
    "line": "hello world"
  }]
}

Testing

Environment did not have cmake available, so full compile/runtime tests were not run here. Runtime coverage is in tests/runtime/out_logdna.c.

Manual verification

# build with runtime tests
cmake -DFLB_TESTS_RUNTIME=On -DFLB_OUT_LOGDNA=On ..
make -j"$(nproc)" flb-rt-out_logdna
./bin/flb-rt-out_logdna

Expected: all tests pass, including:

  • non_duplication — primary keys not re-serialized into line; message used as plain text
  • log_key_as_line / message_preferred_over_logmessage then log preference
  • data_completeness — without message/log, non-primary keys remain in line JSON; promoted keys excluded
  • backward_compatexclude_promoted_keys false still serializes full record when no message/log text field

Optional end-to-end config

service:
  flush: 1
  log_level: info

pipeline:
  inputs:
    - name: dummy
      tag: test
      dummy: '{"message":"hello","meta":{"env":"prod"},"level":"info","app":"myapp"}'

  outputs:
    - name: logdna
      match: test
      api_key: YOUR_API_KEY
      hostname: test-host
      # exclude_promoted_keys defaults to true

Capture the HTTP body (or use a local HTTP sink) and confirm line is "hello" and meta/level/app appear only at the top level of each lines[] entry.

Documentation

  • Existing exclude_promoted_keys docs should note the new default (true) and that a string message/log field is preferred for line.

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

    • LogDNA output now uses the message field as plain-text line content when available.
    • Falls back to the log field when message is absent.
    • Promoted fields are excluded from the line content by default to prevent duplication.
    • Legacy full-record line formatting remains available through configuration.
  • Bug Fixes

    • Improved output formatting and field handling for LogDNA records, including records with both message and log fields.

Prefer a string message/log field as the Mezmo ingest "line" text.
When neither is present, exclude promoted keys (meta, level/severity,
app, file) from the line JSON body by default so they are not
duplicated. Legacy full-record serialization remains available via
exclude_promoted_keys false.

Fixes fluent#11754

Signed-off-by: Dean Chen <862469039@qq.com>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The LogDNA output formatter now prefers string message or log values as plain-text line content, filters promoted keys from serialized line data by default, and preserves an explicit legacy mode. Runtime tests cover precedence, non-duplication, filtering, and compatibility.

Changes

LogDNA line behavior

Layer / File(s) Summary
Line selection contract
plugins/out_logdna/logdna.c
Defines message and log keys, extracts string values with message precedence, and updates exclude_promoted_keys defaults and documentation.
Payload line composition
plugins/out_logdna/logdna.c
Packs extracted text directly, otherwise serializes filtered non-promoted fields or the full record in legacy mode.
Runtime behavior and compatibility tests
tests/runtime/out_logdna.c
Updates existing expectations and adds coverage for log fallback, message precedence, non-duplication, and legacy serialization.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Record
  participant logdna_compose_payload
  participant record_find_line_text
  participant LogDNA payload
  Record->>logdna_compose_payload: provide msgpack record
  logdna_compose_payload->>record_find_line_text: search message then log
  record_find_line_text-->>logdna_compose_payload: return text or no match
  logdna_compose_payload->>LogDNA payload: pack text or serialized JSON line
Loading

Possibly related PRs

Suggested reviewers: edsiper

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.55% 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 accurately summarizes the main change: preventing duplicated promoted keys in the out_logdna line field.
Linked Issues check ✅ Passed The PR excludes promoted keys from line and prefers message/log text, matching the bug fix requested in #11754.
Out of Scope Changes check ✅ Passed The changes stay focused on logdna line composition and matching runtime tests, with no clear unrelated additions.
✨ 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 65af712489

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +304 to +309
if (use_line_text) {
/* Plain text of the log line (Mezmo ingest "line" field) */
msgpack_pack_str(&mp_pck, line_text->via.str.size);
msgpack_pack_str_body(&mp_pck,
line_text->via.str.ptr,
line_text->via.str.size);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve non-primary fields when selecting message

For any record containing a string message/log plus non-primary fields, this branch emits only that text as line; record_append_primary_keys() has already packed only promoted fields into the outer object, so fields such as host and custom are silently dropped. This also happens with exclude_promoted_keys false, because use_line_text bypasses the legacy full-record branch. For example, the existing JSON_LEVEL_AND_SEVERITY fixture includes host, but the updated assertion no longer verifies it survives. Keep the remaining non-promoted fields in the emitted payload or make this lossy behavior explicitly opt-in.

Useful? React with 👍 / 👎.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
plugins/out_logdna/logdna.c (1)

304-337: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Missing NULL check on flb_msgpack_to_json_str() result before strlen(); logic now duplicated across two branches.

line_json is used with strlen(line_json) right after flb_msgpack_to_json_str() in both the new build_line_map branch (317-326) and the legacy branch (329-335) without checking for NULL (e.g. on allocation failure), which would crash on strlen(NULL). The pack/free sequence is now duplicated in two places; consider extracting a small helper (pack_json_str_as_line(mp_pck, msgpack_object, ...)) that also guards against a NULL return.

🛡️ Proposed guard
             line_json = flb_msgpack_to_json_str(1024, &mp_line_result.data,
                                                 config->json_escape_unicode);

             msgpack_unpacked_destroy(&mp_line_result);
             msgpack_sbuffer_destroy(&mp_line_sbuf);

-            len = strlen(line_json);
-            msgpack_pack_str(&mp_pck, len);
-            msgpack_pack_str_body(&mp_pck, line_json, len);
-            flb_free(line_json);
+            if (line_json) {
+                len = strlen(line_json);
+                msgpack_pack_str(&mp_pck, len);
+                msgpack_pack_str_body(&mp_pck, line_json, len);
+                flb_free(line_json);
+            }
+            else {
+                msgpack_pack_str(&mp_pck, 0);
+                msgpack_pack_str_body(&mp_pck, "", 0);
+            }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@plugins/out_logdna/logdna.c` around lines 304 - 337, Update the JSON
serialization paths in the build_line_map and legacy branches to handle a NULL
result from flb_msgpack_to_json_str before calling strlen, packing, or freeing
it; preserve safe behavior on serialization failure. Remove the duplicated
line_json packing sequence by extracting or reusing a helper that performs the
NULL check and packs the serialized string into mp_pck.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@plugins/out_logdna/logdna.c`:
- Around line 304-337: Update the JSON serialization paths in the build_line_map
and legacy branches to handle a NULL result from flb_msgpack_to_json_str before
calling strlen, packing, or freeing it; preserve safe behavior on serialization
failure. Remove the duplicated line_json packing sequence by extracting or
reusing a helper that performs the NULL check and packs the serialized string
into mp_pck.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 10fea5f1-0b73-416e-b78d-930a766cffb9

📥 Commits

Reviewing files that changed from the base of the PR and between fd5ea1f and 65af712.

📒 Files selected for processing (2)
  • plugins/out_logdna/logdna.c
  • tests/runtime/out_logdna.c

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.

logdna output includes the full log event in the line field

1 participant