class PlaceholderExpander

Modified PlaceholderExpander borrowed from fluent-plugin-record-reformer github.com/sonots/fluent-plugin-record-reformer Original author: Naotoshi Seo MIT License: github.com/sonots/fluent-plugin-record-reformer/blob/master/LICENSE Modifications: Michael Karlesky Changes:

- Stripped down to limited use case
- Building of placeholders is now recursive to handle any depth of hash, array, and primitive

Attributes

log[R]
placeholders[R]

Public Class Methods

new(params) click to toggle source
# File lib/fluent/plugin/out_pagerduty.rb, line 83
def initialize(params)
  @log = params[:log]
end

Public Instance Methods

expand(str, placeholders) click to toggle source

Expand string (`${foo}`) with placeholders

# File lib/fluent/plugin/out_pagerduty.rb, line 108
def expand(str, placeholders)
  single_placeholder_matched = str.match(/\A(\${[^}]+}|__[A-Z_]+__)\z/)
  if single_placeholder_matched
    log_if_unknown_placeholder($1, placeholders)
    return placeholders[single_placeholder_matched[1]]
  end
  str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) {
    log_if_unknown_placeholder($1, placeholders)
    placeholders[$1]
  }
end
prepare_placeholders(placeholder_values) click to toggle source
# File lib/fluent/plugin/out_pagerduty.rb, line 87
def prepare_placeholders(placeholder_values)
  placeholders = {}

  placeholder_values.each do |key, value|
    # For any entry in `placeholder_values` that is a hash, we effectively ignore its internal name.
    # For example, `record` is an important hash, but referencing it at the top-level is superfluous namespacing.
    # Instead of '${record["Node"]["Location"]}',
    #  '${Node["Location"]}' more closely maps to a user's knowledge of a data to be processed.
    if value.kind_of?(Hash)
      value.each do |key, value|
        build_placeholders(placeholders, key, value)
      end
    else
      build_placeholders(placeholders, key, value)        
    end
  end

  placeholders
end

Private Instance Methods

build_placeholders(placeholders, key, value) click to toggle source

Recurses to build any depth of hash, arrays, and primitives

# File lib/fluent/plugin/out_pagerduty.rb, line 123
def build_placeholders(placeholders, key, value)
  if value.kind_of?(Array) # tag_parts, etc
    size = value.size
    value.each_with_index do |v, idx|
      build_placeholders(placeholders, "#{key}[#{idx}]", v)
      build_placeholders(placeholders, "#{key}[#{idx-size}]", v) # support [-1]
    end
  elsif value.kind_of?(Hash) # record, etc
    value.each do |k, v|
      build_placeholders(placeholders, %Q[#{key}["#{k}"]], v) # record["foo"]
    end
  else
    placeholders.store("${#{key}}", value)
  end
end
log_if_unknown_placeholder(placeholder, placeholders) click to toggle source
# File lib/fluent/plugin/out_pagerduty.rb, line 139
def log_if_unknown_placeholder(placeholder, placeholders)
  unless placeholders.include?(placeholder)
    log.warn "pagerduty: unknown placeholder `#{placeholder}` found"
  end
end