Sha256: 576698a56718d3bf7a0fa6a3c2ef0cc04faf52ba7d792dcdf716d6ae5b4e4a34

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Fluent
  class BufferedSlackOutput < Fluent::TimeSlicedOutput
    Fluent::Plugin.register_output('buffered_slack', self)
    config_param :api_key,    :string
    config_param :team,       :string
    config_param :channel,    :string
    config_param :username,   :string
    config_param :color,      :string
    config_param :icon_emoji, :string

    attr_reader :slack

    def format(tag, time, record)
      [tag, time, record].to_json + "\n"
    end

    def write(chunk)
      messages = {}
      chunk.msgpack_each do |(tag, time, record)|
        messages[tag] = '' unless messages[tag]
        messages[tag] << "[#{Time.at(time)}] #{record['message']}\n"
      end
      messages.each do |tag, value|
        filed = {
          title: tag,
          value: value
        }
        @slack.say(
          nil,
          channel:     @channel,
          username:    @username,
          icon_emoji:  @icon_emoji,
          attachments: {
            fallback: tag,
            color:    @color,
            fields:   [ field ]
          })
      end
    rescue => e
      $log.error("Slack Error: #{e} / #{e.message}")
    end

    def initialize
      super
      require 'slackr'
    end

    def configure(conf)
      super
      @slack    = Slackr::Webhook.new(conf['team'], conf['api_key'])
      @channel  = conf['channel']
      @username = conf['username'] || 'fluentd'
      @color    = conf['color'] || 'good'
      @icon_emoji = conf['icon_emoji'] || ':question:'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-slack-0.0.1 lib/fluent/plugin/out_buffered_slack.rb