class Fluent::Plugin::QueryParamsExtractor

Attributes

log[R]

Public Class Methods

new(plugin, conf) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 10
def initialize(plugin, conf)
  @log = plugin.log

  if plugin.is_a?(Fluent::Plugin::Output)
    unless have_tag_option?(plugin)
      raise Fluent::ConfigError, "out_extract_query_params: At least one of remove_tag_prefix/remove_tag_suffix/add_tag_prefix/add_tag_suffix is required to be set."
    end
  end

  @key = plugin.key
  @only = plugin.only
  @except = plugin.except
  @discard_key = plugin.discard_key
  @add_field_prefix = plugin.add_field_prefix
  @permit_blank_key = plugin.permit_blank_key

  @add_url_scheme = plugin.add_url_scheme
  @add_url_host = plugin.add_url_host
  @add_url_port = plugin.add_url_port
  @add_url_path = plugin.add_url_path

  if @only
    @include_keys = @only.split(/\s*,\s*/).inject({}) do |hash, i|
      hash[i] = true
      hash
    end
  end

  if @except
    @exclude_keys = @except.split(/\s*,\s*/).inject({}) do |hash, i|
      hash[i] = true
      hash
    end
  end
end

Public Instance Methods

add_query_params_field(record) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 46
def add_query_params_field(record)
  return record unless record[@key]
  url = parse_url(record[@key])
  add_url_scheme(url, record)
  add_url_host(url, record)
  add_url_port(url, record)
  add_url_path(url, record)
  add_query_params(url, record)
  record.delete(@key) if @discard_key
  record
end

Private Instance Methods

add_field_prefix?() click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 105
def add_field_prefix?
  !!@add_field_prefix
end
add_query_params(url, record) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 113
def add_query_params(url, record)
  return if url.query.nil?
  url.query.split('&').each do |pair|
    key, value = pair.split('=', 2).map { |i| CGI.unescape(i) }
    next if (key.nil? || key.empty?) && (!permit_blank_key? || value.nil? || value.empty?)
    key ||= ''
    value ||= ''

    key = create_field_key(key)
    if @only
      record[key] = value if @include_keys.has_key?(key)
    elsif @except
      record[key] = value if !@exclude_keys.has_key?(key)
    else
      record[key] = value
    end
  end
end
add_url_host(url, record) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 87
def add_url_host(url, record)
  return unless @add_url_host
  url_host_key = create_field_key('url_host')
  record[url_host_key] = url.host || ''
end
add_url_path(url, record) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 99
def add_url_path(url, record)
  return unless @add_url_path
  url_path_key = create_field_key('url_path')
  record[url_path_key] = url.path || ''
end
add_url_port(url, record) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 93
def add_url_port(url, record)
  return unless @add_url_port
  url_port_key = create_field_key('url_port')
  record[url_port_key] = url.port || ''
end
add_url_scheme(url, record) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 81
def add_url_scheme(url, record)
  return unless @add_url_scheme
  url_scheme_key = create_field_key('url_scheme')
  record[url_scheme_key] = url.scheme || ''
end
create_field_key(field_key) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 73
def create_field_key(field_key)
  if add_field_prefix?
    "#{@add_field_prefix}#{field_key}"
  else
    field_key
  end
end
have_tag_option?(plugin) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 60
def have_tag_option?(plugin)
  plugin.remove_tag_prefix ||
    plugin.remove_tag_suffix ||
    plugin.add_tag_prefix    ||
    plugin.add_tag_suffix
end
parse_url(url_string) click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 67
def parse_url(url_string)
  URI.parse(url_string)
rescue URI::InvalidURIError
  URI.parse(WEBrick::HTTPUtils.escape(url_string))
end
permit_blank_key?() click to toggle source
# File lib/fluent/plugin/query_params_extractor.rb, line 109
def permit_blank_key?
  @permit_blank_key
end