Class: LogStash::Filters::Grok::TimeoutEnforcer

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/grok/timeout_enforcer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, timeout_nanos) ⇒ TimeoutEnforcer

Returns a new instance of TimeoutEnforcer.



6
7
8
9
10
11
12
13
14
15
# File 'lib/logstash/filters/grok/timeout_enforcer.rb', line 6

def initialize(logger, timeout_nanos)
  @logger = logger
  @running = false
  @timeout_nanos = timeout_nanos

  # Stores running matches with their start time, this is used to cancel long running matches
  # Is a map of Thread => start_time
  @threads_to_start_time = {}
  @state_lock = ReentrantLock.new
end

Instance Attribute Details

#runningObject (readonly)

Returns the value of attribute running.



4
5
6
# File 'lib/logstash/filters/grok/timeout_enforcer.rb', line 4

def running
  @running
end

Instance Method Details

#grok_till_timeout(event, grok, field, value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstash/filters/grok/timeout_enforcer.rb', line 17

def grok_till_timeout(event, grok, field, value)
  begin
    thread = java.lang.Thread.currentThread()
    start_thread_groking(thread)
    yield
  rescue InterruptedRegexpError => e
    raise ::LogStash::Filters::Grok::TimeoutException.new(grok, field, value)
  ensure
    stop_thread_groking(thread)
    # Clear any interrupts from any previous invocations that were not caught by Joni
    # It may appear that this should go in #stop_thread_groking but that would actually
    # break functionality! If this were moved there we would clear the interrupt
    # immediately after setting it in #cancel_timed_out, hence this MUST be here
    thread.interrupted
  end
end

#start!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/logstash/filters/grok/timeout_enforcer.rb', line 34

def start!
  @running = true
  @timer_thread = Thread.new do
    while @running
      begin
        cancel_timed_out!
      rescue Exception => e
        @logger.error("Error while attempting to check/cancel excessively long grok patterns",
                      :message => e.message,
                      :class => e.class.name,
                      :backtrace => e.backtrace
                     )
      end
      sleep 0.25
    end
  end
end

#stop!Object



52
53
54
55
56
# File 'lib/logstash/filters/grok/timeout_enforcer.rb', line 52

def stop!
  @running = false
  # Check for the thread mostly for a fast start/shutdown scenario
  @timer_thread.join if @timer_thread
end