Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created May 11, 2020 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/a40730e2105ba6248aef3a3c36b08afb to your computer and use it in GitHub Desktop.
Save yaauie/a40730e2105ba6248aef3a3c36b08afb to your computer and use it in GitHub Desktop.
###############################################################################
# replace-subset.logstash-filter-ruby.rb
# ---------------------------------
# A script for a Logstash Ruby Filter to replace an event's contents with a
# subset that exists as an object in a field.
###############################################################################
#
# 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`")
@on_error = params.delete('on_error') || 'skip'
ON_ERROR_BEHAVIOURS.include?(@on_error) || report_configuration_error("param `on_error` must be one of #{ON_ERROR_BEHAVIOURS.inspect}")
params.empty? || report_configuration_error("unknown script parameter(s): #{params.keys}.")
end
ON_ERROR_BEHAVIOURS = %w(skip drop).map(&:freeze).freeze
def report_configuration_error(message)
raise LogStash::ConfigurationError, message
end
def filter(event)
timestamp = event.get('@timestamp')
sub_object = event.get(@source)
unless sub_object && sub_object.kind_of?(::Hash)
logger.debug("sub-field `#{@source}` not a map object", :event => event.to_hash) if logger.debug?
return @on_error == 'skip' ? [event] : []
end
clone_event = LogStash::Event.new(sub_object)
clone_event.set('@timestamp', timestamp)
return [clone_event]
rescue => e
logger.error('failed to get subset of event', exception: e.message)
event.tag('_subfield_error')
return @on_error == 'skip' ? [event] : []
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment