forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Allow plugins author to decide how their plugins are structured - Allow a new kind of plugin that allow plugins author to add hooks into logstash core. Fixes elastic#6109
- Loading branch information
Showing
16 changed files
with
479 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# encoding: utf-8 | ||
module LogStash | ||
class EventDispatcher | ||
java_import "java.util.concurrent.CopyOnWriteArrayList" | ||
|
||
attr_reader :emitter | ||
|
||
def initialize(emitter) | ||
@emitter = emitter | ||
@listeners = CopyOnWriteArrayList.new | ||
end | ||
|
||
# This operation is slow because we use a CopyOnWriteArrayList | ||
# But the majority of the addition will be done at bootstrap time | ||
# So add_listener shouldn't be called often at runtime. | ||
# | ||
# On the other hand the notification could be called really often. | ||
def add_listener(listener) | ||
@listeners.add(listener) | ||
end | ||
|
||
# This operation is slow because we use a `CopyOnWriteArrayList` as the backend, instead of a | ||
# ConcurrentHashMap, but since we are mostly adding stuff and iterating the `CopyOnWriteArrayList` | ||
# should provide a better performance. | ||
# | ||
# See note on add_listener, this method shouldn't be called really often. | ||
def remove_listener(listener) | ||
@listeners.remove(listener) | ||
end | ||
|
||
def fire(method_name, *arguments) | ||
@listeners.each do |listener| | ||
if listener.respond_to?(method_name) | ||
if arguments.size > 0 | ||
listener.send(method_name, emitter, *arguments) | ||
else | ||
listener.send(method_name, emitter) | ||
end | ||
end | ||
end | ||
end | ||
alias_method :execute, :fire | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# encoding: utf-8 | ||
module LogStash module Plugins | ||
# This calls allow logstash to expose the endpoints for listeners | ||
class HooksRegistry | ||
java_import "java.util.concurrent.ConcurrentHashMap" | ||
java_import "java.util.concurrent.CopyOnWriteArrayList" | ||
|
||
def initialize | ||
@registered_emmitters = ConcurrentHashMap.new | ||
@registered_hooks = ConcurrentHashMap.new | ||
end | ||
|
||
def register_emitter(emitter_scope, dispatcher) | ||
@registered_emmitters.put(emitter_scope, dispatcher) | ||
sync_hooks | ||
end | ||
|
||
def remove_emitter(emitter_scope) | ||
@registered_emmitters.remove(emitter_scope) | ||
end | ||
|
||
def register_hooks(emitter_scope, callback) | ||
callbacks = @registered_hooks.computeIfAbsent(emitter_scope) do | ||
CopyOnWriteArrayList.new | ||
end | ||
|
||
callbacks.add(callback) | ||
sync_hooks | ||
end | ||
|
||
def emmitters_count | ||
@registered_emmitters.size | ||
end | ||
|
||
def hooks_count(emitter_scope = nil) | ||
if emitter_scope.nil? | ||
@registered_hooks.elements().collect(&:size).reduce(0, :+) | ||
else | ||
callbacks = @registered_hooks.get(emitter_scope) | ||
callbacks.nil? ? 0 : @registered_hooks.get(emitter_scope).size | ||
end | ||
end | ||
|
||
private | ||
def sync_hooks | ||
@registered_emmitters.each do |emitter, dispatcher| | ||
listeners = @registered_hooks.get(emitter) | ||
|
||
unless listeners.nil? | ||
listeners.each do |listener| | ||
dispatcher.add_listener(listener) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end end |
Oops, something went wrong.