counters
counters copied to clipboard
Easily record any metric in your system
h1. Counters
Easily record any metrics from anywhere within your code, using a very simple interface:
ping: When's the last time we saw this thing?hit: Increments a countermagnitude: Measures numerical valueslatency: Measures time intervals
h2. Example
Let's say you have a web crawler. There are a ton of things you can measure about your crawler: how many pages it processed (#hit), how many bytes you read (#magnitude), how long did it take to download the page (#latency), how long did it take to parse the raw HTML to a useable format (#latency).
require "uri"
require "open-uri"
require "counters"
require "nokogiri"
Counter = Counters::Redis.new(Redis.new, :namespace => "crawler", :base_key => "counters")
urls_to_crawl = ["http://blog.teksol.info/", "http://techcrunch.com/", "http://www.google.com/"]
while url = urls_to_crawl.pop
Counter.ping "crawler"
begin
Counter.hit "urls_popped"
puts "Fetching #{url}"
raw_html = Counter.latency "download" do
URI.parse(url).read
end
Counter.magnitude "bytes_in", raw_html.length
parsed_html = Counter.latency "html_parsing" do
Nokogiri::HTML(raw_html)
end
rescue
Counter.hit "error"
end
end
h2. Other Backends
For testing purposes, there also exists a Counters::Memory. This would be good in test mode, for example. The counters are exposed through accessor methods returning a Hash.
You may log to a file, but be advised the file's size grows very quickly. Counters are stored in the file, one per line, in an easily readable format.
$ irb -r counters
> Counter = Counters::File.new("counters.log")
=> #<:file:0x00000101a18f18>, @formatter=#<0x00000101a18bd0><:logdevice:0x00000101a18e28><:logdevice::logdevicemutex:0x00000101a18e00>0x00000101a18bd0>""
""