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.



38
39
40
41
# File 'lib/fluent/plugin/out_slack.rb', line 38

def initialize
  super
  require 'uri'
end

Instance Attribute Details

#localtimeObject (readonly)

for test



36
37
38
# File 'lib/fluent/plugin/out_slack.rb', line 36

def localtime
  @localtime
end

#slackObject (readonly)

for test



36
37
38
# File 'lib/fluent/plugin/out_slack.rb', line 36

def slack
  @slack
end

#time_formatObject (readonly)

for test



36
37
38
# File 'lib/fluent/plugin/out_slack.rb', line 36

def time_format
  @time_format
end

#timefObject (readonly)

for test



36
37
38
# File 'lib/fluent/plugin/out_slack.rb', line 36

def timef
  @timef
end

Instance Method Details

#configure(conf) ⇒ Object



43
44
45
46
47
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
# File 'lib/fluent/plugin/out_slack.rb', line 43

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
    # 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)
  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
    @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
  @icon_emoji ||= ':question:' unless @icon_url

  @post_message_opts = @auto_channels_create ? {auto_channels_create: true} : {}
end

#format(tag, time, record) ⇒ Object



103
104
105
# File 'lib/fluent/plugin/out_slack.rb', line 103

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

#write(chunk) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fluent/plugin/out_slack.rb', line 107

def write(chunk)
  begin
    payloads = build_payloads(chunk)
    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
    log.warn_backtrace e.backtrace
    # discard. @todo: add more retriable errors
  end
end