cabin 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cabin/metrics.rb CHANGED
@@ -3,8 +3,7 @@ require "cabin/metrics/gauge"
3
3
  require "cabin/metrics/meter"
4
4
  require "cabin/metrics/counter"
5
5
  require "cabin/metrics/timer"
6
-
7
-
6
+ require "cabin/metrics/histogram"
8
7
 
9
8
  # What type of metrics do we want?
10
9
  #
@@ -31,8 +30,7 @@ require "cabin/metrics/timer"
31
30
  # like meter, but takes values more than simply '1'
32
31
  # as a result, exposes percentiles, median, etc.
33
32
  # timers
34
- # combination of meter + histogram
35
- # meter for invocations, histogram for duration
33
+ # a time-observing interface on top of histogram.
36
34
  #
37
35
  # With the exception of gauges, all the other metrics are all active/pushed.
38
36
  # Gauges take callbacks, so their values are pulled, not pushed. The active
@@ -0,0 +1,57 @@
1
+ require "cabin/namespace"
2
+ require "thread"
3
+
4
+ class Cabin::Metrics::Histogram
5
+ # A new Histogram.
6
+ public
7
+ def initialize
8
+ @lock = Mutex.new
9
+
10
+ # Histogram should track many things, including:
11
+ # - percentiles (50, 75, 90, 95, 99?)
12
+ # - median
13
+ # - max
14
+ # - min
15
+ # - total sum
16
+ #
17
+ # Sliding values of all of these?
18
+ @total = 0
19
+ @min = 0
20
+ @max = 0
21
+ @count = 0
22
+ @mean = 0.0
23
+ end # def initialize
24
+
25
+ public
26
+ def record(value)
27
+ @lock.synchronize do
28
+ @count += 1
29
+ @total += value
30
+ @min = value if value < @min
31
+ @max = value if value > @max
32
+ @mean = @total / @count
33
+ # TODO(sissel): median
34
+ # TODO(sissel): percentiles
35
+ end
36
+ end # def record
37
+
38
+ # This is a very poor way to access the metric data.
39
+ # TODO(sissel): Need to figure out a better interface.
40
+ public
41
+ def value
42
+ return @lock.synchronize { @count }
43
+ end # def value
44
+
45
+ public
46
+ def to_hash
47
+ return @lock.synchronize do
48
+ {
49
+ :count => @count,
50
+ :total => @total,
51
+ :min => @min,
52
+ :max => @max,
53
+ :mean => @mean,
54
+ }
55
+ end
56
+ end # def to_hash
57
+ end # class Cabin::Metrics::Histogram
@@ -1,19 +1,8 @@
1
1
  require "cabin/namespace"
2
+ require "cabin/metrics/histogram"
2
3
  require "thread"
3
4
 
4
- class Cabin::Metrics::Timer
5
- # A new Timer metric
6
- #
7
- # Timers behave like a combination of Meter and Histogram. Every Timer
8
- # invocation is metered and the duration of the timer is put into the
9
- # Histogram.
10
- public
11
- def initialize
12
- @invocations = 0
13
- @total_duration = 0.0
14
- @lock = Mutex.new
15
- end # def initialize
16
-
5
+ class Cabin::Metrics::Timer < Cabin::Metrics::Histogram
17
6
  # Start timing something.
18
7
  #
19
8
  # If no block is given
@@ -34,37 +23,6 @@ class Cabin::Metrics::Timer
34
23
  record(Time.now - start)
35
24
  end # def time_block
36
25
 
37
- public
38
- def record(duration)
39
- # TODO(sissel): histogram the duration
40
- @lock.synchronize do
41
- @invocations += 1
42
- @total_duration += duration
43
- end
44
- end # def record
45
-
46
- # Get the number of times this timer has been used
47
- public
48
- def count
49
- return @lock.synchronize { @invocations }
50
- end # def value
51
-
52
- # TODO(sissel): Until I find a good API.
53
- public
54
- def value
55
- return count
56
- end # def value
57
-
58
- public
59
- def to_hash
60
- return @lock.synchronize do
61
- {
62
- :count => @invocations,
63
- :duration_sum => @total_duration
64
- }
65
- end
66
- end # def to_hash
67
-
68
26
  class TimerContext
69
27
  public
70
28
  def initialize(&stop_callback)
@@ -78,4 +36,4 @@ class Cabin::Metrics::Timer
78
36
  @callback.call(duration)
79
37
  end # def stop
80
38
  end # class TimerContext
81
- end # class Cabin::Metrics::Counter
39
+ end # class Cabin::Metrics::Timer
data/test/test_metrics.rb CHANGED
@@ -47,9 +47,12 @@ describe Cabin::Metrics do
47
47
  test "timer counter" do
48
48
  timer = @metrics.timer(self)
49
49
  30.times do |i|
50
- assert_equal(i, timer.count)
50
+ assert_equal(i, timer.value)
51
51
  assert_equal(i, timer.to_hash[:count])
52
52
  timer.time { true }
53
+ assert(timer.to_hash[:total] > 0, "total should be nonzero")
54
+ assert(timer.to_hash[:mean] > 0, "mean should be nonzero")
55
+ assert(timer.to_hash[:max] > 0, "max should be nonzero")
53
56
  end
54
57
  end
55
58
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cabin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &20938580 !ruby/object:Gem::Requirement
16
+ requirement: &9557240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *20938580
24
+ version_requirements: *9557240
25
25
  description: This is an experiment to try and make logging more flexible and more
26
26
  consumable. Plain text logs are bullshit, let's emit structured and contextual logs.
27
27
  Metrics, too!
@@ -40,6 +40,7 @@ files:
40
40
  - lib/cabin/metrics/counter.rb
41
41
  - lib/cabin/metrics/timer.rb
42
42
  - lib/cabin/metrics/gauge.rb
43
+ - lib/cabin/metrics/histogram.rb
43
44
  - lib/cabin/metrics/meter.rb
44
45
  - lib/cabin/timer.rb
45
46
  - lib/cabin/metrics.rb