class LogStash::Outputs::SumoLogic::PayloadBuilder

Constants

ALWAYS_EXCLUDED
JSON_PLACEHOLDER
METRICS_NAME_TAG
TIMESTAMP_FIELD

Public Class Methods

new(stats, config) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 16
def initialize(stats, config)
  @stats = stats
  
  @format = config["format"] ||= DEFAULT_LOG_FORMAT
  @json_mapping = config["json_mapping"]

  @metrics = config["metrics"]
  @metrics_name = config["metrics_name"]
  @fields_as_metrics = config["fields_as_metrics"]
  @metrics_format = (config["metrics_format"] ||= CARBON2).downcase
  @intrinsic_tags = config["intrinsic_tags"] ||= {}
  @meta_tags = config["meta_tags"] ||= {}
  @fields_include = config["fields_include"] ||= []
  @fields_exclude = config["fields_exclude"] ||= []

end

Public Instance Methods

build(event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 33
def build(event)
  payload = if @metrics || @fields_as_metrics
    build_metrics_payload(event)
  else
    build_log_payload(event)
  end
  payload
end

Private Instance Methods

apply_template(template, event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 129
def apply_template(template, event)
  if template == JSON_PLACEHOLDER
    hash = event2hash(event)
    LogStash::Json.dump(hash)
  elsif template.include? JSON_PLACEHOLDER
    result = event.sprintf(template)
    hash = event2hash(event)
    dump = LogStash::Json.dump(hash)
    result.gsub(JSON_PLACEHOLDER) { dump }
  else
    event.sprintf(template)
  end
end
build_log_payload(event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 44
def build_log_payload(event)
  @stats.record_log_process()
  apply_template(@format, event)
end
build_metrics_payload(event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 49
def build_metrics_payload(event)
  timestamp = event.get(TIMESTAMP_FIELD).to_i
  source = if @fields_as_metrics
    event_as_metrics(event)
  else
    expand_hash(@metrics, event)
  end
  lines = source.flat_map { |key, value|
    get_single_line(event, key, value, timestamp)
  }.reject(&:nil?)
  @stats.record_metrics_process(lines.size)
  lines.join($/)
end
dotify(acc, key, value, prefix) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 88
def dotify(acc, key, value, prefix)
  pk = prefix ? "#{prefix}.#{key}" : key.to_s
  if value.is_a?(Hash)
    value.each do |k, v|
      dotify(acc, k, v, pk)
    end
  elsif value.is_a?(Array)
    value.each_with_index.map { |v, i|
      dotify(acc, i.to_s, v, pk)
    }
  else
    acc[pk] = value
  end
end
event2hash(event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 103
def event2hash(event)
  if @json_mapping
    @json_mapping.reduce({}) do |acc, kv|
      k, v = kv
      acc[k] = event.sprintf(v)
      acc
    end
  else
    event.to_hash
  end
end
event_as_metrics(event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 63
def event_as_metrics(event)
  hash = event2hash(event)
  acc = {}
  hash.keys.each do |field|
    value = hash[field]
    dotify(acc, field, value, nil)
  end
  acc
end
expand_hash(hash, event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 119
def expand_hash(hash, event)
  hash.reduce({}) do |acc, kv|
    k, v = kv
    exp_k = apply_template(k, event)
    exp_v = apply_template(v, event)
    acc[exp_k] = exp_v
    acc
  end
end
get_metrics_name(event, name) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 143
def get_metrics_name(event, name)
  name = @metrics_name.gsub(METRICS_NAME_PLACEHOLDER) { name } if @metrics_name
  event.sprintf(name)
end
get_single_line(event, key, value, timestamp) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 73
def get_single_line(event, key, value, timestamp)
  full = get_metrics_name(event, key)
  if !ALWAYS_EXCLUDED.include?(full) &&  \
    (@fields_include.empty? || @fields_include.any? { |regexp| full.match(regexp) }) && \
    !(@fields_exclude.any? {|regexp| full.match(regexp)}) && \
    is_number?(value)
    if @metrics_format == GRAPHITE
      "#{full} #{value} #{timestamp}" 
    else
      @intrinsic_tags[METRICS_NAME_TAG] = full
      "#{hash2line(@intrinsic_tags, event)} #{hash2line(@meta_tags, event)}#{value} #{timestamp}"
    end
  end
end
hash2line(hash, event) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 148
def hash2line(hash, event)
  if (hash.is_a?(Hash) && !hash.empty?)
    expand_hash(hash, event).flat_map { |k, v|
      "#{k}=#{v} "
    }.join()
  else
    ""
  end
end
is_number?(me) click to toggle source
# File lib/logstash/outputs/sumologic/payload_builder.rb, line 115
def is_number?(me)
  me.to_f.to_s == me.to_s || me.to_i.to_s == me.to_s
end