fluent-plugin-slack 0.2.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b835ec9f32cc25b75bd4e0a18d7aa52097ff61e
4
+ data.tar.gz: 21a5be237380dc845f1ead2dad7e547500d88c19
5
+ SHA512:
6
+ metadata.gz: 7e36906812d93f8887a0994209c7efecf432b71e85e75cbc3b0fa9e7660c2dea8f04fc0eba411a35a11671d28c9d55ed646e153e408e71f5b9ffe79934b42578
7
+ data.tar.gz: 7090cf28cbf42af855170144a1c9b446837704f3f84d5c6340a9e9b1d3d86b9dce7f50a76c90d3773da22dbfa9cc68491404b86f94f35c5aebb2afc3738e79c2
data/README.rdoc CHANGED
@@ -8,11 +8,32 @@
8
8
 
9
9
  <match slack>
10
10
  type buffered_slack
11
- api_key XXX
11
+ webhook_url https://hooks.slack.com/services/XXX/XXX/XXX
12
+ team sowa
12
13
  channel %23general # You should use %23 in return for #
13
14
  username sowasowa
14
15
  color good
15
16
  icon_emoji :ghost:
17
+ buffer_path /path/to/tmp
18
+ flush_interval 60s
19
+ </match>
20
+
21
+ fluent_logger.post('buffered_slack', {
22
+ :message => 'Hello<br>World!'
23
+ })
24
+
25
+ = Usage(rtm)
26
+
27
+ <match slack>
28
+ type buffered_slack
29
+ rtm true
30
+ token xoxb-XXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX
31
+ channel %23general # You should use %23 in return for #
32
+ username sowasowa
33
+ color good
34
+ icon_emoji :ghost:
35
+ buffer_path /path/to/tmp
36
+ flush_interval 60s
16
37
  </match>
17
38
 
