#378 FnordMetric
Sep 04, 2012 | 10 minutes | Tools
FnordMetric allows you to chart events in real time. This is great for keeping track of user activity in your Rails app as demonstrated in this episode.
Resources
terminal
brew install redis redis-server /usr/local/etc/redis.conf gem install fnordmetric ruby full_example.rb
fnordmetric_app.rb
require "fnordmetric" FnordMetric.namespace :store do hide_active_users toplist_gauge :popular_products, title: "Popular Products" distribution_gauge :popular_prices, title: "Popular Prices", value_ranges: [0..5, 5..10, 10..20, 20..50, 50..10000] gauge :product_views_per_second, tick: 1.second widget "Product Views", title: "Views per Second", type: :timeline, width: 100, gauges: :product_views_per_second, include_current: true, autoupdate: 1 event :view_product do observe :popular_products, data[:name] observe :popular_prices, data[:price] incr :product_views_per_second end end FnordMetric::Web.new(port: 4242) FnordMetric::Worker.new FnordMetric.run
config/intializers/fnordmetric.rb
FNORD_METRIC = FnordMetric::API.new
models/product.rb
def trigger_view_event FNORD_METRIC.event(attributes.merge(_type: :view_product)) end
lib/tasks/product.rake
namespace :fnordmetric do desc "Populate FnordMetric with events to simulate user activity" task :populate => :environment do products = Product.all loop do products.sample.trigger_view_event sleep(rand) end end end
loading