Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created September 10, 2020 16:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/cad23f232f0734b27721af1338443d36 to your computer and use it in GitHub Desktop.
Save yaauie/cad23f232f0734b27721af1338443d36 to your computer and use it in GitHub Desktop.
stringify a timestamp field
###############################################################################
# stringify-timestamp.logstash-filter-ruby.rb
# ---------------------------------
# A script for a Logstash Ruby Filter to stringify logstash timestamps using
# ISO-8601.
###############################################################################
#
# 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
# source: the source field to convert
@source = params.delete('source') || report_configuration_error("missing required param `source`.")
# target: the target for the converted result
# if a target is not provided, conversion will be made in-place
@target = params.delete('target') || @source
report_configuration_error("cannot be used to convert field `#{@target}`") if %w(@timestamp [@timestamp]).include?(@target)
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)
source_field = event.get(@source)
return [event] unless source_field
case
when source_field.respond_to?(:to_iso8601)
event.set(@target, source_field.to_iso8601)
when souece_field.respond_to?(:iso8601)
event.set(@target, source_field.iso8601)
else
fail("source field `#{@source}` not a convertable timestamp") unless source_field.respond_to?(:to_iso8601)
end
rescue => e
logger.error('failed to stringify timestamp', exception: e.message)
event.tag('_stringifytimestamperror')
ensure
return [event]
end
filter {
  ruby {
    path => "${PWD}/stringify-timestamp.logstash-filter-ruby.rb"
    script_params => {
      source => "[timestamp_field]"
    }
  }
}

filter {
  ruby {
    path => "${PWD}/stringify-timestamp.logstash-filter-ruby.rb"
    script_params => {
      source => "[timestamp_field]"
      target => "[destination_field]"
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment