Class: Fluent::SlackOutput

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

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSlackOutput

Returns a new instance of SlackOutput.



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

def initialize
  super
  require 'uri'
end

Instance Attribute Details

#localtimeObject (readonly)

for test



123
124
125
# File 'lib/fluent/plugin/out_slack.rb', line 123

def localtime
  @localtime
end

#mrkdwn_inObject (readonly)

for test



123
124
125
# File 'lib/fluent/plugin/out_slack.rb', line 123

def mrkdwn_in
  @mrkdwn_in
end

#post_message_optsObject (readonly)

for test



123
124
125
# File 'lib/fluent/plugin/out_slack.rb', line 123

def post_message_opts
  @post_message_opts
end

#slackObject (readonly)

for test



123
124
125
# File 'lib/fluent/plugin/out_slack.rb', line 123

def slack
  @slack
end

#time_formatObject (readonly)

for test



123
124
125
# File 'lib/fluent/plugin/out_slack.rb', line 123

def time_format
  @time_format
end

#timefObject (readonly)

for test



123
124
125
# File 'lib/fluent/plugin/out_slack.rb', line 123

def timef
  @timef
end

Instance Method Details

#configure(conf) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fluent/plugin/out_slack.rb', line 130

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
    unless @as_user.nil?
      log.warn "out_slack: `as_user` parameter are not available for Incoming Webhook"
    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
    unless @as_user.nil?
      log.warn "out_slack: `as_user` parameter 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 @as_user and (@icon_emoji or @icon_url or @username)
    raise Fluent::ConfigError, "`username`, `icon_emoji` and `icon_url` cannot be specified when `as_user` is set to true"
  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

#desc(description) ⇒ Object



11
12
# File 'lib/fluent/plugin/out_slack.rb', line 11

def desc(description)
end

#format(tag, time, record) ⇒ Object



219
220
221
# File 'lib/fluent/plugin/out_slack.rb', line 219

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

#write(chunk) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/fluent/plugin/out_slack.rb', line 223

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