Class: Fluent::TypetalkOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypetalkOutput

Returns a new instance of TypetalkOutput.



24
25
26
27
28
# File 'lib/fluent/plugin/out_typetalk.rb', line 24

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

Instance Attribute Details

#typetalkObject (readonly)

Returns the value of attribute typetalk.



16
17
18
# File 'lib/fluent/plugin/out_typetalk.rb', line 16

def typetalk
  @typetalk
end

Instance Method Details

#configure(conf) ⇒ Object



30
31
32
33
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
# File 'lib/fluent/plugin/out_typetalk.rb', line 30

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

end

#evaluate_message(tag, time, record) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fluent/plugin/out_typetalk.rb', line 100

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

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

#format(tag, time, record) ⇒ Object



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

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

#send_message(tag, time, record) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fluent/plugin/out_typetalk.rb', line 83

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 {}
      msg = body['error']
    end
    raise TypetalkError, "failed to post, msg: #{msg}, code: #{res['status']}"
  end
end

#shutdownObject



65
66
67
# File 'lib/fluent/plugin/out_typetalk.rb', line 65

def shutdown
  super
end

#startObject



61
62
63
# File 'lib/fluent/plugin/out_typetalk.rb', line 61

def start
  super
end

#write(chunk) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/out_typetalk.rb', line 73

def write(chunk)
  chunk.msgpack_each do |(tag,time,record)|
    begin
      send_message(tag, time, record)
    rescue => e
      log.error("out_typetalk:", :error_class => e.class, :error => e.message)
    end
  end
end