fluent-plugin-typetalk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjIwYTlmMzRhMTFhNTk5NDAxODU1ODVkYjEzOTViYTY1MGJlNTRiNA==
4
+ MzFhNThjMWE0NjIzNmIyZDNhMDk4ODg1OTM0OGQ3ZTNjOGIwYjUzOQ==
5
5
  data.tar.gz: !binary |-
6
- NTRhNDIxMWNiMmEwMmViNTlkOGFmNjIyZDA4NTQ2ZDA5NThiNzczOA==
6
+ YmE2Y2ZkM2ViZmNmNzg0MzRiMGFhYmI5MzU2N2IwODk0MTA4ZDdlOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDQ4NmQxNTZiNjYyMDdiODRmN2ViZDY0ZTdlN2MwNmFiZmUyZmQxYmVkMTRm
10
- NjQ4NTQ4ZmIxY2I5NTAyNWZiZTc5ZDAzYTc2MDE0NjAwZGI3N2Y0MzcxYWYw
11
- NWVlM2Y3ZGU1MDk0OGJjZjZjNDE2NzhmNjU4MTk4MGM4YTY0M2E=
9
+ ODQzZDBmMjc1Zjc4Njg1ZWY5NTczMjE3NDI5ODEzZTE0MzIyZDRlZGJiNzU2
10
+ NzhlNTljZmYyOWU3MDBjYjI5MmQwMGZiMmNiY2EyNjk3NWEwZGJhYmViNjM2
11
+ YmJlOGUzYjM1OWU0MzM4NTRjYTQ4NDA5YjVhMTY4ZDQ1ZDFhYWQ=
12
12
  data.tar.gz: !binary |-
13
- YWIyYjc2YWJhMDM0ZjI0OTA1ZDdkMDAxYTc3MmNiOTI5NmVjZTY0OGU5ZWRk
14
- ZTcxZGI2NWE3YTNmOTQ1ZTYwZTc0MDY3OTkwMGViOTE0MTE1MzE1MjMzMDZm
15
- ZjU1MjIwMjQ3ODAwMWZkMmY3MjUwMzk3Y2Q5N2JhMTIyNGEzNzA=
13
+ NjBkMjgyODJjZDgzY2E3NjA1YTI1ODRiMGI4MDlmNTA0NzhhZmUxY2Q0MzIz
14
+ ZTZkNmQzODE2NWI5YmVmMzQ0YTcyZGFiMWQ2NWRiYzJhYjMzYjljNjQ4MTQ3
15
+ NzFjMTE4OTVkNDhlOTYxYjFhOWQ1YzNiZjFlOWRkZTdiZTFhZDA=
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # fluent-plugin-typetalk
1
+ # fluent-plugin-typetalk [![Build Status](https://travis-ci.org/tksmd/fluent-plugin-typetalk.png?branch=master)](https://travis-ci.org/tksmd/fluent-plugin-typetalk)
2
2
 
3
3
  ## Overview
4
4
 
5
- [Fluentd](http://fluentd.org) plugin to emit notifications to Typetalk.
5
+ [Fluentd](http://fluentd.org) plugin to emit notifications to [Typetalk](http://typetalk.in/).
6
6
 
7
7
  ## Installation
8
8
 
@@ -27,29 +27,30 @@ This plugin uses client credentials for authentication. See [the developer docum
27
27
  client_id YOUR_CLIENT_ID
28
28
  client_secret YOUR_CLIENT_SECRET
29
29
  topic_id YOUR_TOPIC_ID
30
+ message notice: %s [%s] %s errors found
31
+ out_keys tag,time,count
32
+ time_key time
33
+ time_format %Y/%m/%d %H:%M:%S
34
+ tag_key tag
30
35
  </match>
31
36
  ```
32
37
 
33
- The default output format is "<%= tag %> at <%= Time.at(time).localtime %>\n<%= record.to_json %>" and an example output is like this:
34
- ```
35
- test at 2014-05-13 01:21:30 0900
36
- {"message":"test1"}
37
- ```
38
+ The notified message will look like this : `notice: count.service [2014/05/13 03:34:02] 200 errors found`. The meanings of out_keys,time_key,time_format and tag_key are equivalent to those of [fluent-plugin-ikachan](https://github.com/tagomoris/fluent-plugin-ikachan).
38
39
 
39
- To change output format, you can set "template" parameter as follows:
40
+ This plugin allows you to use special value namely $hostname in out_keys. By using it, you can include hostname within the notified message.
40
41
  ```
41
42
  <match ...>
42
43
  type typetalk
43
44
  client_id YOUR_CLIENT_ID
44
45
  client_secret YOUR_CLIENT_SECRET
45
46
  topic_id YOUR_TOPIC_ID
46
- template "Check! <%= record.to_json %>"
47
+ message notice: %s [%s] %s errors found on %s
48
+ out_keys tag,time,count,$hostname
49
+ time_key time
50
+ time_format %Y/%m/%d %H:%M:%S
51
+ tag_key tag
47
52
  </match>
48
53
  ```
49
- Then you'll get the output like this:
50
- ```
51
- Check! {"message":"test1"}
52
- ```
53
54
 
54
55
  ## TODO
55
56
 
@@ -57,12 +58,22 @@ Pull requests are very welcome!!
57
58
 
58
59
  ## For developers
59
60
 
61
+ You have to run the command below when starting development.
62
+ ```
63
+ $ bundle install --path vendor/bundle
64
+ ```
65
+
60
66
  To run tests, do the following.
61
67
  ```
62
68
  $ VERBOSE=1 bundle exec rake test
63
69
  ```
64
70
 
71
+ When releasing, call rake release as follows.
72
+ ```
73
+ $ bundle exec rake release
74
+ ```
75
+
65
76
  ## Copyright
66
77
 
67
- Copyright : Copyright (c) 2014- Takashi Someda (@tksmd)
68
- License : Apache License, Version 2.0
78
+ * Copyright (c) 2014- Takashi Someda ([@tksmd](http://twitter.com/tksmd/))
79
+ * Apache License, Version 2.0
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-typetalk"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.0.2"
8
8
  spec.authors = ["tksmd"]
9
9
  spec.email = ["someda@isenshi.com"]
10
10
  spec.description = %q{fluent plugin to send message to typetalk}
@@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "rake"
22
22
  spec.add_development_dependency "rr", ">= 1.0.0"
23
23
  spec.add_runtime_dependency "fluentd"
24
+
25
+ spec.required_ruby_version = '>= 1.9.3'
24
26
  end
@@ -5,9 +5,14 @@ module Fluent
5
5
  config_param :client_id, :string
6
6
  config_param :client_secret, :string
7
7
  config_param :topic_id, :integer
8
- config_param :template, :string, :default => "<%= tag %> at <%= Time.at(time).localtime %>\n<%= record.to_json %>"
9
8
  config_param :flush_interval, :time, :default => 1
10
9
 
10
+ config_param :message, :string
11
+ config_param :out_keys, :string, :default => ""
12
+ config_param :time_key, :string, :default => 'time'
13
+ config_param :time_format, :string, :default => nil
14
+ config_param :tag_key, :string, :default => 'tag'
15
+
11
16
  attr_reader :typetalk
12
17
 
13
18
  # Define `log` method for v0.10.42 or earlier
@@ -19,11 +24,32 @@ module Fluent
19
24
  def initialize
20
25
  super
21
26
  require 'erb'
27
+ require 'socket'
22
28
  end
23
29
 
24
30
  def configure(conf)
25
31
  super
26
32
  @typetalk = Typetalk.new(conf['client_id'], conf['client_secret'])
33
+ @hostname = Socket.gethostname
34
+
35
+ @out_keys = @out_keys.split(',')
36
+
37
+ begin
38
+ @message % (['1'] * @out_keys.length)
39
+ rescue ArgumentError
40
+ raise Fluent::ConfigError, "string specifier '%s' and out_keys specification mismatch"
41
+ end
42
+
43
+ if @time_format
44
+ f = @time_format
45
+ tf = Fluent::TimeFormatter.new(f, true) # IRC notification is formmatted as localtime only...
46
+ @time_format_proc = tf.method(:format)
47
+ @time_parse_proc = Proc.new {|str| Time.strptime(str, f).to_i }
48
+ else
49
+ @time_format_proc = Proc.new {|time| time.to_s }
50
+ @time_parse_proc = Proc.new {|str| str.to_i }
51
+ end
52
+
27
53
  end
28
54
 
29
55
  def start
@@ -49,14 +75,34 @@ module Fluent
49
75
  end
50
76
 
51
77
  def send_message(tag, time, record)
52
- message = ERB.new(@template).result(binding)
78
+ message = evaluate_message(tag, time, record)
53
79
  @typetalk.post(@topic_id, message)
54
80
  end
55
81
 
82
+ def evaluate_message(tag, time, record)
83
+
84
+ values = out_keys.map do |key|
85
+ case key
86
+ when @time_key
87
+ @time_format_proc.call(time)
88
+ when @tag_key
89
+ tag
90
+ when "$hostname"
91
+ @hostname
92
+ else
93
+ record[key].to_s
94
+ end
95
+ end
96
+
97
+ (message % values).gsub(/\\n/, "\n")
98
+ end
99
+
56
100
  end
57
101
 
58
102
  class Typetalk
59
103
 
104
+ USER_AGENT = "fluent-plugin-typetalk Ruby/#{RUBY_VERSION}"
105
+
60
106
  def initialize(client_id, client_secret)
61
107
  require 'net/http'
62
108
  require 'uri'
@@ -76,12 +122,17 @@ module Fluent
76
122
  res = @http.post(
77
123
  "/api/v1/topics/#{topic_id}",
78
124
  "message=#{message}",
79
- { 'Authorization' => "Bearer #{@access_token}" }
125
+ { 'Authorization' => "Bearer #{@access_token}", 'User-Agent' => USER_AGENT }
80
126
  )
81
127
 
82
128
  # todo: handling 429
83
129
  unless res and res.is_a?(Net::HTTPSuccess)
84
- raise TypetalkError, "failed to post to typetalk.in, code: #{res && res.code}"
130
+ msg = ""
131
+ unless res.body.nil?
132
+ json = JSON.parse(res.body)
133
+ msg = json.fetch('errors', [])[0].fetch('message',"")
134
+ end
135
+ raise TypetalkError, "failed to post, msg: #{msg}, code: #{res.code}"
85
136
  end
86
137
 
87
138
  end
@@ -106,7 +157,8 @@ module Fluent
106
157
 
107
158
  res = @http.post(
108
159
  "/oauth2/access_token",
109
- params
160
+ params,
161
+ { 'User-Agent' => USER_AGENT }
110
162
  )
111
163
 
112
164
  if res.is_a?(Net::HTTPUnauthorized)
@@ -13,7 +13,8 @@ class TypetalkOutputTest < Test::Unit::TestCase
13
13
  client_id 123456
14
14
  client_secret secret
15
15
  topic_id 1
16
- template <%= record.to_json %>
16
+ message notice : %s
17
+ out_keys message
17
18
  ]
18
19
 
19
20
  def create_driver(conf = CONFIG, tag = 'test')
@@ -28,15 +29,16 @@ class TypetalkOutputTest < Test::Unit::TestCase
28
29
 
29
30
  def test_write
30
31
  d = create_driver()
31
- stub(d.instance.typetalk).post(1, '{"message":"test1"}')
32
+ stub(d.instance.typetalk).post(1, 'notice : test1')
32
33
  d.emit({'message' => 'test1'})
33
34
  d.run()
34
35
  end
35
36
 
36
37
  def test_template
37
38
  d = create_driver(CONFIG, 'warn')
38
- d.instance.template = "<%= tag %> at <%= Time.at(time).localtime %>\n<%= record.to_json %>"
39
- stub(d.instance.typetalk).post(1, "warn at 2014-05-13 01:05:38 +0900\n{\"message\":\"test1\"}")
39
+ d.instance.message = "notice : %s [%s]"
40
+ d.instance.out_keys = ["message", "time"]
41
+ stub(d.instance.typetalk).post(1, "notice : test1 [1399910738]")
40
42
 
41
43
  ENV["TZ"]="Asia/Tokyo"
42
44
  t = Time.strptime('2014-05-13 01:05:38', '%Y-%m-%d %T')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-typetalk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - tksmd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-12 00:00:00.000000000 Z
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -74,6 +74,7 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - .gitignore
77
+ - .travis.yml
77
78
  - Gemfile
78
79
  - LICENSE.txt
79
80
  - README.md
@@ -94,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
95
  requirements:
95
96
  - - ! '>='
96
97
  - !ruby/object:Gem::Version
97
- version: '0'
98
+ version: 1.9.3
98
99
  required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  requirements:
100
101
  - - ! '>='