|
144 | 144 | # filter. This newly defined patterns in `pattern_definitions` will not be available outside of that particular `grok` filter.
|
145 | 145 | #
|
146 | 146 | class LogStash::Filters::Grok < LogStash::Filters::Base
|
147 |
| - require 'logstash/filters/grok/timeout_support' |
148 |
| - |
149 |
| - include TimeoutSupport |
150 |
| - |
151 | 147 | config_name "grok"
|
152 | 148 |
|
153 | 149 | # A hash of matches of field => value
|
@@ -289,7 +285,7 @@ def register
|
289 | 285 | @match_counter = metric.counter(:matches)
|
290 | 286 | @failure_counter = metric.counter(:failures)
|
291 | 287 |
|
292 |
| - @timeout = @timeout_millis > 0.0 ? RubyTimeout.new(@timeout_millis) : NoopTimeout.new |
| 288 | + @timeout = @timeout_millis > 0.0 ? RubyTimeout.new(@timeout_millis) : NoopTimeout::INSTANCE |
293 | 289 | @matcher = ( @timeout_scope.eql?('event') ? EventTimeoutMatcher : PatternTimeoutMatcher ).new(self)
|
294 | 290 | end # def register
|
295 | 291 |
|
@@ -479,4 +475,52 @@ def trunc_value
|
479 | 475 | end
|
480 | 476 | end
|
481 | 477 | end
|
| 478 | + |
| 479 | + def with_timeout(context, &block) |
| 480 | + @timeout.exec(&block) |
| 481 | + rescue TimeoutError => error |
| 482 | + handle_timeout(context, error) |
| 483 | + end |
| 484 | + public :with_timeout |
| 485 | + |
| 486 | + def handle_timeout(context, error) |
| 487 | + raise GrokTimeoutException.new(context.grok, context.field, context.input) |
| 488 | + end |
| 489 | + |
| 490 | + # @private |
| 491 | + class GrokContext |
| 492 | + attr_reader :grok, :field, :input |
| 493 | + |
| 494 | + def initialize(field, input) |
| 495 | + @field = field |
| 496 | + @input = input |
| 497 | + end |
| 498 | + |
| 499 | + def set_grok(grok) |
| 500 | + @grok = grok |
| 501 | + end |
| 502 | + end |
| 503 | + |
| 504 | + # @private |
| 505 | + class NoopTimeout |
| 506 | + INSTANCE = new |
| 507 | + |
| 508 | + def exec |
| 509 | + yield |
| 510 | + end |
| 511 | + end |
| 512 | + |
| 513 | + # @private |
| 514 | + class RubyTimeout |
| 515 | + def initialize(timeout_millis) |
| 516 | + # divide by float to allow fractional seconds, the Timeout class timeout value is in seconds but the underlying |
| 517 | + # executor resolution is in microseconds so fractional second parameter down to microseconds is possible. |
| 518 | + # see https://github.com/jruby/jruby/blob/9.2.7.0/core/src/main/java/org/jruby/ext/timeout/Timeout.java#L125 |
| 519 | + @timeout_seconds = timeout_millis / 1000.0 |
| 520 | + end |
| 521 | + |
| 522 | + def exec(&block) |
| 523 | + Timeout.timeout(@timeout_seconds, TimeoutError, &block) |
| 524 | + end |
| 525 | + end |
482 | 526 | end # class LogStash::Filters::Grok
|
0 commit comments