observed-gauge 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWE3MTcxMTExMDI3YmFhZTc5MjMyOTQzZDBjMmM0ZDdhOGJmMDIwZA==
5
+ data.tar.gz: !binary |-
6
+ OWIxMTJhOTBhMTgyNGQzYmVkMzdkM2M1Y2RhNzU0ZTk4MmZhOWM0MA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZWYyNDJmNjFmOGFiY2NiZWM2YmE4NWM1YWM2Mjg4Yzg2MjFjZTlhNmQzN2My
10
+ MDIwYzIwYmYzZDI3N2U5NjNiYzczNGM5ZjkzNjg0NDdjYWM2NTAzNmEwODMx
11
+ OWVhZWNkNTI1NGY0MDYxNWMxYWU3ZGUyMzRjODBkNmNkNGQzZjg=
12
+ data.tar.gz: !binary |-
13
+ MGM5MzYxYjg5NDYzYWEyOGVjNzExYjA1OGE2N2RjNmNhNzcyYmE2YWMwMDEx
14
+ MDNjNGM2NDhjNDU4OGY4MDM2YzBkYjg2OWRhY2E3ZTA2M2FmODQxNGMzMDY3
15
+ NDVkNzUzNjI5YTczNGY0OTZkZGRmOThjN2RkY2VkZWY1ZWQ2ODE=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ *~
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in observed-gauge.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 GREE, Inc.
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,29 @@
1
+ # Observed::Gauge
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'observed-gauge'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install observed-gauge
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module Observed
2
+ module Gauge
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,117 @@
1
+ require 'observed/reporter'
2
+ require 'observed/reporter/regexp_matching'
3
+ require 'observed/gauge/version'
4
+ require 'logger'
5
+ require 'rrd'
6
+
7
+ module Observed
8
+ module Plugins
9
+ class Gauge < Observed::Reporter
10
+
11
+ plugin_name 'gauge'
12
+
13
+ include Observed::Reporter::RegexpMatching
14
+
15
+ attribute :tag
16
+ attribute :key_path
17
+ attribute :coerce, default: ->(data){ data }
18
+ attribute :rrd
19
+ attribute :step
20
+ attribute :period
21
+
22
+ def report(tag, time, data)
23
+ rewrote = update_value_for_key_path(data, key_path) do |v|
24
+ sample = coerce.call(v)
25
+ average = get_cdp_updated_with(time, sample)
26
+ average
27
+ end
28
+ unless fetch_value_for_key_path(rewrote, key_path).nan?
29
+ system.report(self.tag, rewrote)
30
+ end
31
+ end
32
+
33
+ def prepare_rrd(args)
34
+ start = args[:start]
35
+ logger.debug "Creating a rrd file named '#{args[:rrd]}' with options {:start => #{start}}"
36
+ result = RRD::Builder.new(args[:rrd], start: start, step: step.seconds).tap do |builder|
37
+ builder.datasource data_source, :type => :gauge, :heartbeat => period.seconds, :min => 0, :max => :unlimited
38
+ builder.archive :average, :every => period.seconds, :during => period.seconds
39
+ builder.save
40
+ end
41
+ logger.debug "Builder#save returned: #{result.inspect}"
42
+ end
43
+
44
+ private
45
+
46
+ def update_value_for_key_path(data, key_path, &block)
47
+ first, *rest = split_key_path(key_path)
48
+ data = data.dup
49
+ dug_data = data[first] || data[first.intern]
50
+
51
+ if rest.empty?
52
+ hash_update(data, first, block.call(dug_data))
53
+ else
54
+ hash_update(data, first, update_value_for_key_path(dug_data, rest, &block))
55
+ end
56
+
57
+ data
58
+ end
59
+
60
+ def hash_update(hash, key, value)
61
+ if hash[key]
62
+ hash[key] = value
63
+ else
64
+ hash[key.intern] = value
65
+ end
66
+ end
67
+
68
+ def fetch_value_for_key_path(data, key_path)
69
+ first, *rest = split_key_path(key_path)
70
+ dug_data = data[first] || data[first.intern]
71
+ if rest.empty?
72
+ dug_data
73
+ else
74
+ fetch_value_for_key_path(dug_data, rest)
75
+ end
76
+ end
77
+
78
+ def split_key_path(key_path)
79
+ case key_path
80
+ when Array
81
+ key_path
82
+ when String
83
+ key_path.split('.')
84
+ else
85
+ fail "Unexpected type of key_path met. Expected an Array or a String, but it was a(n) #{key_path.class}"
86
+ end
87
+ end
88
+
89
+ def data_source
90
+ self.key_path.gsub('.', '_')
91
+ end
92
+
93
+ # @param [Time] time
94
+ def get_cdp_updated_with(time, value)
95
+ rrd_path = self.rrd
96
+ t = time.to_i
97
+
98
+ rrd = RRD::Base.new(rrd_path)
99
+
100
+ unless File.exist? rrd_path
101
+ prepare_rrd(rrd: rrd_path, start: t)
102
+ end
103
+
104
+ logger.debug "Updating the data source '#{data_source}' with the value #{value} with timestamp #{t}"
105
+ rrd.update t, value
106
+
107
+ logger.debug rrd.fetch!(:average)[-2..-1]
108
+
109
+ rrd.fetch(:average)[-2..-1].first.last
110
+ end
111
+
112
+ def logger
113
+ @logger ||= Logger.new(STDOUT)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'observed/gauge/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'observed-gauge'
8
+ spec.version = Observed::Gauge::VERSION
9
+ spec.authors = ['KUOKA Yusuke']
10
+ spec.email = ['yusuke.kuoka@gree.net']
11
+ spec.description = %q{Gauge plugin for Observed}
12
+ spec.summary = %q{A plugin to consolidate outputs from other Observed output plugins by averaging values by averaging them over configured periods}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'observed', '~> 0.1.0'
22
+ spec.add_dependency 'rrd-ffi', '~> 0.2.14'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'mocha'
28
+ spec.add_development_dependency 'fakefs'
29
+ spec.add_development_dependency 'simplecov'
30
+ end
@@ -0,0 +1,195 @@
1
+ require 'spec_helper'
2
+
3
+ require 'observed/gauge'
4
+
5
+ shared_examples_for 'the observed-gauge plugin' do
6
+
7
+ subject {
8
+ described_class.new
9
+ }
10
+
11
+ before {
12
+ File.delete(rrd) if File.exist?(rrd)
13
+
14
+ subject.configure rrd: rrd, key_path: key_path
15
+ }
16
+
17
+ it 'matches against the tag using a pattern described in regular expression' do
18
+ expect(subject.match('test.foo')).to be_true
19
+ end
20
+
21
+ it 'reports data with averaged values' do
22
+ subject.prepare_rrd(start: t - 120, rrd: rrd)
23
+ system.expects(:report).with(tag, expected_data.freeze).once
24
+ expect { subject.report('test.foo', t - 120, data) }.to_not raise_error
25
+ expect { subject.report('test.foo', t - 60, data) }.to_not raise_error
26
+ expect { subject.report('test.foo', t, data) }.to_not raise_error
27
+ end
28
+
29
+ it 'creates rrd files automatically on first report' do
30
+ expect { subject.report('test.foo', t, data) }.to_not raise_error
31
+
32
+ expect { File.exist? rrd }.to be_true
33
+ end
34
+ end
35
+
36
+ describe Observed::Plugins::Gauge do
37
+ subject {
38
+ Observed::Plugins::Gauge.new
39
+ }
40
+
41
+ it 'has a name' do
42
+ expect(described_class.plugin_name).to eq('gauge')
43
+ end
44
+
45
+ context 'with configuration' do
46
+
47
+ before {
48
+
49
+ subject.configure(
50
+ system: system,
51
+ tag_pattern: tag_pattern,
52
+ tag: tag,
53
+ step: step,
54
+ period: period
55
+ )
56
+ }
57
+
58
+ after {
59
+ File.delete(rrd) if File.exist?(rrd)
60
+ }
61
+
62
+ let(:t) {
63
+ Time.now
64
+ }
65
+
66
+ let(:system) {
67
+ sys = mock('system')
68
+
69
+ sys.stubs(:now).returns(t)
70
+
71
+ sys
72
+ }
73
+
74
+ let(:tag_pattern) {
75
+ /test.\.*/
76
+ }
77
+
78
+ let(:tag) {
79
+ 'test.out'
80
+ }
81
+
82
+ let(:step) {
83
+ 10
84
+ }
85
+
86
+ let(:period) {
87
+ 60
88
+ }
89
+
90
+ before {
91
+ subject.configure(
92
+ system: system,
93
+ tag_pattern: tag_pattern,
94
+ tag: tag,
95
+ step: step,
96
+ period: period,
97
+ key_path: key_path
98
+ )
99
+ }
100
+
101
+ context 'with an incorrect key path' do
102
+
103
+ let(:key_path) {
104
+ 123
105
+ }
106
+
107
+ let(:rrd) {
108
+ 'gauge_incorrect_key_path.rrd'
109
+ }
110
+
111
+ let(:data) {
112
+ { response: { time: 10 }}
113
+ }
114
+
115
+ let(:t) {
116
+ Time.now
117
+ }
118
+
119
+ it 'raise an error' do
120
+ expect { subject.report('test.foo', t, data) }.to raise_error(/Unexpected type of key_path met/)
121
+ end
122
+ end
123
+
124
+ context 'with a correct key path' do
125
+
126
+ let(:key_path) {
127
+ 'response.time'
128
+ }
129
+
130
+ let(:data_source) {
131
+ 'response_time'
132
+ }
133
+
134
+ context 'with the default coercer' do
135
+
136
+ context 'with data whose keys are symbols' do
137
+ let(:rrd) {
138
+ 'gauge_spec_symbols.rrd'
139
+ }
140
+
141
+ let(:data) {
142
+ { response: { time: 10 }}
143
+ }
144
+
145
+ let(:expected_data) {
146
+ { response: { time: 10 }}
147
+ }
148
+
149
+ it_behaves_like 'the observed-gauge plugin'
150
+ end
151
+
152
+ context 'with data whose keys are strings' do
153
+ let(:rrd) {
154
+ 'gauge_spec_strings.rrd'
155
+ }
156
+
157
+ let(:data) {
158
+ {'response' => {'time' => 10}}
159
+ }
160
+
161
+ let(:expected_data) {
162
+ { 'response' => { 'time' => 10 }}
163
+ }
164
+
165
+ it_behaves_like 'the observed-gauge plugin'
166
+ end
167
+
168
+ end
169
+
170
+ context 'with a string-to-integer coercer for values' do
171
+
172
+ before {
173
+ subject.configure coercer: ->(v){ t.to_i }
174
+ }
175
+
176
+ let(:rrd) {
177
+ 'gauge_spec_coercer.rrd'
178
+ }
179
+
180
+ let(:data) {
181
+ {'response' => {'time' => '10'}}
182
+ }
183
+
184
+ let(:expected_data) {
185
+ { 'response' => { 'time' => 10 }}
186
+ }
187
+
188
+ it_behaves_like 'the observed-gauge plugin'
189
+ end
190
+
191
+ end
192
+
193
+ end
194
+
195
+ end
@@ -0,0 +1,25 @@
1
+ require 'fakefs/spec_helpers'
2
+ require 'rspec'
3
+
4
+ Dir["#{File.expand_path('..', __FILE__)}/support/**/*.rb"].each { |f| require f }
5
+
6
+ puts "Please do not update/create files while tests are running."
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ config.order = :random
11
+ config.filter_run :focus => true
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+
15
+ config.before(:each) do
16
+ @fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
17
+ end
18
+
19
+ config.mock_framework = :mocha
20
+ end
21
+
22
+ if RUBY_VERSION =~ /^1.9/
23
+ require 'simplecov'
24
+ SimpleCov.start
25
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: observed-gauge
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - KUOKA Yusuke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: observed
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.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.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rrd-ffi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.14
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.14
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: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: fakefs
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Gauge plugin for Observed
126
+ email:
127
+ - yusuke.kuoka@gree.net
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - lib/observed/gauge.rb
138
+ - lib/observed/gauge/version.rb
139
+ - observed-gauge.gemspec
140
+ - spec/gauge_spec.rb
141
+ - spec/spec_helper.rb
142
+ homepage: ''
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.0.5
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: A plugin to consolidate outputs from other Observed output plugins by averaging
166
+ values by averaging them over configured periods
167
+ test_files:
168
+ - spec/gauge_spec.rb
169
+ - spec/spec_helper.rb