-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: event factory support #13017
Feat: event factory support #13017
Changes from 10 commits
62cbfd1
df7b95f
f4d1b9a
130ffba
8c1e1bb
4f96605
71f533a
31f62fb
6c02f00
df3e545
da60663
4377bc5
00fa6e7
e4294e5
d912c50
4973557
aeebb4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'logstash/util/thread_safe_attributes' | ||
|
||
module LogStash | ||
module Plugins | ||
module EventFactorySupport | ||
|
||
include LogStash::Util::ThreadSafeAttributes | ||
|
||
|
||
lazy_init_attr :event_factory do | ||
create_event_factory | ||
end | ||
|
||
lazy_init_attr :targeted_event_factory do | ||
raise ArgumentError.new('config.target not present') unless respond_to?(:target) | ||
target.nil? ? event_factory : TargetedEventFactory(event_factory, target) | ||
end | ||
|
||
private | ||
|
||
# @private Internal API | ||
def create_event_factory | ||
BasicEventFactory::INSTANCE | ||
end | ||
|
||
class BasicEventFactory | ||
INSTANCE = new | ||
|
||
# @param payload [Hash] | ||
# @return [LogStash::Event] | ||
def new_event(payload) | ||
LogStash::Event.new(payload) | ||
end | ||
|
||
end | ||
private_constant :BasicEventFactory | ||
|
||
class TargetedEventFactory | ||
|
||
def initialize(inner, target) | ||
@delegate = inner | ||
@target = target | ||
kares marked this conversation as resolved.
Show resolved
Hide resolved
kares marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
# @param payload [Hash] | ||
# @return [LogStash::Event] | ||
def new_event(payload) | ||
@delegate.new_event(@target => payload) | ||
end | ||
|
||
end | ||
private_constant :TargetedEventFactory | ||
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Licensed to Elasticsearch B.V. under one or more contributor | ||
# license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright | ||
# ownership. Elasticsearch B.V. licenses this file to you under | ||
# the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
module LogStash | ||
module Util | ||
module ThreadSafeAttributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 clean reusable implementation, really cleans up the details of using them. |
||
|
||
def self.included(base) | ||
base.extend ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
|
||
def lazy_init_attr(attribute, &block) | ||
raise ArgumentError.new("invalid attribute name: #{attribute}") unless attribute.match? /^[_A-Za-z]\w*$/ | ||
raise ArgumentError.new('no block given') unless block_given? | ||
var_name = "@#{attribute}".to_sym | ||
send(:define_method, attribute.to_sym) do | ||
if instance_variable_defined?(var_name) | ||
instance_variable_get(var_name) | ||
else | ||
LogStash::Util.synchronize(self) do | ||
if instance_variable_defined?(var_name) | ||
instance_variable_get(var_name) | ||
else | ||
instance_variable_set(var_name, instance_eval(&block)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.logstash; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* A factory for events. | ||
*/ | ||
@FunctionalInterface | ||
public interface EventFactory { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new interface very nearly is implemented by the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yaauie 💯 thanks I completely missed the existing |
||
|
||
Event newEvent(Map<String, Object> data); | ||
|
||
default Event newEvent() { | ||
return newEvent(Collections.emptyMap()); | ||
} | ||
|
||
/** | ||
* A default event factory implementation. | ||
*/ | ||
EventFactory DEFAULT = (data) -> new Event(data); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we drop this method for now? to align with the support mixin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's private, so not necessarily something we need to align on. It is also a good hook point for future efforts in LS core that don't need backport support (things like alternate
Event
implementation experiments like copy-on-write or field reference parser changes), so I expect even if we remove it now we will add it back in short order.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, sticking around just wanted to double check given what
private
means in Ruby land 😉