-
-
Notifications
You must be signed in to change notification settings - Fork 159
flatten json log messages inside log record body into individual fields #1522
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
base: main
Are you sure you want to change the base?
flatten json log messages inside log record body into individual fields #1522
Conversation
if a log event is json, with otel-collector, log records's body will contain the entire log event in order to make it usable and queryable, we flatten the json into individual columns original body field is also pertained for backward compatibility if log event is unstructured (not a json), it remains as-is in body field
WalkthroughAdded JSON-string parsing and flattening to OTEL LogRecord body processing: when a body field is a string that parses to a JSON object, the inner object is flattened and its keys are added as top-level attributes prefixed by "{original_key}_{inner_key}", while retaining the original body field. Changes
Sequence Diagram(s)(Skipped — change is localized to a single function and does not introduce multi-component sequential flows.) Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
There was a problem hiding this 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
🤖 Fix all issues with AI agents
In `@src/otel/logs.rs`:
- Around line 101-112: The current loop that handles Value::String -> parsed ->
generic_flattening stores flattened objects into the same map (log_record_json)
so array elements overwrite each other; update the logic in the block
referencing generic_flattening, log_record_json, prefixed_key and
flattened_value to either skip flattening when generic_flattening returns
multiple Value::Object items (preserve the original body field) or, preferably,
enumerate array elements and inject keys with indexed prefixes (e.g.,
format!("{key}_{index}_{inner_key}") for each flattened_value) so fields from
all array elements are preserved; if the overwrite behavior is intentional, add
a clear comment near generic_flattening usage documenting that only the last
array element is kept.
🧹 Nitpick comments (1)
src/otel/logs.rs (1)
96-114: Potential key collision between original and flattened fields.If
body_jsoncontains a key that matches a generated prefixed key, the flattened value will silently overwrite the original. For example, ifbody_jsonhas both("body_foo", ...)and("body", "{\"foo\": ...}"), the flattenedbody_foowill replace the original.Consider checking for key existence before inserting flattened fields to avoid silent overwrites:
💡 Suggested approach
for (inner_key, inner_value) in flattened_obj { let prefixed_key = format!("{key}_{inner_key}"); + // Avoid overwriting existing keys from original body + if !log_record_json.contains_key(&prefixed_key) { log_record_json.insert(prefixed_key, inner_value); + } }
if a log event is json, with otel-collector, log records's body will contain the entire log event
in order to make it usable and queryable, we flatten the json into individual columns
original body field is also pertained for backward compatibility
if log event is unstructured (not a json), it remains as-is in body field
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.