Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for derivative metrics #22

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/metriks.rb
@@ -1,4 +1,3 @@

module Metriks
VERSION = '0.9.9.1'

Expand All @@ -22,6 +21,10 @@ def self.meter(name)
Metriks::Registry.default.meter(name)
end

def self.derive(name)
Metriks::Registry.default.derive(name)
end

def self.histogram(name)
Metriks::Registry.default.histogram(name)
end
Expand Down
13 changes: 13 additions & 0 deletions lib/metriks/derive.rb
@@ -0,0 +1,13 @@
require 'atomic'

require 'metriks/meter'

module Metriks
class Derive < Metriks::Meter
def mark(val = 1)
@last ||= Atomic.new(val)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be initialized in the constructor to be threadsafe.

last = @last.swap(val)
super(last > val ? val : val - last)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem with doing this simplistically is that the rate calculations, etc. are all updated every 5 seconds. If a counter is polled (and mark() is called) less frequently than 5 seconds, the rates will be recorded as all happening right when mark() was called instead of something more reasonable (like linearly from the last time it was polled and now).

I'll have to dig into the underlying classes more to see if there's a reasonable way to do this sort of thing, but it may be that a meter isn't the right thing to be using as an underlying implementation for a derived counter.

end
end
end
18 changes: 17 additions & 1 deletion lib/metriks/registry.rb
Expand Up @@ -2,6 +2,7 @@
require 'metriks/timer'
require 'metriks/utilization_timer'
require 'metriks/meter'
require 'metriks/derive'

module Metriks
# Public: A collection of metrics
Expand Down Expand Up @@ -86,6 +87,21 @@ def meter(name)
add_or_get(name, Metriks::Meter)
end

# Public: Fetch or create a new derivative metric. Derivatives are a meter
# that tracks throughput by calculating the derivative to the previous
# value.
#
# name - The String name of the metric to define or fetch
#
# Examples
#
# registry.derive('network.bytes')
#
# Returns the Metricks::Derive identified by the name.
def derive(name)
add_or_get(name, Metriks::Derive)
end

# Public: Fetch or create a new timer metric. Timers provide the means to
# time the execution of a method including statistics on the number of
# invocations, average length of time, throughput.
Expand Down Expand Up @@ -188,4 +204,4 @@ def add_or_get(name, klass, &create_metric)
end
end
end
end
end
32 changes: 32 additions & 0 deletions test/derive_test.rb
@@ -0,0 +1,32 @@
require 'test_helper'

require 'metriks/derive'

class DeriveTest < Test::Unit::TestCase
include ThreadHelper

def setup
@meter = Metriks::Derive.new
end

def teardown
@meter.stop
end

def test_meter
@meter.mark(100)
@meter.mark(150)

assert_equal 50, @meter.count
end

def test_one_minute_rate
@meter.mark(1000)
@meter.mark(2000)

# Pretend it's been 5 seconds
@meter.tick

assert_equal 200, @meter.one_minute_rate
end
end