Class: Fluent::TypetalkOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_typetalk.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypetalkOutput



28
29
30
31
32
# File 'lib/fluent/plugin/out_typetalk.rb', line 28

def initialize
  super
  require 'socket'
  require 'typetalk'
end

Instance Attribute Details

#typetalkObject (readonly)

Returns the value of attribute typetalk.



20
21
22
# File 'lib/fluent/plugin/out_typetalk.rb', line 20

def typetalk
  @typetalk
end

Instance Method Details

#configure(conf) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fluent/plugin/out_typetalk.rb', line 34

def configure(conf)
  super
  Typetalk.configure do |c|
    c.client_id = conf['client_id']
    c.client_secret = conf['client_secret']
    c.scope = 'topic.post'
    c.user_agent = "fluent-plugin-typetalk Ruby/#{RUBY_VERSION}"
  end
  @typetalk = Typetalk::Api.new
  @hostname = Socket.gethostname

  @out_keys = @out_keys.split(',')

  begin
    @message % (['1'] * @out_keys.length)
  rescue ArgumentError
    raise Fluent::ConfigError, "string specifier '%s' and out_keys specification mismatch"
  end

  if @time_format
    f = @time_format
    tf = Fluent::TimeFormatter.new(f, true) # IRC notification is formmatted as localtime only...
    @time_format_proc = tf.method(:format)
    @time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
  else
    @time_format_proc = Proc.new {|time| time.to_s }
    @time_parse_proc = Proc.new {|str| str.to_i }
  end

  @need_throttle = @limit > 0 && @interval > 0
  @slot = []

end

#emit(tag, es, chain) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fluent/plugin/out_typetalk.rb', line 76

def emit(tag, es, chain)
  es.each do |time, record|
    if @need_throttle && throttle(time)
      log.error("out_typetalk:", :error => "number of posting message within #{@interval}(sec) reaches to the limit #{@limit}")
      next
    end

    begin
      send_message(tag, time, record)
    rescue => e
      log.error("out_typetalk:", :error_class => e.class, :error => e.message)
    end
  end

  chain.next
end

#evaluate_message(tag, time, record) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fluent/plugin/out_typetalk.rb', line 137

def evaluate_message(tag, time, record)

  values = out_keys.map do |key|
    case key
    when @time_key
      @time_format_proc.call(time)
    when @tag_key
      tag
    when "$hostname"
      @hostname
    else
      record[key].to_s
    end
  end

  truncate (@message % values).gsub(/\\n/, "\n")
end

#send_message(tag, time, record) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fluent/plugin/out_typetalk.rb', line 107

def send_message(tag, time, record)
  message = evaluate_message(tag, time, record)
  begin
    @typetalk.post_message(@topic_id, message)
  rescue Typetalk::Unauthorized
    raise TypetalkError, "invalid credentials used. check client_id and client_secret in your configuration."
  rescue => e
    msg = ''
    res = JSON.parse(e.message) rescue {}
    unless res['body'].nil?
      body = JSON.parse(res['body']) rescue {}

      # for auth error, the error stored in "error" property
      # https://developer.nulab-inc.com/docs/typetalk/auth#client
      if body.has_key?('error')
        msg = body['error']
      elsif body.has_key?('errors')
        msg = body['errors'].map{|f|
          f['field'] + ' : ' + f['message']
        }.join(',')
      elsif !res['headers'].nil?
        headers = JSON.parse(res['headers']) rescue {}
        msg = headers['www-authenticate']
      end

    end
    raise TypetalkError, "failed to post, msg: #{msg}, code: #{res['status']}"
  end
end

#shutdownObject



72
73
74
# File 'lib/fluent/plugin/out_typetalk.rb', line 72

def shutdown
  super
end

#startObject



68
69
70
# File 'lib/fluent/plugin/out_typetalk.rb', line 68

def start
  super
end

#throttle(time) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fluent/plugin/out_typetalk.rb', line 93

def throttle(time)
  expired = time.to_f - @interval
  while @slot.first && (@slot.first <= expired)
    @slot.shift
  end

  exceed = @slot.length >= @limit
  unless exceed
    @slot.push(time.to_f)
  end

  exceed
end

#truncate(str, limit = 4000) ⇒ Object



155
156
157
# File 'lib/fluent/plugin/out_typetalk.rb', line 155

def truncate(str, limit=4000)
  @truncate_message && str.size >= limit ? str[0,limit-5] + ' ...' : str
end