Skip to content

Instantly share code, notes, and snippets.

@yaauie
Last active February 2, 2021 09:29
Show Gist options
  • Save yaauie/35f6d459077ef72d26627e7dd9ea9872 to your computer and use it in GitHub Desktop.
Save yaauie/35f6d459077ef72d26627e7dd9ea9872 to your computer and use it in GitHub Desktop.
###############################################################################
# pull-up.logstash-filter-ruby.rb
# ---------------------------------
# A script for a Logstash Ruby Filter to pull entries from a key/value map up
# to the root.
###############################################################################
#
# Copyright 2020 Ry Biesemeyer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
def register(params)
params = params.dup # isolate
@source = params.delete('source') || report_configuration_error("missing required param `source`")
@prefix = params.delete('prefix').to_s
params.empty? || report_configuration_error("unknown script parameter(s): #{params.keys}.")
end
def report_configuration_error(message)
raise LogStash::ConfigurationError, message
end
def filter(event)
timestamp = event.get('@timestamp')
sub_object = event.get(@source)
return [event] unless event.include?(@source)
unless event.get(@source).kind_of?(::Hash)
logger.debug("sub-field `#{@source}` not a key/value map object", :event => event.to_hash) if logger.debug?
return [event]
end
# to prevent partial failure, we clone the event and return the
# modified clone on success or the original unmodified event
# on failure
clone_event = LogStash::Event.new(event.to_hash_with_metadata)
sub_field = clone_event.remove(@source)
sub_field.each do |key, value|
clone_event.set("#{@prefix}#{key}", value)
end
event.cancel
return [clone_event]
rescue => e
logger.error('failed to pull fields up on event', exception: e.message, event: event.to_hash, source: @source)
event.tag('_pullup_error')
[event]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment