fluent-plugin-dd 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWMzODQ3ODM1YTNiMGFmNGMyMGJmNWQ0NzYxNjcxYzdkNWNhMmRiOQ==
5
+ data.tar.gz: !binary |-
6
+ OTFlZTRhZWIzZjdkMGU3NGY1MTRjNjU0MmZjODRlNTJkMTc1ZGJkZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NDA5OWE0NjEzMjcxYWU3MzljNjFjMzhmM2VkYTM1MjQwNGZkNDU0ZDE5M2Ez
10
+ ZjYzM2IzNWE5ODljZmM2YTA4YzE4MGZiNDAwODY2YTJjYWIzODMxMWYxNDZj
11
+ NTEzZGI5ZTBkNDRjNmFiZGQ4NWM1NjAzMTFiZmE3ZGU3ZDI0Mzg=
12
+ data.tar.gz: !binary |-
13
+ ZmJkOWUyMGU1YmZmNzhhMWMxNTJlNTM3N2U0YzQxODBjZjBjMmM2NzY0YzEw
14
+ NmY0N2JjZjg0OWRjMjAyZTU2MmQ5OTJhOGJmOTY3NjlkNTEwYjAzZDUwOTc3
15
+ YTRlMTlhZGE2ZjM5MmZmYjM1MDkxZGI3YjBlZDFjMzc5NTk3NTk=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-dd.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Genki Sugawara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # fluent-plugin-dd
2
+
3
+ Output plugin for Datadog
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/fluent-plugin-dd.png)](http://badge.fury.io/rb/fluent-plugin-dd)
6
+ [![Build Status](https://drone.io/bitbucket.org/winebarrel/fluent-plugin-dd/status.png)](https://drone.io/bitbucket.org/winebarrel/fluent-plugin-dd/latest)
7
+
8
+ ## Installation
9
+
10
+ $ gem install fluent-plugin-dd
11
+
12
+ ## Configuration
13
+
14
+ ```
15
+ <match datadog.**>
16
+ type dd
17
+ dd_api_key ...
18
+ #host my_host.example.com
19
+ </match>
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```sh
25
+ echo '{"metric":"some.metric.name", "value":50.0}' | fluent-cat datadog.metric
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'fluent-plugin-dd'
4
+ spec.version = '0.1.0'
5
+ spec.authors = ['Genki Sugawara']
6
+ spec.email = ['sugawara@cookpad.com']
7
+ spec.description = %q{Output plugin for Datadog}
8
+ spec.summary = %q{Output plugin for Datadog}
9
+ spec.homepage = 'https://bitbucket.org/winebarrel/fluent-plugin-dd'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_dependency 'fluentd'
18
+ spec.add_dependency 'dogapi'
19
+ spec.add_development_dependency 'bundler', '~> 1.3'
20
+ spec.add_development_dependency 'rake'
21
+ spec.add_development_dependency 'rspec', '>= 2.11.0'
22
+ end
@@ -0,0 +1,79 @@
1
+ class Fluent::DdOutput < Fluent::BufferedOutput
2
+ include Fluent::SetTimeKeyMixin
3
+ include Fluent::SetTagKeyMixin
4
+
5
+ Fluent::Plugin.register_output('dd', self)
6
+
7
+ unless method_defined?(:log)
8
+ define_method('log') { $log }
9
+ end
10
+
11
+ config_set_default :include_time_key, true
12
+ config_set_default :include_tag_key, true
13
+
14
+ config_param :dd_api_key, :string
15
+ config_param :host, :string, :default => nil
16
+
17
+ def initialize
18
+ super
19
+ require 'dogapi'
20
+ require 'socket'
21
+ require 'time'
22
+ end
23
+
24
+ def start
25
+ super
26
+ end
27
+
28
+ def shutdown
29
+ super
30
+ end
31
+
32
+ def configure(conf)
33
+ super
34
+
35
+ unless @dd_api_key
36
+ raise Fluent::ConfigError, '`dd_api_key` is required'
37
+ end
38
+
39
+ unless @host
40
+ @host = %x[hostname -f 2> /dev/null].strip
41
+ @host = Socket.gethostname if @host.empty?
42
+ end
43
+
44
+ @dog = Dogapi::Client.new(@dd_api_key, nil, @host)
45
+ end
46
+
47
+ def format(tag, time, record)
48
+ record.to_msgpack
49
+ end
50
+
51
+ def write(chunk)
52
+ enum = chunk.to_enum(:msgpack_each)
53
+
54
+ enum.select {|record|
55
+ unless record['metric']
56
+ log.warn("`metric` key does not exist: #{record.inspect}")
57
+ end
58
+
59
+ record['metric']
60
+ }.chunk {|record|
61
+ record.values_at('metric', 'tag', 'host', 'type')
62
+ }.each {|i, records|
63
+ metric, tag, host, type = i
64
+
65
+ points = records.map do |record|
66
+ time = Time.parse(record['time'])
67
+ value = record['value']
68
+ [time, value]
69
+ end
70
+
71
+ options = {}
72
+ options['tags'] = [tag] if tag
73
+ options['host'] = host if host
74
+ options['type'] = type if type
75
+
76
+ @dog.emit_points(metric, points, options)
77
+ }
78
+ end
79
+ end
@@ -0,0 +1,138 @@
1
+ describe Fluent::DdOutput do
2
+ let(:time) {
3
+ Time.parse('2014-02-08 04:14:15 UTC').to_i
4
+ }
5
+
6
+ it 'should receive an API key' do
7
+ Dogapi::Client.should_receive(:new).with("test_dd_api_key", nil, "test_host")
8
+ run_driver {|d, dog| }
9
+ end
10
+
11
+ it 'should be called emit_points' do
12
+ run_driver do |d, dog|
13
+ dog.should_receive(:emit_points).with(
14
+ "some.metric.name",
15
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 50.0],
16
+ [Time.parse("2014-02-08 04:14:15 UTC"), 100.0]],
17
+ {"tags"=>["test.default"]}
18
+ )
19
+
20
+ d.emit({"metric" => "some.metric.name", "value" => 50.0}, time)
21
+ d.emit({"metric" => "some.metric.name", "value" => 100.0}, time)
22
+ end
23
+ end
24
+
25
+ it 'should be called emit_points for each tag' do
26
+ run_driver do |d, dog|
27
+ dog.should_receive(:emit_points).with(
28
+ "some.metric.name",
29
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 50.0],
30
+ [Time.parse("2014-02-08 04:14:15 UTC"), 100.0]],
31
+ {"tags"=>["test.1"]}
32
+ )
33
+
34
+ dog.should_receive(:emit_points).with(
35
+ "some.metric.name",
36
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 150.0],
37
+ [Time.parse("2014-02-08 04:14:15 UTC"), 200.0]],
38
+ {"tags"=>["test.2"]}
39
+ )
40
+
41
+ dog.should_receive(:emit_points).with(
42
+ "some.metric.name",
43
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 250.0],
44
+ [Time.parse("2014-02-08 04:14:15 UTC"), 300.0]],
45
+ {"tags"=>["test.3"]}
46
+ )
47
+
48
+ d.tag = 'test.1'
49
+ d.emit({"metric" => "some.metric.name", "value" => 50.0}, time)
50
+ d.emit({"metric" => "some.metric.name", "value" => 100.0}, time)
51
+
52
+ d.tag = 'test.2'
53
+ d.emit({"metric" => "some.metric.name", "value" => 150.0}, time)
54
+ d.emit({"metric" => "some.metric.name", "value" => 200.0}, time)
55
+
56
+ d.tag = 'test.3'
57
+ d.emit({"metric" => "some.metric.name", "value" => 250.0}, time)
58
+ d.emit({"metric" => "some.metric.name", "value" => 300.0}, time)
59
+ end
60
+ end
61
+
62
+ it 'should be called emit_points for each host' do
63
+ run_driver do |d, dog|
64
+ dog.should_receive(:emit_points).with(
65
+ "some.metric.name",
66
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 50.0],
67
+ [Time.parse("2014-02-08 04:14:15 UTC"), 100.0]],
68
+ {"tags"=>["test.default"], "host"=>"www1.example.com"}
69
+ )
70
+
71
+ dog.should_receive(:emit_points).with(
72
+ "some.metric.name",
73
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 150.0],
74
+ [Time.parse("2014-02-08 04:14:15 UTC"), 200.0]],
75
+ {"tags"=>["test.default"], "host"=>"www2.example.com"}
76
+ )
77
+
78
+ dog.should_receive(:emit_points).with(
79
+ "some.metric.name",
80
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 250.0],
81
+ [Time.parse("2014-02-08 04:14:15 UTC"), 300.0]],
82
+ {"tags"=>["test.default"], "host"=>"www3.example.com"}
83
+ )
84
+
85
+ d.emit({"metric" => "some.metric.name", "value" => 50.0, "host" => "www1.example.com"}, time)
86
+ d.emit({"metric" => "some.metric.name", "value" => 100.0, "host" => "www1.example.com"}, time)
87
+ d.emit({"metric" => "some.metric.name", "value" => 150.0, "host" => "www2.example.com"}, time)
88
+ d.emit({"metric" => "some.metric.name", "value" => 200.0, "host" => "www2.example.com"}, time)
89
+ d.emit({"metric" => "some.metric.name", "value" => 250.0, "host" => "www3.example.com"}, time)
90
+ d.emit({"metric" => "some.metric.name", "value" => 300.0, "host" => "www3.example.com"}, time)
91
+ end
92
+ end
93
+
94
+ it 'should be called emit_points for each type' do
95
+ run_driver do |d, dog|
96
+ dog.should_receive(:emit_points).with(
97
+ "some.metric.name",
98
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 50.0],
99
+ [Time.parse("2014-02-08 04:14:15 UTC"), 100.0]],
100
+ {"tags"=>["test.default"], "type"=>"gauge"}
101
+ )
102
+
103
+ dog.should_receive(:emit_points).with(
104
+ "some.metric.name",
105
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 150.0],
106
+ [Time.parse("2014-02-08 04:14:15 UTC"), 200.0]],
107
+ {"tags"=>["test.default"], "type"=>"counter"}
108
+ )
109
+
110
+ d.emit({"metric" => "some.metric.name", "value" => 50.0, "type" => "gauge"}, time)
111
+ d.emit({"metric" => "some.metric.name", "value" => 100.0, "type" => "gauge"}, time)
112
+ d.emit({"metric" => "some.metric.name", "value" => 150.0, "type" => "counter"}, time)
113
+ d.emit({"metric" => "some.metric.name", "value" => 200.0, "type" => "counter"}, time)
114
+ end
115
+ end
116
+
117
+ it 'should be skipped if `metric` key does not exists' do
118
+ run_driver do |d, dog|
119
+ dog.should_receive(:emit_points).with(
120
+ "some.metric.name",
121
+ [[Time.parse("2014-02-08 04:14:15 UTC"), 50.0],
122
+ [Time.parse("2014-02-08 04:14:15 UTC"), 100.0]],
123
+ {"tags"=>["test.default"]}
124
+ )
125
+
126
+ log = d.instance.log
127
+ log.should_receive(:warn)
128
+ .with('`metric` key does not exist: {"no metric"=>"some.metric.name", "value"=>51.0, "time"=>"2014-02-08T04:14:15Z", "tag"=>"test.default"}')
129
+ log.should_receive(:warn)
130
+ .with('`metric` key does not exist: {"no metric"=>"some.metric.name", "value"=>101.0, "time"=>"2014-02-08T04:14:15Z", "tag"=>"test.default"}')
131
+
132
+ d.emit({"no metric" => "some.metric.name", "value" => 51.0}, time)
133
+ d.emit({"no metric" => "some.metric.name", "value" => 101.0}, time)
134
+ d.emit({"metric" => "some.metric.name", "value" => 50.0}, time)
135
+ d.emit({"metric" => "some.metric.name", "value" => 100.0}, time)
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,34 @@
1
+ require 'fluent/test'
2
+ require 'fluent/plugin/out_dd'
3
+ require 'dogapi'
4
+ require 'time'
5
+
6
+ # Disable Test::Unit
7
+ module Test::Unit::RunCount; def run(*); end; end
8
+
9
+ RSpec.configure do |config|
10
+ config.before(:all) do
11
+ Fluent::Test.setup
12
+ end
13
+ end
14
+
15
+ def run_driver(options = {})
16
+ options = options.dup
17
+
18
+ dd_api_key = options[:dd_api_key] || 'test_dd_api_key'
19
+ host = options[:host] || 'test_host'
20
+
21
+ fluentd_conf = <<-EOS
22
+ type datadog
23
+ dd_api_key #{dd_api_key}
24
+ host #{host}
25
+ EOS
26
+
27
+ tag = options[:tag] || 'test.default'
28
+ driver = Fluent::Test::OutputTestDriver.new(Fluent::DdOutput, tag).configure(fluentd_conf)
29
+
30
+ driver.run do
31
+ dog = driver.instance.instance_variable_get(:@dog)
32
+ yield(driver, dog)
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-dd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dogapi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.11.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.11.0
83
+ description: Output plugin for Datadog
84
+ email:
85
+ - sugawara@cookpad.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - fluent-plugin-dd.gemspec
97
+ - lib/fluent/plugin/out_dd.rb
98
+ - spec/out_dd_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://bitbucket.org/winebarrel/fluent-plugin-dd
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.1.11
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Output plugin for Datadog
124
+ test_files:
125
+ - spec/out_dd_spec.rb
126
+ - spec/spec_helper.rb