ffwd-kairosdb 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ca0c918e4d9ca750323bf9c6548a36f4ad439aa
4
+ data.tar.gz: a92c004e3588e43d1359d8eab17245bd2d51f1a0
5
+ SHA512:
6
+ metadata.gz: 28eeea4df3f61cdbbb27cabeceb378d6434676395d01ad213bd7163661da80958fb001047fdca2f6aef4c07c2f55415bcd33b82df82596e0e3009c44229a7037
7
+ data.tar.gz: fec8b9af3382cfbfd135dfb835581b582572c70ce36fe34635da054d99586f813e26c32102dc81090d240f8e7557f135133118e8396a8893c86157a830201b82
@@ -0,0 +1,130 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require 'ffwd/reporter'
17
+
18
+ require_relative 'utils'
19
+
20
+ module FFWD::Plugin::KairosDB
21
+ class Output
22
+ include FFWD::Reporter
23
+ setup_reporter :keys => [:dropped_metrics, :sent_metrics, :failed_metrics]
24
+
25
+ attr_reader :log, :reporter_meta
26
+
27
+ HEADER = {
28
+ "Content-Type" => "application/json"
29
+ }
30
+
31
+ API_PATH = "/api/v1/datapoints"
32
+
33
+ def initialize core, log, url, flush_interval, buffer_limit
34
+ @log = log
35
+ @url = url
36
+ @flush_interval = flush_interval
37
+ @buffer_limit = buffer_limit
38
+ @reporter_meta = {:type => "kairosdb_out", :url => @url}
39
+
40
+ @buffer = []
41
+ @pending = nil
42
+ @c = nil
43
+
44
+ @sub = nil
45
+
46
+ core.starting do
47
+ log.info "Will send events to #{@url}"
48
+
49
+ @c = EM::HttpRequest.new(@url)
50
+
51
+ @sub = core.output.metric_subscribe do |metric|
52
+ if @buffer.size >= @buffer_limit
53
+ increment :dropped_metrics, 1
54
+ next
55
+ end
56
+
57
+ @buffer << metric
58
+ check_timer!
59
+ end
60
+ end
61
+
62
+ core.stopping do
63
+ # Close is buggy, don.
64
+ #@c.close
65
+
66
+ log.info "Closing connection to #{@url}"
67
+
68
+ if @sub
69
+ @sub.unsubscribe
70
+ @sub = nil
71
+ end
72
+
73
+ if @timer
74
+ @timer.cancel
75
+ @timer = nil
76
+ end
77
+ end
78
+ end
79
+
80
+ def flush!
81
+ if @timer
82
+ @timer.cancel
83
+ @timer = nil
84
+ end
85
+
86
+ if @pending
87
+ log.info "Request already in progress, dropping metrics"
88
+ increment :dropped_metrics, @buffer.size
89
+ @buffer.clear
90
+ return
91
+ end
92
+
93
+ unless @c
94
+ log.error "Dropping metrics, no active connection available"
95
+ increment :dropped_metrics, @buffer.size
96
+ @buffer.clear
97
+ return
98
+ end
99
+
100
+ buffer_size = @buffer.size
101
+ metrics = Utils.make_metrics(@buffer)
102
+ metrics = JSON.dump(metrics)
103
+ @buffer.clear
104
+
105
+ log.info "Sending #{buffer_size} metric(s) to #{@url}"
106
+ @pending = @c.post(:path => API_PATH, :head => HEADER, :body => metrics)
107
+
108
+ @pending.callback do
109
+ increment :sent_metrics, buffer_size
110
+ @pending = nil
111
+ end
112
+
113
+ @pending.errback do
114
+ log.error "Failed to submit events: #{@pending.error}"
115
+ increment :failed_metrics, buffer_size
116
+ @pending = nil
117
+ end
118
+ end
119
+
120
+ def check_timer!
121
+ return if @timer
122
+
123
+ log.debug "Setting timer to #{@flush_interval}s"
124
+
125
+ @timer = EM::Timer.new(@flush_interval) do
126
+ flush!
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,64 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ module FFWD::Plugin::KairosDB
17
+ module Utils
18
+ # groups similar metadata and escapes them using the suite of safe_*
19
+ # functions available.
20
+ #
21
+ # Should prevent unecessary invocations of safe_entry by only adding new
22
+ # groups of the source metric differs (||=).
23
+ def self.make_metrics buffer
24
+ groups = {}
25
+
26
+ buffer.each do |m|
27
+ entry = {:host => m.host, :name => m.key, :attributes => m.attributes}
28
+ group = (groups[entry] ||= safe_entry(entry).merge(:datapoints => []))
29
+ group[:datapoints] << [(m.time.to_f * 1000).to_i, m.value]
30
+ end
31
+
32
+ return groups.values
33
+ end
34
+
35
+ # make safe entry out of available information.
36
+ def self.safe_entry entry
37
+ name = entry[:name]
38
+ host = entry[:host]
39
+ attributes = entry[:attributes]
40
+ {:name => safe_string(name), :tags => safe_tags(host, attributes)}
41
+ end
42
+
43
+ # Warning: These are the 'bad' characters I've been able to reverse
44
+ # engineer so far.
45
+ def self.safe_string string
46
+ string = string.to_s
47
+ string = string.gsub " ", "/"
48
+ string.gsub ":", "_"
49
+ end
50
+
51
+ # Warning: KairosDB ignores complete metrics if you use tags which have no
52
+ # values, therefore I have not figured out a way to transport 'tags'.
53
+ def self.safe_tags host, attributes
54
+ tags = {"host" => safe_string(host)}
55
+
56
+ attributes.each do |key, value|
57
+ tags[safe_string(key)] = safe_string(value)
58
+ end
59
+
60
+ return tags
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,22 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ module FFWD
17
+ module Plugin
18
+ module KairosDB
19
+ VERSION = "0.1.0"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ # $LICENSE
2
+ # Copyright 2013-2014 Spotify AB. All rights reserved.
3
+ #
4
+ # The contents of this file are licensed under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations under
14
+ # the License.
15
+
16
+ require 'eventmachine'
17
+ require 'em-http'
18
+
19
+ require 'ffwd/logging'
20
+ require 'ffwd/plugin'
21
+ require 'ffwd/reporter'
22
+
23
+ require_relative 'kairosdb/output'
24
+
25
+ module FFWD::Plugin::KairosDB
26
+ include FFWD::Plugin
27
+ include FFWD::Logging
28
+
29
+ register_plugin "kairosdb"
30
+
31
+ DEFAULT_URL = "http://localhost:8080"
32
+ DEFAULT_FLUSH_INTERVAL = 10
33
+ DEFAULT_BUFFER_LIMIT = 100000
34
+
35
+ def self.setup_output opts, core
36
+ url = opts[:url] || DEFAULT_URL
37
+ flush_interval = opts[:flush_interval] || DEFAULT_FLUSH_INTERVAL
38
+ buffer_limit = opts[:buffer_limit] || DEFAULT_BUFFER_LIMIT
39
+ Output.new core, log, url, flush_interval, buffer_limit
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffwd-kairosdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John-John Tedro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: em-http-request
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: ffwd
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-mocks
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
+ description:
70
+ email:
71
+ - udoprog@spotify.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/ffwd/plugin/kairosdb.rb
77
+ - lib/ffwd/plugin/kairosdb/output.rb
78
+ - lib/ffwd/plugin/kairosdb/utils.rb
79
+ - lib/ffwd/plugin/kairosdb/version.rb
80
+ homepage: https://github.com/spotify/ffwd
81
+ licenses:
82
+ - Apache 2.0
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: KairosDB support for FFWD.
104
+ test_files: []