Class: Fluent::SlackOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
SetTagKeyMixin, SetTimeKeyMixin
Defined in:
lib/fluent/plugin/out_slack.rb,
lib/fluent/plugin/out_buffered_slack.rb

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSlackOutput

Returns a new instance of SlackOutput.



43
44
45
46
# File 'lib/fluent/plugin/out_slack.rb', line 43

def initialize
  super
  require 'uri'
end

Instance Attribute Details

#localtimeObject (readonly)

for test



41
42
43
# File 'lib/fluent/plugin/out_slack.rb', line 41

def localtime
  @localtime
end

#mrkdwn_inObject (readonly)

for test



41
42
43
# File 'lib/fluent/plugin/out_slack.rb', line 41

def mrkdwn_in
  @mrkdwn_in
end

#post_message_optsObject (readonly)

for test



41
42
43
# File 'lib/fluent/plugin/out_slack.rb', line 41

def post_message_opts
  @post_message_opts
end

#slackObject (readonly)

for test



41
42
43
# File 'lib/fluent/plugin/out_slack.rb', line 41

def slack
  @slack
end

#time_formatObject (readonly)

for test



41
42
43
# File 'lib/fluent/plugin/out_slack.rb', line 41

def time_format
  @time_format
end

#timefObject (readonly)

for test



41
42
43
# File 'lib/fluent/plugin/out_slack.rb', line 41

def timef
  @timef
end

Instance Method Details

#configure(conf) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/out_slack.rb', line 48

def configure(conf)
  conf['time_format'] ||= '%H:%M:%S' # old version compatiblity
  conf['localtime'] ||= true unless conf['utc']
 
  super

  @channel = URI.unescape(@channel) # old version compatibility
  @channel = '#' + @channel unless @channel.start_with?('#')

  if @webhook_url
    if @webhook_url.empty?
      raise Fluent::ConfigError.new("`webhook_url` is an empty string")
    end
    @slack = Fluent::SlackClient::IncomingWebhook.new(@webhook_url)
  elsif @slackbot_url
    if @slackbot_url.empty?
      raise Fluent::ConfigError.new("`slackbot_url` is an empty string")
    end
    if @username or @color or @icon_emoji or @icon_url
      log.warn "out_slack: `username`, `color`, `icon_emoji`, `icon_url` parameters are not available for Slackbot Remote Control"
    end
    @slack = Fluent::SlackClient::Slackbot.new(@slackbot_url)
  elsif @token
    if @token.empty?
      raise Fluent::ConfigError.new("`token` is an empty string")
    end
    @slack = Fluent::SlackClient::WebApi.new
  else
    raise Fluent::ConfigError.new("One of `webhook_url` or `slackbot_url`, or `token` is required")
  end
  @slack.log = log
  @slack.debug_dev = log.out if log.level <= Fluent::Log::LEVEL_TRACE

  if @https_proxy
    @slack.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 @title and @title_keys
    begin
      @title % (['1'] * @title_keys.length)
    rescue ArgumentError
      raise Fluent::ConfigError, "string specifier '%s' for `title` and `title_keys` specification mismatch"
    end
  end
  if @channel_keys
    begin
      @channel % (['1'] * @channel_keys.length)
    rescue ArgumentError
      raise Fluent::ConfigError, "string specifier '%s' for `channel` and `channel_keys` specification mismatch"
    end
  end

  if @icon_emoji and @icon_url
    raise Fluent::ConfigError, "either of `icon_emoji` or `icon_url` can be specified"
  end

  if @mrkdwn
    # Enable markdown for attachments. See https://api.slack.com/docs/formatting
    @mrkdwn_in = %w[text fields]
  end

  if @parse and !%w[none full].include?(@parse)
    raise Fluent::ConfigError, "`parse` must be either of `none` or `full`"
  end

  @post_message_opts = {}
  if @auto_channels_create
    raise Fluent::ConfigError, "`token` parameter is required to use `auto_channels_create`" unless @token
    @post_message_opts = {auto_channels_create: true}
  end
end

#format(tag, time, record) ⇒ Object



127
128
129
# File 'lib/fluent/plugin/out_slack.rb', line 127

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

#write(chunk) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/out_slack.rb', line 131

def write(chunk)
  begin
    payloads = build_payloads(chunk)
    payloads.each {|payload| @slack.post_message(payload, @post_message_opts) }
  rescue Timeout::Error => e
    log.warn "out_slack:", :error => e.to_s, :error_class => e.class.to_s
    raise e # let Fluentd retry
  rescue => e
    log.error "out_slack:", :error => e.to_s, :error_class => e.class.to_s
    log.warn_backtrace e.backtrace
    # discard. @todo: add more retriable errors
  end
end