Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created December 15, 2022 01:30
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/d9968babf698c8b9a9c4f9d360d06e27 to your computer and use it in GitHub Desktop.
Save yaauie/d9968babf698c8b9a9c4f9d360d06e27 to your computer and use it in GitHub Desktop.
###############################################################################
# determine-field-type.logstash-filter-ruby.rb
# ---------------------------------
# A script for a Logstash Ruby Filter to determine a field's type
###############################################################################
#
# Copyright 2022 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 whose type needs to be determined
@source = params.delete('source') or report_configuration_error("`source` is required!")
# target: where to place the type of the field (defaults to a same-nested path in [@metadata][typeinfo])
@target = params.delete('target') { "[@metadata][typeinfo][#{@source}]" }
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)
type = :unset unless event.include?(@source)
type ||= case event.get(@source)
when nil then :nil
when String then :string
when true, false then :boolean
when Numeric then :number
when Array then :array
when Hash then :'key-value-map'
else :unknown
end
event.set(@target, type.to_s)
rescue => e
logger.error('failed to determine type', exception: e.message)
event.tag('_type_determination_error')
ensure
return [event]
end
###############################
# TESTING
common_test_event = {
"int" => 1,
"str" => "fubar",
"empty_array" => [],
"empty_hash" => {},
"array" => [
{"int" => 12},
{"str" => "foobar"},
{"empty_hash" => {}},
{"empty_array" => []}
],
"boolean-true" => true,
"boolean-false" => false,
"hash" => {
"int" => 123,
"str" => "FUBAR",
"empty_hash" => {},
"empty_array" => [],
"non-empty_hash" => {
"a" => "b",
"nested" => {
"another" => "level",
"so" => "deep",
},
"empty_hash" => {},
}
}
}
shared_event_provider = Proc.new do
in_event { ::Marshal.load(::Marshal.dump(common_test_event)) }
end
{
'boolean-true' => 'boolean',
'boolean-false' => 'boolean',
'missing' => 'unset',
'str' => 'string',
'int' => 'number',
'hash' => 'key-value-map',
'array' => 'array',
'empty_array' => 'array',
'[hash][str]' => 'string',
'[hash][int]' => 'number',
}.each do |field, expected_type|
test "detect #{field} as #{expected_type}" do
parameters { { 'source' => field } }
instance_eval(&shared_event_provider)
expect("detect field `#{field}` to have type `#{expected_type}`") do |events|
event = events.first
event.get("[@metadata][typeinfo][#{field}]") == expected_type
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment