lib/fluent/plugin/out_slack.rb in fluent-plugin-slack-0.5.0 vs lib/fluent/plugin/out_slack.rb in fluent-plugin-slack-0.5.1

- old
+ new

@@ -9,26 +9,27 @@ include SetTagKeyMixin config_set_default :include_time_key, true config_set_default :include_tag_key, true - config_param :webhook_url, :string, default: nil # incoming webhook - config_param :token, :string, default: nil # api token - config_param :username, :string, default: 'fluentd' - config_param :color, :string, default: 'good' - config_param :icon_emoji, :string, default: ':question:' + config_param :webhook_url, :string, default: nil # incoming webhook + config_param :token, :string, default: nil # api token + config_param :username, :string, default: 'fluentd' + config_param :color, :string, default: 'good' + config_param :icon_emoji, :string, default: ':question:' + config_param :auto_channels_create, :bool, default: false - config_param :channel, :string - config_param :channel_keys, default: nil do |val| + config_param :channel, :string + config_param :channel_keys, default: nil do |val| val.split(',') end - config_param :title, :string, default: nil - config_param :title_keys, default: nil do |val| + config_param :title, :string, default: nil + config_param :title_keys, default: nil do |val| val.split(',') end - config_param :message, :string, default: nil - config_param :message_keys, default: nil do |val| + config_param :message, :string, default: nil + config_param :message_keys, default: nil do |val| val.split(',') end # for test attr_reader :slack, :time_format, :localtime, :timef @@ -46,23 +47,28 @@ @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 # following default values are for old version compatibility @title ||= '%s' @title_keys ||= %w[tag] @message ||= '[%s] %s' @message_keys ||= %w[time message] @slack = Fluent::SlackClient::IncomingWebhook.new(@webhook_url) - else - unless @token - raise Fluent::ConfigError.new("`token` is required to call slack api") + elsif @token + if @token.empty? + raise Fluent::ConfigError.new("`token` is an empty string") end @message ||= '%s' @message_keys ||= %w[message] @slack = Fluent::SlackClient::WebApi.new + else + raise Fluent::ConfigError.new("Either of `webhook_url` or `token` is required") end @slack.log = log @slack.debug_dev = log.out if log.level <= Fluent::Log::LEVEL_TRACE begin @@ -82,19 +88,21 @@ @channel % (['1'] * @channel_keys.length) rescue ArgumentError raise Fluent::ConfigError, "string specifier '%s' for `channel` and `channel_keys` specification mismatch" end end + + @post_message_opts = @auto_channels_create ? {auto_channels_create: true} : {} end def format(tag, time, record) [tag, time, record].to_msgpack end def write(chunk) begin payloads = build_payloads(chunk) - payloads.each {|payload| @slack.post_message(payload) } + payloads.each {|payload| @slack.post_message(payload, @post_message_opts) } rescue Net::OpenTimeout, Net::ReadTimeout => 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