Skip to content

Fix Array message handling in parser methods#62

Merged
silva96 merged 1 commit into
silva96:mainfrom
hecvasro:fix/array-message-handling
Nov 12, 2025
Merged

Fix Array message handling in parser methods#62
silva96 merged 1 commit into
silva96:mainfrom
hecvasro:fix/array-message-handling

Conversation

@hecvasro

Copy link
Copy Markdown
Contributor

Problem

log_bench was crashing with the following error when encountering log entries where the message field was an Array instead of a String:

Error: undefined method 'match?' for an instance of Array

/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/parser.rb:103:in 'LogBench::Log::Parser.job_enqueue_message?'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/parser.rb:77:in 'LogBench::Log::Parser.determine_json_type'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/parser.rb:31:in 'LogBench::Log::Parser.build_specific_entry'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/parser.rb:13:in 'LogBench::Log::Parser.parse_line'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/parser.rb:22:in 'block in LogBench::Log::Parser.parse_lines'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/parser.rb:22:in 'Array#map'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/parser.rb:22:in 'LogBench::Log::Parser.parse_lines'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/collection.rb:73:in 'LogBench::Log::Collection#parse_input'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/collection.rb:12:in 'LogBench::Log::Collection#initialize'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/file.rb:44:in 'Class#new'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/file.rb:44:in 'LogBench::Log::File#collection'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/log/file.rb:16:in 'LogBench::Log::File#requests'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/app/main.rb:71:in 'LogBench::App::Main#load_initial_data'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/lib/log_bench/app/main.rb:29:in 'LogBench::App::Main#run'
/ruby/3.4.7/lib/ruby/gems/3.4.0/gems/log_bench-0.5.2/exe/log_bench:61:in '<top (required)>'
/ruby/3.4.7/bin/log_bench:25:in 'Kernel#load'
/ruby/3.4.7/bin/log_bench:25:in '<main>'

Root Cause

The parser methods (sql_message?, cache_message?, call_stack_message?, job_enqueue_message?) were calling String methods like match?() and include?() directly on data["message"] without checking if it was actually a String. Some logging libraries may output the message field as an Array, causing these methods to fail.

Solution

Added a normalize_message() helper method that safely converts various input types to a String:

  • String: Returns as-is
  • Array: Joins elements with spaces
  • nil: Returns empty string
  • Other types: Converts to string using to_s

All message detection methods now use this helper to ensure they always receive a String, preventing the match? error.

Changes

  • ✅ Added normalize_message() helper method to LogBench::Log::Parser
  • ✅ Updated sql_message?() to use normalized messages
  • ✅ Updated cache_message?() to use normalized messages
  • ✅ Updated call_stack_message?() to use normalized messages
  • ✅ Updated job_enqueue_message?() to use normalized messages
  • ✅ Updated extract_job_id_from_enqueue() to handle Array messages
  • ✅ Updated Entry#initialize to normalize messages when setting content
  • ✅ Updated CallLineEntry to use normalized messages
  • ✅ Added comprehensive test coverage for Array message handling

Testing

Added tests covering:

  • normalize_message() with String, Array, nil, and other types
  • All message detection methods with Array messages
  • End-to-end parsing of log entries with Array messages
  • Job enqueue detection and extraction with Array messages

All existing tests continue to pass, ensuring backward compatibility.

Impact

This fix ensures log_bench works correctly with log formats that use Array messages. The change is backward compatible - String messages continue to work as before.

Some log formats may have the 'message' field as an Array instead of a
String. The parser methods were calling String methods like match?() and
include?() directly on the message field, which caused 'undefined method
match? for an instance of Array' errors.

Changes:
- Add normalize_message() helper method to handle String, Array, nil, and
  other types
- Update all message detection methods (sql_message?, cache_message?,
  call_stack_message?, job_enqueue_message?) to use normalize_message()
- Update Entry class to normalize messages when setting content
- Update CallLineEntry to use normalized messages
- Update extract_job_id_from_enqueue() to handle Array messages
- Add comprehensive test coverage for Array message handling

Fixes issue where log_bench would crash when encountering log entries
with Array messages, particularly affecting job enqueue detection.
@silva96 silva96 self-requested a review November 12, 2025 00:44
@silva96 silva96 assigned silva96 and hecvasro and unassigned silva96 Nov 12, 2025
@silva96 silva96 merged commit 29af974 into silva96:main Nov 12, 2025
1 check passed
@silva96

silva96 commented Nov 12, 2025

Copy link
Copy Markdown
Owner

Thanks for fixing this @hecvasro !!

it's live on 0.5.3

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants