class Fluent::GoogleChatOutput

Constants

Field

Attributes

google_chat[R]

for test

localtime[R]

for test

mrkdwn_in[R]

for test

post_message_opts[R]

for test

time_format[R]

for test

timef[R]

for test

Public Class Methods

desc(description) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 11
def desc(description)
end
new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_google_chat.rb, line 55
def initialize
  super
  require 'uri'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_google_chat.rb, line 60
def configure(conf)
  conf['time_format'] ||= '%H:%M:%S' # old version compatiblity
  conf['localtime'] ||= true unless conf['utc']

  super

  if @space
    @space = URI.unescape(@space) # old version compatibility
  else
    raise Fluent::ConfigError.new("`space` is required")
  end

  if @keyfile
    if @keyfile.empty?
      raise Fluent::ConfigError.new("`keyfile` is an empty string")
    end
    if @keyfile.nil?
      raise Fluent::ConfigError.new("`keyfile` parameter required for Google Chat")
    end
    @google_chat = Fluent::GoogleChatClient::WebApi.new(@keyfile)
  else
    raise Fluent::ConfigError.new("`keyfile` is required")
  end
  @google_chat.log = log
  @google_chat.debug_dev = log.out if log.level <= Fluent::Log::LEVEL_TRACE

  if @https_proxy
    @google_chat.https_proxy = @https_proxy
  end

  @message      ||= '%s'
  @message_keys ||= %w[message]
  begin
    @message % (['1'] * @message_keys.length)
  rescue ArgumentError
    raise Fluent::ConfigError, "string specifier '%s' for `message`  and `message_keys` specification mismatch"
  end
  if @space_keys
    begin
      @space % (['1'] * @space_keys.length)
    rescue ArgumentError
      raise Fluent::ConfigError, "string specifier '%s' for `space` and `space_keys` specification mismatch"
    end
  end
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 106
def format(tag, time, record)
  [tag, time, record].to_msgpack
end
write(chunk) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 110
def write(chunk)
  begin
    payloads = build_payloads(chunk)
    payloads.each {|payload| @google_chat.post_message(payload) }
  rescue Timeout::Error => e
    log.warn "out_google_chat:", :error => e.to_s, :error_class => e.class.to_s
    raise e # let Fluentd retry
  rescue => e
    log.error "out_google_chat:", :error => e.to_s, :error_class => e.class.to_s
    log.warn_backtrace e.backtrace
    # discard. @todo: add more retriable errors
  end
end

Private Instance Methods

build_message(record) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 147
def build_message(record)
  values = fetch_keys(record, @message_keys)
  @message % values
end
build_payloads(chunk) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 126
def build_payloads(chunk)
  build_plain_payloads(chunk)
end
build_plain_payloads(chunk) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 134
def build_plain_payloads(chunk)
  messages = {}
  chunk.msgpack_each do |tag, time, record|
    space = build_space(record)
    messages[space] ||= ''
    messages[space] << "#{build_message(record)}\n"
  end
  messages.map do |space, text|
    msg = {text: text}
    msg.merge!(space: space) if space
  end
end
build_space(record) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 152
def build_space(record)
  return nil if @space.nil?
  return @space unless @space_keys

  values = fetch_keys(record, @space_keys)
  @space % values
end
fetch_keys(record, keys) click to toggle source
# File lib/fluent/plugin/out_google_chat.rb, line 160
def fetch_keys(record, keys)
  Array(keys).map do |key|
    begin
      record.fetch(key).to_s
    rescue KeyError
      log.warn "out_google_chat: the specified key '#{key}' not found in record. [#{record}]"
      ''
    end
  end
end