pulse_meter_core 0.4.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.gitignore +19 -0
  2. data/.rbenv-version +1 -0
  3. data/.rspec +1 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +8 -0
  6. data/Gemfile +2 -0
  7. data/LICENSE +22 -0
  8. data/README.md +40 -0
  9. data/Rakefile +20 -0
  10. data/lib/pulse_meter/command_aggregator/async.rb +83 -0
  11. data/lib/pulse_meter/command_aggregator/sync.rb +18 -0
  12. data/lib/pulse_meter/command_aggregator/udp.rb +48 -0
  13. data/lib/pulse_meter/mixins/dumper.rb +87 -0
  14. data/lib/pulse_meter/mixins/utils.rb +155 -0
  15. data/lib/pulse_meter/observer.rb +118 -0
  16. data/lib/pulse_meter/observer/extended.rb +32 -0
  17. data/lib/pulse_meter/sensor.rb +61 -0
  18. data/lib/pulse_meter/sensor/base.rb +88 -0
  19. data/lib/pulse_meter/sensor/configuration.rb +106 -0
  20. data/lib/pulse_meter/sensor/counter.rb +39 -0
  21. data/lib/pulse_meter/sensor/hashed_counter.rb +36 -0
  22. data/lib/pulse_meter/sensor/hashed_indicator.rb +24 -0
  23. data/lib/pulse_meter/sensor/indicator.rb +35 -0
  24. data/lib/pulse_meter/sensor/multi.rb +97 -0
  25. data/lib/pulse_meter/sensor/timeline.rb +236 -0
  26. data/lib/pulse_meter/sensor/timeline_reduce.rb +68 -0
  27. data/lib/pulse_meter/sensor/timelined/average.rb +32 -0
  28. data/lib/pulse_meter/sensor/timelined/counter.rb +23 -0
  29. data/lib/pulse_meter/sensor/timelined/hashed_counter.rb +31 -0
  30. data/lib/pulse_meter/sensor/timelined/hashed_indicator.rb +30 -0
  31. data/lib/pulse_meter/sensor/timelined/indicator.rb +23 -0
  32. data/lib/pulse_meter/sensor/timelined/max.rb +19 -0
  33. data/lib/pulse_meter/sensor/timelined/median.rb +14 -0
  34. data/lib/pulse_meter/sensor/timelined/min.rb +19 -0
  35. data/lib/pulse_meter/sensor/timelined/multi_percentile.rb +34 -0
  36. data/lib/pulse_meter/sensor/timelined/percentile.rb +22 -0
  37. data/lib/pulse_meter/sensor/timelined/uniq_counter.rb +22 -0
  38. data/lib/pulse_meter/sensor/timelined/zset_based.rb +37 -0
  39. data/lib/pulse_meter/sensor/uniq_counter.rb +24 -0
  40. data/lib/pulse_meter/server.rb +0 -0
  41. data/lib/pulse_meter/server/command_line_options.rb +0 -0
  42. data/lib/pulse_meter/server/config_options.rb +0 -0
  43. data/lib/pulse_meter/server/sensors.rb +0 -0
  44. data/lib/pulse_meter/udp_server.rb +45 -0
  45. data/lib/pulse_meter_core.rb +66 -0
  46. data/pulse_meter_core.gemspec +33 -0
  47. data/spec/pulse_meter/command_aggregator/async_spec.rb +53 -0
  48. data/spec/pulse_meter/command_aggregator/sync_spec.rb +25 -0
  49. data/spec/pulse_meter/command_aggregator/udp_spec.rb +45 -0
  50. data/spec/pulse_meter/mixins/dumper_spec.rb +162 -0
  51. data/spec/pulse_meter/mixins/utils_spec.rb +212 -0
  52. data/spec/pulse_meter/observer/extended_spec.rb +92 -0
  53. data/spec/pulse_meter/observer_spec.rb +207 -0
  54. data/spec/pulse_meter/sensor/base_spec.rb +106 -0
  55. data/spec/pulse_meter/sensor/configuration_spec.rb +103 -0
  56. data/spec/pulse_meter/sensor/counter_spec.rb +54 -0
  57. data/spec/pulse_meter/sensor/hashed_counter_spec.rb +43 -0
  58. data/spec/pulse_meter/sensor/hashed_indicator_spec.rb +39 -0
  59. data/spec/pulse_meter/sensor/indicator_spec.rb +43 -0
  60. data/spec/pulse_meter/sensor/multi_spec.rb +137 -0
  61. data/spec/pulse_meter/sensor/timeline_spec.rb +88 -0
  62. data/spec/pulse_meter/sensor/timelined/average_spec.rb +6 -0
  63. data/spec/pulse_meter/sensor/timelined/counter_spec.rb +6 -0
  64. data/spec/pulse_meter/sensor/timelined/hashed_counter_spec.rb +8 -0
  65. data/spec/pulse_meter/sensor/timelined/hashed_indicator_spec.rb +8 -0
  66. data/spec/pulse_meter/sensor/timelined/indicator_spec.rb +6 -0
  67. data/spec/pulse_meter/sensor/timelined/max_spec.rb +7 -0
  68. data/spec/pulse_meter/sensor/timelined/median_spec.rb +7 -0
  69. data/spec/pulse_meter/sensor/timelined/min_spec.rb +7 -0
  70. data/spec/pulse_meter/sensor/timelined/multi_percentile_spec.rb +21 -0
  71. data/spec/pulse_meter/sensor/timelined/percentile_spec.rb +17 -0
  72. data/spec/pulse_meter/sensor/timelined/uniq_counter_spec.rb +9 -0
  73. data/spec/pulse_meter/sensor/uniq_counter_spec.rb +28 -0
  74. data/spec/pulse_meter/udp_server_spec.rb +36 -0
  75. data/spec/pulse_meter_spec.rb +73 -0
  76. data/spec/shared_examples/timeline_sensor.rb +439 -0
  77. data/spec/shared_examples/timelined_subclass.rb +23 -0
  78. data/spec/spec_helper.rb +37 -0
  79. data/spec/support/matchers.rb +34 -0
  80. data/spec/support/observered.rb +40 -0
  81. metadata +342 -0
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Counter do
4
+ let(:name){ :some_counter }
5
+ let(:sensor){ described_class.new(name) }
6
+ let(:redis){ PulseMeter.redis }
7
+
8
+ describe "#event" do
9
+ it "should increment sensor value by passed value" do
10
+ expect{ sensor.event(10) }.to change{ sensor.value }.from(0).to(10)
11
+ expect{ sensor.event(15) }.to change{ sensor.value }.from(10).to(25)
12
+ end
13
+
14
+ it "should truncate increment value" do
15
+ expect{ sensor.event(10.4) }.to change{ sensor.value }.from(0).to(10)
16
+ expect{ sensor.event(15.1) }.to change{ sensor.value }.from(10).to(25)
17
+ end
18
+ end
19
+
20
+ describe "#value_key" do
21
+ it "should be composed of sensor name and pulse_meter:value: prefix" do
22
+ sensor.value_key.should == "pulse_meter:value:#{name}"
23
+ end
24
+ end
25
+
26
+ describe "#value" do
27
+ it "should have initial value 0" do
28
+ sensor.value.should == 0
29
+ end
30
+
31
+ it "should store stringified value by value_key" do
32
+ sensor.event(123)
33
+ sensor.value.should == 123
34
+ redis.get(sensor.value_key).should == '123'
35
+ end
36
+ end
37
+
38
+ describe "#incr" do
39
+ it "should increment value by 1" do
40
+ expect{ sensor.incr }.to change{ sensor.value }.from(0).to(1)
41
+ expect{ sensor.incr }.to change{ sensor.value }.from(1).to(2)
42
+ end
43
+ end
44
+
45
+ describe "#cleanup" do
46
+ it "should remove all sensor data" do
47
+ sensor.annotate("My Counter")
48
+ sensor.event(123)
49
+ sensor.cleanup
50
+ redis.keys('*').should be_empty
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::HashedCounter do
4
+ let(:name){ :some_counter }
5
+ let(:sensor){ described_class.new(name) }
6
+ let(:redis){ PulseMeter.redis }
7
+
8
+ describe "#event" do
9
+ it "should increment sensor value by passed value" do
10
+ expect{ sensor.event({"foo" => 10}) }.to change{ sensor.value["foo"] }.from(0).to(10)
11
+ expect{ sensor.event({"foo" => 15}) }.to change{ sensor.value["foo"] }.from(10).to(25)
12
+ end
13
+
14
+ it "should truncate increment value" do
15
+ expect{ sensor.event({"foo" => 10.4}) }.to change{ sensor.value["foo"] }.from(0).to(10)
16
+ expect{ sensor.event({"foo" => 15.1}) }.to change{ sensor.value["foo"] }.from(10).to(25)
17
+ end
18
+
19
+ it "should increment total value" do
20
+ expect{ sensor.event({"foo" => 1, "bar" => 2}) }.to change{sensor.value["total"]}.from(0).to(3)
21
+ end
22
+ end
23
+
24
+ describe "#value" do
25
+ it "should have initial value 0" do
26
+ sensor.value["foo"].should == 0
27
+ end
28
+
29
+ it "should store redis hash by value_key" do
30
+ sensor.event({"foo" => 1})
31
+ sensor.value.should == {"foo" => 1, "total" => 1}
32
+ redis.hgetall(sensor.value_key).should == {"foo" => "1", "total" => "1"}
33
+ end
34
+ end
35
+
36
+ describe "#incr" do
37
+ it "should increment key value by 1" do
38
+ expect{ sensor.incr("foo") }.to change{ sensor.value["foo"] }.from(0).to(1)
39
+ expect{ sensor.incr("foo") }.to change{ sensor.value["foo"] }.from(1).to(2)
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::HashedIndicator do
4
+ let(:name){ :some_counter }
5
+ let(:sensor){ described_class.new(name) }
6
+ let(:redis){ PulseMeter.redis }
7
+
8
+ describe "#event" do
9
+ it "should set sensor value to passed value" do
10
+ expect{ sensor.event("foo" => 10.4) }.to change{ sensor.value["foo"] }.from(0).to(10.4)
11
+ expect{ sensor.event("foo" => 15.1) }.to change{ sensor.value["foo"] }.from(10.4).to(15.1)
12
+ end
13
+
14
+ it "should take multiple events" do
15
+ data = {"foo" => 1.1, "boo" => 2.2}
16
+ sensor.event(data)
17
+ sensor.value.should == data
18
+ end
19
+ end
20
+
21
+ describe "#value_key" do
22
+ it "should be composed of sensor name and pulse_meter:value: prefix" do
23
+ sensor.value_key.should == "pulse_meter:value:#{name}"
24
+ end
25
+ end
26
+
27
+ describe "#value" do
28
+ it "should have initial value 0" do
29
+ sensor.value["foo"].should == 0
30
+ end
31
+
32
+ it "should store redis hash by value_key" do
33
+ sensor.event({"foo" => 1})
34
+ sensor.value.should == {"foo" => 1}
35
+ redis.hgetall(sensor.value_key).should == {"foo" => "1.0"}
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Indicator do
4
+ let(:name){ :some_value }
5
+ let(:sensor){ described_class.new(name) }
6
+ let(:redis){ PulseMeter.redis }
7
+
8
+ describe "#event" do
9
+ it "should set sensor value to passed value" do
10
+ expect{ sensor.event(10.4) }.to change{ sensor.value }.from(0).to(10.4)
11
+ expect{ sensor.event(15.1) }.to change{ sensor.value }.from(10.4).to(15.1)
12
+ end
13
+ end
14
+
15
+ describe "#value_key" do
16
+ it "should be composed of sensor name and pulse_meter:value: prefix" do
17
+ sensor.value_key.should == "pulse_meter:value:#{name}"
18
+ end
19
+ end
20
+
21
+ describe "#value" do
22
+ it "should have initial value 0" do
23
+ sensor.value.should == 0
24
+ end
25
+
26
+ it "should store stringified value by value_key" do
27
+ sensor.event(123)
28
+ sensor.value.should == 123
29
+ redis.get(sensor.value_key) == '123'
30
+ end
31
+ end
32
+
33
+ describe "#cleanup" do
34
+ it "should remove all sensor data" do
35
+ sensor.annotate("My Indicator")
36
+ sensor.event(123)
37
+ sensor.cleanup
38
+ redis.keys('*').should be_empty
39
+ end
40
+ end
41
+
42
+ end
43
+
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Multi do
4
+ let(:name){ :foo }
5
+ let(:annotation) { "Multi sensor" }
6
+ let(:type) {'counter'}
7
+ let(:factors) {[:f1, :f2]}
8
+ let(:configuration) {
9
+ {
10
+ sensor_type: type,
11
+ args: {
12
+ annotation: annotation
13
+ }
14
+ }
15
+ }
16
+ let(:init_values) { { factors: factors, configuration: configuration } }
17
+ let!(:sensor) { described_class.new(name, init_values) }
18
+ let!(:redis){ PulseMeter.redis }
19
+
20
+ describe '#initialize' do
21
+ context "when factors are not corretly passed" do
22
+ it "raises ArgumentError" do
23
+ expect {described_class.new(name, {factors: :not_array, configuration: configuration})}.to raise_exception(ArgumentError)
24
+ expect {described_class.new(name, {configuration: configuration})}.to raise_exception(ArgumentError)
25
+ end
26
+ end
27
+
28
+ context "when configuration missing" do
29
+ it "raises ArgumentError" do
30
+ expect {described_class.new(name, {factors: factors})}.to raise_exception(ArgumentError)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#factors" do
36
+ it "returns factors passed to constructor" do
37
+ sensor.factors.should == factors
38
+ end
39
+ end
40
+
41
+ describe "#configuration_options" do
42
+ it "returns configuration option passed to constructor" do
43
+ sensor.configuration_options.should == configuration
44
+ end
45
+ end
46
+
47
+ describe "#sensors" do
48
+ it "returns PulseMeter::Sensor::Configuration instance" do
49
+ sensor.sensors.should be_instance_of(PulseMeter::Sensor::Configuration)
50
+ end
51
+ end
52
+
53
+ describe "#event" do
54
+
55
+ it "raises ArgumentError unless all factors' values given" do
56
+ expect {sensor.event({f1: :v1}, 1)}.to raise_exception(ArgumentError)
57
+ end
58
+
59
+
60
+ context "when sensors must be created" do
61
+ let(:factor_values) { {f1: :v1, f2: :v2} }
62
+
63
+ it "implicitly creates them" do
64
+ expect {sensor.event(factor_values, 1)}.to change{sensor.sensors.to_a.count}
65
+ end
66
+
67
+ it "assigns names based on factors' names and values" do
68
+ sensor.event(factor_values, 1)
69
+ names = sensor.sensors.to_a.map(&:name)
70
+ names.sort.should == [
71
+ "#{name}",
72
+ "#{name}_f1_v1",
73
+ "#{name}_f2_v2",
74
+ "#{name}_f1_v1_f2_v2"
75
+ ].sort
76
+ end
77
+
78
+ it "creates sensors of given type with configuration options passed" do
79
+ sensor.event(factor_values, 1)
80
+ sensor.sensors.each do |s|
81
+ s.should be_instance_of(PulseMeter::Sensor::Counter)
82
+ s.annotation.should == annotation
83
+ end
84
+ end
85
+ end
86
+
87
+ it "sends event to all combinations of factors and values" do
88
+ sensor.event({f1: :f1v1, f2: :f2v1}, 1)
89
+ sensor.event({f1: :f1v2, f2: :f2v1}, 2)
90
+ [
91
+ ["#{name}", 3],
92
+ ["#{name}_f1_f1v1", 1],
93
+ ["#{name}_f1_f1v2", 2],
94
+ ["#{name}_f2_f2v1", 3],
95
+ ["#{name}_f1_f1v1_f2_f2v1", 1],
96
+ ["#{name}_f1_f1v2_f2_f2v1", 2]
97
+ ].each do |sensor_name, sum|
98
+ sensor.sensor(sensor_name) { |s|
99
+ s.value.should == sum
100
+ }
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "#each" do
106
+ it "when used by Enumerable it lists all ever created subsensors of multisensor" do
107
+ sensor.event({f1: :f1v1, f2: :f2v1}, 1)
108
+ restored_sensor = PulseMeter::Sensor::Base.restore(name)
109
+
110
+ restored_sensor.to_a.map(&:name).sort.should == [
111
+ "#{name}",
112
+ "#{name}_f1_f1v1",
113
+ "#{name}_f2_f2v1",
114
+ "#{name}_f1_f1v1_f2_f2v1",
115
+ ].sort
116
+ end
117
+ end
118
+
119
+ describe "#sensor_for_factors" do
120
+ context "when sensor has already been created" do
121
+ it "yields block with sensor for given combination of factors and their values" do
122
+ sensor.event({f1: :f1v1, f2: :f2v1}, 1)
123
+ sensor.sensor_for_factors([:f1, :f2], [:f1v1, :f2v1]){|s| s.name.should == "#{name}_f1_f1v1_f2_f2v1"}
124
+ sensor.sensor_for_factors([:f1], [:f1v1]){|s| s.name.should == "#{name}_f1_f1v1"}
125
+ end
126
+ end
127
+
128
+ context "when such a sensor was not created" do
129
+ it "does not yields block" do
130
+ yielded = false
131
+ sensor.sensor_for_factors([:foo], [:bar]){ yielded = true }
132
+ yielded.should be_false
133
+ end
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Timeline do
4
+ let(:name){ :some_value_with_history }
5
+ let(:ttl){ 100 }
6
+ let(:raw_data_ttl){ 10 }
7
+ let(:interval){ 5 }
8
+ let(:reduce_delay){ 3 }
9
+ let(:good_init_values){ {:ttl => ttl, :raw_data_ttl => raw_data_ttl, :interval => interval, :reduce_delay => reduce_delay} }
10
+ let(:sensor){ described_class.new(name, good_init_values) }
11
+ let(:redis){ PulseMeter.redis }
12
+
13
+ it_should_behave_like "timeline sensor"
14
+
15
+ describe '#new' do
16
+ INIT_VALUE_NAMES = {
17
+ :with_defaults => [:raw_data_ttl, :reduce_delay],
18
+ :without_defaults => [:ttl, :interval]
19
+ }
20
+
21
+ shared_examples_for "error raiser" do |value_names, bad_values|
22
+ value_names.each do |value|
23
+ bad_values.each do |bad_value|
24
+ it "should raise exception if a bad value #{bad_value.inspect} passed for #{value.inspect}" do
25
+ expect{ described_class.new(name, good_init_values.merge(value => bad_value)) }.to raise_exception(ArgumentError)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ it "should initialize #ttl #raw_data_ttl #interval and #name attributes" do
32
+ sensor.name.should == name.to_s
33
+
34
+ sensor.ttl.should == ttl
35
+ sensor.raw_data_ttl.should == raw_data_ttl
36
+ sensor.interval.should == interval
37
+ end
38
+
39
+ it_should_behave_like "error raiser", INIT_VALUE_NAMES[:without_defaults], [:bad, -1, nil]
40
+ it_should_behave_like "error raiser", INIT_VALUE_NAMES[:with_defaults], [:bad, -1]
41
+
42
+ INIT_VALUE_NAMES[:with_defaults].each do |value|
43
+ it "should not raise exception if #{value.inspect} is not defined" do
44
+ values = good_init_values
45
+ values.delete(value)
46
+ expect {described_class.new(name, good_init_values)}.not_to raise_exception(ArgumentError)
47
+ end
48
+
49
+ it "should assign default value to #{value.inspect} if it is not defined" do
50
+ values = good_init_values
51
+ values.delete(value)
52
+ obj = described_class.new(name, good_init_values)
53
+ obj.send(value).should be_kind_of(Fixnum)
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+ describe "#deflate_safe" do
60
+ class GoodSubclass < described_class
61
+ def deflate(value)
62
+ value.to_i
63
+ end
64
+ end
65
+
66
+ class BadSubclass < described_class
67
+ def deflate(value)
68
+ raise "Any conversion error"
69
+ end
70
+ end
71
+
72
+ let!(:good_instance) { GoodSubclass.new("good", good_init_values) }
73
+ let!(:bad_instance) { BadSubclass.new("bad", good_init_values) }
74
+
75
+ it "preserves nil values" do
76
+ good_instance.deflate_safe(nil).should be_nil
77
+ end
78
+
79
+ it "converts value as defined in subclass" do
80
+ good_instance.deflate_safe("10").should == 10
81
+ end
82
+
83
+ it "returns nil if conversion fails" do
84
+ bad_instance.deflate_safe(:foo).should be_nil
85
+ end
86
+ end
87
+
88
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Timelined::Average do
4
+ it_should_behave_like "timeline sensor"
5
+ it_should_behave_like "timelined subclass", [1, 2], 1.5
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Timelined::Counter do
4
+ it_should_behave_like "timeline sensor"
5
+ it_should_behave_like "timelined subclass", [1, 2], 3
6
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Timelined::HashedCounter do
4
+ it_should_behave_like "timeline sensor", {}, {:foo => 1}
5
+ it_should_behave_like "timelined subclass", [{:foo => 1}, {:foo => 2}], {:foo => 3, :total => 3}.to_json
6
+ it_should_behave_like "timelined subclass", [{:foo => 1}, {:foo => :bad_value}], {:foo => 1, :total => 1}.to_json
7
+ it_should_behave_like "timelined subclass", [{:foo => 1}, {:boo => 2}], {:foo => 1, :boo => 2, :total => 3}.to_json
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe PulseMeter::Sensor::Timelined::HashedIndicator do
4
+ it_should_behave_like "timeline sensor", {}, {:foo => 1}
5
+ it_should_behave_like "timelined subclass", [{:foo => 1}, {:foo => 2}], {:foo => 2}.to_json
6
+ it_should_behave_like "timelined subclass", [{:foo => 1}, {:foo => :bad_value}], {:foo => 1}.to_json
7
+ it_should_behave_like "timelined subclass", [{:foo => 1}, {:boo => 2}], {:foo => 1, :boo => 2}.to_json
8
+ end