Class: Fluent::TypetalkOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::TypetalkOutput
- Defined in:
- lib/fluent/plugin/out_typetalk.rb
Instance Attribute Summary collapse
-
#typetalk ⇒ Object
readonly
Returns the value of attribute typetalk.
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #emit(tag, es, chain) ⇒ Object
- #evaluate_message(tag, time, record) ⇒ Object
-
#initialize ⇒ TypetalkOutput
constructor
A new instance of TypetalkOutput.
- #send_message(tag, time, record) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #throttle(time) ⇒ Object
- #truncate(str, limit = 4000) ⇒ Object
Constructor Details
#initialize ⇒ TypetalkOutput
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
#typetalk ⇒ Object (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 % (['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 (tag, time, record) rescue => e log.error("out_typetalk:", :error_class => e.class, :error => e.) 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 (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 ( % 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 (tag, time, record) = (tag, time, record) begin @typetalk.(@topic_id, ) rescue Typetalk:: raise TypetalkError, "invalid credentials used. check client_id and client_secret in your configuration." rescue => e msg = '' res = JSON.parse(e.) 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 |
#shutdown ⇒ Object
72 73 74 |
# File 'lib/fluent/plugin/out_typetalk.rb', line 72 def shutdown super end |
#start ⇒ Object
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) && str.size >= limit ? str[0,limit-5] + ' ...' : str end |