18
39
  fluent_logger.post('buffered_slack', {
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.4.0
@@ -2,13 +2,16 @@ module Fluent
2
2
  class BufferedSlackOutputError < StandardError; end
3
3
  class BufferedSlackOutput < Fluent::TimeSlicedOutput
4
4
  Fluent::Plugin.register_output('buffered_slack', self)
5
- config_param :api_key, :string
6
- config_param :team, :string
5
+ config_param :api_key, :string, default: nil
6
+ config_param :token, :string, default: nil
7
+ config_param :team, :string, default: nil
7
8
  config_param :channel, :string
8
9
  config_param :username, :string
9
10
  config_param :color, :string
10
11
  config_param :icon_emoji, :string
11
12
  config_param :timezone, :string, default: nil
13
+ config_param :rtm, :bool , default: false
14
+ config_param :webhook_url,:string, default: nil
12
15
 
13
16
  attr_reader :slack
14
17
 
@@ -23,18 +26,35 @@ module Fluent
23
26
  messages[tag] << "[#{Time.at(time).in_time_zone(@timezone).strftime("%H:%M:%S")}] #{record['message']}\n"
24
27
  end
25
28
  begin
26
- payload = {
27
- channel: @channel,
28
- username: @username,
29
- icon_emoji: @icon_emoji,
30
- attachments: [{
31
- fallback: messages.keys.join(','),
32
- color: @color,
33
- fields: messages.map{|k,v| {title: k, value: v} }
34
- }]}
35
- post_request(
36
- payload: payload.to_json
37
- )
29
+
30
+ # https://api.slack.com/rtm
31
+ if @rtm
32
+ params = {
33
+ :token => @token,
34
+ :channel => @channel,
35
+ :text => messages.values,
36
+ :username => @username,
37
+ :icon_emoji => @icon_emoji,
38
+ :attachments => [{
39
+ :color => @color,
40
+ :text => messages.values
41
+ }].to_json
42
+ }
43
+ get_request(params)
44
+ else
45
+ payload = {
46
+ channel: @channel,
47
+ username: @username,
48
+ icon_emoji: @icon_emoji,
49
+ attachments: [{
50
+ fallback: messages.keys.join(','),
51
+ color: @color,
52
+ fields: messages.map{|k,v| {title: k, value: v} }
53
+ }]}
54
+ post_request(
55
+ payload: payload.to_json
56
+ )
57
+ end
38
58
  rescue => e
39
59
  $log.error("Slack Error: #{e.backtrace[0]} / #{e.message}")
40
60
  end
@@ -49,18 +69,41 @@ module Fluent
49
69
 
50
70
  def configure(conf)
51
71
  super
52
- @channel = URI.unescape(conf['channel'])
53
- @username = conf['username'] || 'fluentd'
54
- @color = conf['color'] || 'good'
72
+
73
+ @channel = URI.unescape(conf['channel'])
74
+ @username = conf['username'] || 'fluentd'
75
+ @color = conf['color'] || 'good'
55
76
  @icon_emoji = conf['icon_emoji'] || ':question:'
56
- @timezone = conf['timezone'] || 'UTC'
57
- @team = conf['team']
58
- @api_key = conf['api_key']
77
+
78
+ if @rtm
79
+ @token = conf['token']
80
+ else
81
+ @timezone = conf['timezone'] || 'UTC'
82
+ @team = conf['team']
83
+ @api_key = conf['api_key']
84
+ @webhook_url= conf['webhook_url']
85
+ end
59
86
  end
60
87
 
61
88
  private
89
+ def response_check(res)
90
+ if res.code != "200"
91
+ raise BufferedSlackOutputError, "Slack.com - #{res.code} - #{res.body}"
92
+ end
93
+ end
94
+
95
+ def get_request(params)
96
+ query = URI.encode_www_form(params)
97
+ uri = URI.parse("https://slack.com/api/chat.postMessage?#{query}")
98
+ http = Net::HTTP.new(uri.host, uri.port)
99
+ http.use_ssl = true
100
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
101
+ res = http.get(uri.request_uri)
102
+ response_check(res)
103
+ end
104
+
62
105
  def endpoint
63
- URI.parse "https://#{@team}.slack.com/services/hooks/incoming-webhook?token=#{@api_key}"
106
+ URI.parse @webhook_url || "https://#{@team}.slack.com/services/hooks/incoming-webhook?token=#{@api_key}"
64
107
  end
65
108
 
66
109
  def post_request(data)
@@ -69,9 +112,7 @@ module Fluent
69
112
  http = Net::HTTP.new endpoint.host, endpoint.port
70
113
  http.use_ssl = (endpoint.scheme == "https")
71
114
  res = http.request(req)
72
- if res.code != "200"
73
- raise BufferedSlackOutputError, "Slack.com - #{res.code} - #{res.body}"
74
- end
115
+ response_check(res)
75
116
  end
76
117
  end
77
118
  end
@@ -8,7 +8,7 @@ class BufferedSlackOutputTest < Test::Unit::TestCase
8
8
  Fluent::Test.setup
9
9
  end
10
10
 
11
- CONFIG = %[
11
+ CONFIG1 = %[
12
12
  type buffered_slack
13
13
  api_key testtoken
14
14
  team sowasowa
@@ -22,7 +22,7 @@ class BufferedSlackOutputTest < Test::Unit::TestCase
22
22
  utc
23
23
  ]
24
24
 
25
- def create_driver(conf = CONFIG)
25
+ def create_driver(conf = CONFIG1)
26
26
  Fluent::Test::BufferedOutputTestDriver.new(Fluent::BufferedSlackOutput).configure(conf)
27
27
  end
28
28
 
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+ require 'time'
3
+
4
+ class BufferedSlackOutputTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ super
8
+ Fluent::Test.setup
9
+ end
10
+
11
+ CONFIG2 = %[
12
+ type buffered_slack
13
+ rtm true
14
+ token testtoken
15
+ channel C01234567
16
+ username testuser
17
+ color good
18
+ icon_emoji :ghost:
19
+ buffer_path ./test/tmp
20
+ ]
21
+
22
+ def create_driver(conf = CONFIG2)
23
+ Fluent::Test::BufferedOutputTestDriver.new(Fluent::BufferedSlackOutput).configure(conf)
24
+ end
25
+
26
+ def test_format_rtm
27
+ d = create_driver
28
+ time = Time.parse("2014-01-01 22:00:00 UTC").to_i
29
+ d.tag = 'test'
30
+ stub(d.instance.slack).ping(
31
+ nil,
32
+ channel: 'C01234567',
33
+ username: 'testuser',
34
+ icon_emoji: ':ghost:',
35
+ attachments: [{
36
+ color: 'good',
37
+ text: "[#{Time.at(time).in_time_zone('Tokyo')}] sowawa\n"
38
+ }]
39
+ )
40
+ d.emit({message: 'sowawa'}, time)
41
+ d.expect_format %[#{['test', time, {message: 'sowawa'}].to_msgpack}]
42
+ d.run
43
+ end
44
+
45
+ def test_write_rtm
46
+ d = create_driver
47
+ time = Time.parse("2014-01-01 22:00:00 UTC").to_i
48
+ d.tag = 'test'
49
+ stub(d.instance.slack).ping(
50
+ nil,
51
+ channel: 'C01234567',
52
+ username: 'testuser',
53
+ icon_emoji: ':ghost:',
54
+ attachments: [{
55
+ color: 'good',
56
+ text: "[#{Time.at(time).in_time_zone('Tokyo')}] sowawa\n"
57
+ }]
58
+ )
59
+ d.emit({message: 'sowawa1'}, time)
60
+ d.emit({message: 'sowawa2'}, time)
61
+ d.run
62
+ end
63
+ end
metadata CHANGED
@@ -1,126 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
5
- prerelease:
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Keisuke SOGAWA
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-07-10 00:00:00.000000000 Z
11
+ date: 2015-02-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: fluentd
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 0.10.8
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.10.8
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activesupport
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: 3.2.0
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: 3.2.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: tzinfo
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: 0.3.38
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: 0.3.38
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: 10.1.1
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: 10.1.1
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: rr
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
75
  version: 1.0.0
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
82
  version: 1.0.0
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: pry
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: minitest
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ~>
101
+ - - "~>"
116
102
  - !ruby/object:Gem::Version
117
103
  version: '4.0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ~>
108
+ - - "~>"
124
109
  - !ruby/object:Gem::Version
125
110
  version: '4.0'
126
111
  description: fluent Slack plugin
@@ -129,7 +114,7 @@ executables: []
129
114
  extensions: []
130
115
  extra_rdoc_files: []
131
116
  files:
132
- - .gitignore
117
+ - ".gitignore"
133
118
  - AUTHORS
134
119
  - Gemfile
135
120
  - README.rdoc
@@ -138,37 +123,32 @@ files:
138
123
  - fluent-plugin-slack.gemspec
139
124
  - lib/fluent/plugin/out_buffered_slack.rb
140
125
  - test/plugin/test_out_buffered_slack.rb
126
+ - test/plugin/test_out_buffered_slack_rtm.rb
141
127
  - test/test_helper.rb
142
128
  homepage: https://github.com/sowawa/fluent-plugin-slack
143
129
  licenses: []
130
+ metadata: {}
144
131
  post_install_message:
145
132
  rdoc_options: []
146
133
  require_paths:
147
134
  - lib
148
135
  required_ruby_version: !ruby/object:Gem::Requirement
149
- none: false
150
136
  requirements:
151
- - - ! '>='
137
+ - - ">="
152
138
  - !ruby/object:Gem::Version
153
139
  version: '0'
154
- segments:
155
- - 0
156
- hash: -2798180410085788285
157
140
  required_rubygems_version: !ruby/object:Gem::Requirement
158
- none: false
159
141
  requirements:
160
- - - ! '>='
142
+ - - ">="
161
143
  - !ruby/object:Gem::Version
162
144
  version: '0'
163
- segments:
164
- - 0
165
- hash: -2798180410085788285
166
145
  requirements: []
167
146
  rubyforge_project:
168
- rubygems_version: 1.8.23
147
+ rubygems_version: 2.2.2
169
148
  signing_key:
170
- specification_version: 3
149
+ specification_version: 4
171
150
  summary: fluent Slack plugin
172
151
  test_files:
173
152
  - test/plugin/test_out_buffered_slack.rb
153
+ - test/plugin/test_out_buffered_slack_rtm.rb
174
154
  - test/test_helper.rb