Class: Fluent::BufferedSlackOutput

Inherits:
TimeSlicedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_buffered_slack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBufferedSlackOutput

Returns a new instance of BufferedSlackOutput.



43
44
45
46
47
48
# File 'lib/fluent/plugin/out_buffered_slack.rb', line 43

def initialize
  super
  require 'active_support/time'
  require 'uri'
  require 'net/http'
end

Instance Attribute Details

#slackObject (readonly)

Returns the value of attribute slack.



13
14
15
# File 'lib/fluent/plugin/out_buffered_slack.rb', line 13

def slack
  @slack
end

Instance Method Details

#configure(conf) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/fluent/plugin/out_buffered_slack.rb', line 50

def configure(conf)
  super
  @channel  = URI.unescape(conf['channel'])
  @username = conf['username'] || 'fluentd'
  @color    = conf['color'] || 'good'
  @icon_emoji = conf['icon_emoji'] || ':question:'
  @timezone   = conf['timezone'] || 'UTC'
  @team       = conf['team']
  @api_key    = conf['api_key']
end

#format(tag, time, record) ⇒ Object



15
16
17
# File 'lib/fluent/plugin/out_buffered_slack.rb', line 15

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#write(chunk) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fluent/plugin/out_buffered_slack.rb', line 19

def write(chunk)
  messages = {}
  chunk.msgpack_each do |tag, time, record|
    messages[tag] = '' if messages[tag].nil?
    messages[tag] << "[#{Time.at(time).in_time_zone(@timezone).strftime("%H:%M:%S")}] #{record['message']}\n"
  end
  begin
    payload = {
      channel:      @channel,
        username:   @username,
        icon_emoji: @icon_emoji,
        attachments: [{
          fallback: messages.keys.join(','),
          color:    @color,
          fields:   messages.map{|k,v| {title: k, value: v} }
        }]}
    post_request(
      payload: payload.to_json
    )
  rescue => e
    $log.error("Slack Error: #{e.backtrace[0]} / #{e.message}")
  end
end