Class: Fluent::ExceptionDetector
- Inherits:
-
Object
- Object
- Fluent::ExceptionDetector
- Defined in:
- lib/fluent/plugin/exception_detector.rb
Overview
State machine that consumes individual log lines and detects multi-line stack traces.
Instance Method Summary collapse
-
#initialize(*languages) ⇒ ExceptionDetector
constructor
A new instance of ExceptionDetector.
- #reset ⇒ Object
-
#update(line) ⇒ Object
Updates the state machine and returns the trace detection status: - no_trace: ‘line’ does not belong to an exception trace, - start_trace: ‘line’ starts a detected exception trace, - inside: ‘line’ is part of a detected exception trace, - end: the detected exception trace ends after ‘line’.
Constructor Details
#initialize(*languages) ⇒ ExceptionDetector
Returns a new instance of ExceptionDetector.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/fluent/plugin/exception_detector.rb', line 140 def initialize(*languages) @state = :start_state @rules = Hash.new { |h, k| h[k] = [] } languages = [:all] if languages.empty? languages.each do |lang| rule_config = ExceptionDetectorConfig::RULES_BY_LANG.fetch(lang.downcase) do |_k| raise ArgumentError, "Unknown language: #{lang}" end rule_config.each do |r| target = ExceptionDetectorConfig::RuleTarget.new(r[:pattern], r[:to_state]) @rules[r[:from_state]] << target end end @rules.each_value(&:uniq!) end |
Instance Method Details
#reset ⇒ Object
188 189 190 |
# File 'lib/fluent/plugin/exception_detector.rb', line 188 def reset @state = :start_state end |
#update(line) ⇒ Object
Updates the state machine and returns the trace detection status:
-
no_trace: ‘line’ does not belong to an exception trace,
-
start_trace: ‘line’ starts a detected exception trace,
-
inside: ‘line’ is part of a detected exception trace,
-
end: the detected exception trace ends after ‘line’.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/fluent/plugin/exception_detector.rb', line 167 def update(line) trace_seen_before = transition(line) # If the state machine fell back to the start state because there is no # defined transition for 'line', trigger another state transition because # 'line' may contain the beginning of another exception. transition(line) unless trace_seen_before new_state = @state trace_seen_after = new_state != :start_state case [trace_seen_before, trace_seen_after] when [true, true] :inside_trace when [true, false] :end_trace when [false, true] :start_trace else :no_trace end end |