Skip to content
Browse files

Fozzie gem for Rails application metrics.

  • Loading branch information...
0 parents commit 214df0e87c5b14e4b94b27e6e8cf47543b9820a7 @marckysharky marckysharky committed Dec 10, 2011
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
1 .rvmrc
@@ -0,0 +1 @@
+rvm use --create 1.8.7@fozzie
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in fozzie.gemspec
+gemspec
1 Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
24 fozzie.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "fozzie/version"
+
+Gem::Specification.new do |s|
+ s.name = "fozzie"
+ s.version = Fozzie::VERSION
+ s.authors = ["Marc Watts"]
+ s.email = ["marcw.watts@loneplanet.com"]
+ s.homepage = "www.lonelyplanet.com"
+ s.summary = %q{Statistics gem for LonelyPlanet Online}
+ s.description = %q{Gem allows statistics gathering from Ruby and Ruby
+ on Rails applications within LonelyPlanet Online}
+
+ s.rubyforge_project = "fozzie"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "mocha"
+end
17 lib/fozzie.rb
@@ -0,0 +1,17 @@
+require "fozzie/configuration"
+require "fozzie/connection"
+require "fozzie/methods"
+require "fozzie/version"
+
+module Fozzie
+ extend Fozzie::Methods
+
+ def self.c
+ config
+ end
+
+ def self.config
+ @config ||= Configuration.new
+ end
+
+end
22 lib/fozzie/configuration.rb
@@ -0,0 +1,22 @@
+require 'ostruct'
+
+module Fozzie
+
+ class Configuration < OpenStruct
+
+ def initialize(args = {})
+ super self.class.default_configuration.merge(args)
+ end
+
+ private
+
+ def self.default_configuration
+ {
+ :host => '127.0.0.1',
+ :port => 8081
+ }.dup
+ end
+
+ end
+
+end
11 lib/fozzie/connection.rb
@@ -0,0 +1,11 @@
+module Fozzie
+ class Connection
+
+ def self.send_data(data)
+ TCPSocket.open(Fozzie.c.host, Fozzie.c.port) do |sock|
+ sock.puts(data)
+ end rescue nil
+ end
+
+ end
+end
42 lib/fozzie/methods.rb
@@ -0,0 +1,42 @@
+require 'fozzie/connection'
+
+module Fozzie
+ module Methods
+
+ def count(bucket, value, extra = nil)
+ register(bucket, value, :c, extra)
+ end
+
+ def timer(bucket, value, unit, extra = nil)
+ register(bucket, value, unit.to_sym, extra)
+ end
+
+ def sample(bucket, value, type, extra)
+ case type.to_s
+ when 'count' || 'c'
+ extra = "@#{extra}" unless extra.nil?
+ count(bucket, value, extra)
+ else
+ raise NotImplementedError
+ end
+ end
+
+ private
+
+ def register(bucket, value, type, extra = nil)
+ send_data(create_key(bucket, value, type, extra))
+ self
+ end
+
+ def send_data(data)
+ Connection.send_data data
+ end
+
+ def create_key(bucket, value, type, extra)
+ arr = [bucket, ":", value, "|", type]
+ arr.push("|", extra) unless extra.nil?
+ arr.join
+ end
+
+ end
+end
3 lib/fozzie/version.rb
@@ -0,0 +1,3 @@
+module Fozzie
+ VERSION = "0.0.1"
+end
13 spec/lib/fozzie/configuration_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe Fozzie::Configuration do
+
+ it "#host" do
+ subject.host.should be_kind_of(String)
+ end
+
+ it "#port" do
+ subject.port.should be_kind_of(Fixnum)
+ end
+
+end
13 spec/lib/fozzie/connection_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe Fozzie::Connection do
+
+ it { Fozzie::Connection.should respond_to(:send_data) }
+
+ it "#send_data" do
+ proc {
+ Fozzie::Connection.send_data('blablabla')
+ }.should_not raise_error
+ end
+
+end
10 spec/lib/fozzie/version_spec.rb
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+describe "Fozzie Version" do
+
+ it "exists" do
+ Fozzie::VERSION.should be_kind_of(String)
+ Fozzie::VERSION.should match(/\d{1,3}?\.?/)
+ end
+
+end
31 spec/lib/fozzie_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Fozzie do
+
+ it { should respond_to(:count) }
+ it { should respond_to(:timer) }
+ it { should respond_to(:sample) }
+ it { should respond_to(:c) }
+ it { should respond_to(:config) }
+
+ it "has configuration" do
+ Fozzie.config.should be_kind_of(Fozzie::Configuration)
+ Fozzie.c.should be_kind_of(Fozzie::Configuration)
+ end
+
+ it "registers count" do
+ Fozzie::Connection.expects(:send_data).with('test.bucket:1|c')
+ Fozzie.count('test.bucket', 1)
+ end
+
+ it "registers timing" do
+ Fozzie::Connection.expects(:send_data).with('test.bucket:320|ms')
+ Fozzie.timer('test.bucket', 320, 'ms')
+ end
+
+ it "registers sampling" do
+ Fozzie::Connection.expects(:send_data).with('test.bucket:1|c|@0.1')
+ Fozzie.sample('test.bucket', 1, :count, 0.1)
+ end
+
+end
9 spec/spec_helper.rb
@@ -0,0 +1,9 @@
+root = File.expand_path('../', File.dirname(__FILE__))
+lib = File.expand_path('lib', root)
+$:.unshift(root, lib)
+
+require 'fozzie'
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end

0 comments on commit 214df0e

Please sign in to comment.
Something went wrong with that request. Please try again.