class Fluent::Plugin::TypetalkOutput

Attributes

typetalk[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_typetalk.rb, line 28
def initialize
  super
  require 'socket'
  require 'typetalk'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_typetalk.rb, line 34
def configure(conf)
  super
  Typetalk.configure do |c|
    c.client_id = conf['client_id']
    c.client_secret = conf['client_secret']
    c.scope = 'topic.post'
    c.user_agent = "fluent-plugin-typetalk Ruby/#{RUBY_VERSION}"
  end
  @typetalk = Typetalk::Api.new
  @hostname = Socket.gethostname

  @out_keys = @out_keys.split(',')

  begin
    @message % (['1'] * @out_keys.length)
  rescue ArgumentError
    raise Fluent::ConfigError, "string specifier '%s' and out_keys specification mismatch"
  end

  if @time_format
    f = @time_format
    tf = Fluent::TimeFormatter.new(f, true) # IRC notification is formmatted as localtime only...
    @time_format_proc = tf.method(:format)
    @time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
  else
    @time_format_proc = Proc.new {|time| time.to_s }
    @time_parse_proc = Proc.new {|str| str.to_i }
  end

  @need_throttle = @limit > 0 && @interval > 0
  @slot = []

end
evaluate_message(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_typetalk.rb, line 135
def evaluate_message(tag, time, record)

  values = out_keys.map do |key|
    case key
    when @time_key
      @time_format_proc.call(time)
    when @tag_key
      tag
    when "$hostname"
      @hostname
    else
      record[key].to_s
    end
  end

  truncate (@message % values).gsub(/\\n/, "\n")
end
process(tag, es) click to toggle source
# File lib/fluent/plugin/out_typetalk.rb, line 76
def process(tag, es)
  es.each do |time, record|
    if @need_throttle && throttle(time)
      log.error("out_typetalk:", :error => "number of posting message within #{@interval}(sec) reaches to the limit #{@limit}")
      next
    end

    begin
      send_message(tag, time, record)
    rescue => e
      log.error("out_typetalk:", :error_class => e.class, :error => e.message)
    end
  end
end
send_message(tag, time, record) click to toggle source
# File lib/fluent/plugin/out_typetalk.rb, line 105
def send_message(tag, time, record)
  message = evaluate_message(tag, time, record)
  begin
    @typetalk.post_message(@topic_id, message, {show_link_meta: false})
  rescue Typetalk::Unauthorized
    raise TypetalkError, "invalid credentials used. check client_id and client_secret in your configuration."
  rescue => e
    msg = ''
    res = JSON.parse(e.message) rescue {}
    unless res['body'].nil?
      body = JSON.parse(res['body']) rescue {}

      # for auth error, the error stored in "error" property
      # https://developer.nulab-inc.com/docs/typetalk/auth#client
      if body.has_key?('error')
        msg = body['error']
      elsif body.has_key?('errors')
        msg = body['errors'].map{|f|
          f['field'] + ' : ' + f['message']
        }.join(',')
      elsif !res['headers'].nil?
        headers = JSON.parse(res['headers']) rescue {}
        msg = headers['www-authenticate']
      end

    end
    raise TypetalkError, "failed to post, msg: #{msg}, code: #{res['status']}"
  end
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_typetalk.rb, line 72
def shutdown
  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_typetalk.rb, line 68
def start
  super
end
throttle(time) click to toggle source
# File lib/fluent/plugin/out_typetalk.rb, line 91
def throttle(time)
  expired = time.to_f - @interval
  while @slot.first && (@slot.first <= expired)
    @slot.shift
  end

  exceed = @slot.length >= @limit
  unless exceed
    @slot.push(time.to_f)
  end

  exceed
end
truncate(str, limit=4000) click to toggle source
# File lib/fluent/plugin/out_typetalk.rb, line 153
def truncate(str, limit=4000)
  @truncate_message && str.size >= limit ? str[0,limit-5] + ' ...' : str
end