Class: CloudwatchQuery::Parsers::RailsParser

Inherits:
Base
  • Object
show all
Defined in:
lib/cloudwatch_query/parsers/rails_parser.rb

Constant Summary collapse

BUILT_IN_SUB_PARSERS =

Built-in sub-parsers mapped to symbols

{
  request: Rails::RequestSubParser,
  parameters: Rails::ParametersSubParser,
  redirect: Rails::RedirectSubParser,
  active_job: Rails::ActiveJobSubParser,
  processing: Rails::ProcessingSubParser,
  completed: Rails::CompletedSubParser
}.freeze
REQUEST_ID_REGEX =

Matches request UUID pattern: [abc-123-def-456]

/\[(?<request_id>[a-f0-9-]{36})\]/
SYSLOG_PREFIX_REGEX =

Matches syslog prefix: "Feb 4 22:37:47 ip-10-15-1-216 cryo:"

/^(?<syslog_timestamp>\w+\s+\d+\s+[\d:]+)\s+(?<server>[\w.-]+)\s+\w+\[(?<process_id>\d+)\]:\s*/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RailsParser

Initialize with specific sub-parsers or use all defaults

Examples:

Use all defaults

RailsParser.new

Use only specific built-in sub-parsers

RailsParser.new(:request, :parameters)

Mix built-in and custom sub-parsers

RailsParser.new(:request, :parameters, MyCustomSubParser)

All defaults plus custom

RailsParser.new(:all, MyCustomSubParser)


41
42
43
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 41

def initialize(*args)
  @sub_parsers = resolve_sub_parsers(args)
end

Instance Attribute Details

#sub_parsers ⇒ Object (readonly)

Returns the value of attribute sub_parsers.



25
26
27
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 25

def sub_parsers
  @sub_parsers
end

Class Method Details

.available_sub_parsers ⇒ Object

List available built-in sub-parser names



87
88
89
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 87

def available_sub_parsers
  BUILT_IN_SUB_PARSERS.keys
end

.matches?(message) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 74

def matches?(message)
  new.matches?(message)
end

.parse(message) ⇒ Object



78
79
80
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 78

def parse(message)
  new.parse(message)
end

.parser_name ⇒ Object



82
83
84
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 82

def parser_name
  "rails"
end

Instance Method Details

#matches?(message) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 45

def matches?(message)
  # Must have a Rails request UUID pattern
  message.match?(REQUEST_ID_REGEX)
end

#parse(message) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cloudwatch_query/parsers/rails_parser.rb', line 50

def parse(message)
  return nil unless matches?(message)

  base_data = extract_base_data(message)
  clean_message = strip_syslog_prefix(message)

  # Try each sub-parser until one matches
  sub_parser_data = {}
  @sub_parsers.each do |parser|
    if parser.matches?(clean_message)
      sub_parser_data = parser.parse(clean_message, base_data)
      break if sub_parser_data[:line_type]
    end
  end

  # If no sub-parser matched, mark as unknown
  sub_parser_data[:line_type] ||= :unknown
  sub_parser_data[:raw_message] = message if sub_parser_data[:line_type] == :unknown

  Rails::RailsLog.new(base_data.merge(sub_parser_data))
end