Permalink
Browse files

Version 1.0.0

  • Loading branch information...
0 parents commit 712b6864cedffc269e45b0f4e4897ca1105a2438 @jeyb committed Jan 7, 2013
Showing with 217 additions and 0 deletions.
  1. +25 −0 .gitignore
  2. +2 −0 .rspec
  3. +3 −0 Gemfile
  4. +35 −0 Gemfile.lock
  5. +20 −0 LICENSE
  6. +28 −0 README.md
  7. +25 −0 Rakefile
  8. +24 −0 graphite-ruby.gemspec
  9. +29 −0 lib/graphite.rb
  10. +19 −0 spec/lib/graphite_spec.rb
  11. +7 −0 spec/spec_helper.rb
25 .gitignore
@@ -0,0 +1,25 @@
+# Ignore bundler config.
+/.bundle
+
+# Ignore all logfiles and tempfiles.
+/log/*.log
+/tmp
+
+# OS generated files.
+.DS_Store*
+ehthumbs.db
+Icon?
+Thumbs.db
+
+# VIM.
+.*.sw[a-z]
+*.un~
+Session.vim
+
+# Yard
+.yardoc
+_yardoc
+doc/
+
+# Custom
+spec/support/log
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--debug
3 Gemfile
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
35 Gemfile.lock
@@ -0,0 +1,35 @@
+PATH
+ remote: .
+ specs:
+ graphite-ruby (1.0.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ columnize (0.3.6)
+ debugger (1.2.3)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.1.1)
+ debugger-ruby_core_source (~> 1.1.5)
+ debugger-linecache (1.1.2)
+ debugger-ruby_core_source (>= 1.1.1)
+ debugger-ruby_core_source (1.1.6)
+ diff-lcs (1.1.3)
+ rake (10.0.3)
+ rspec (2.12.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
+ rspec-core (2.12.0)
+ rspec-expectations (2.12.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ debugger (~> 1.2.2)
+ graphite-ruby!
+ rake
+ rspec (~> 2.12.0)
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Jey Balachandran
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 README.md
@@ -0,0 +1,28 @@
+graphite-ruby
+=============
+Sends measurements to [Hosted Graphite](http://hostedgraphite.com/) via UDP.
+
+Usage
+-----
+```ruby
+$graphite = Graphite.new(ENV["GRAPHITE_TOKEN"], "carbon.hostedgraphite.com")
+
+$graphite.measure "stats.name", 123
+$graphite.measure "stats.another_name" do
+ sleep 1
+end
+```
+
+Contributing to graphite-ruby
+-----------------------------
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
+* Fork the project.
+* Start a feature/bugfix branch.
+* Commit and push until you are happy with your contribution.
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+
+Copyright
+---------
+Copyright (c) 2013 Jey Balachandran. See LICENSE for further details.
25 Rakefile
@@ -0,0 +1,25 @@
+# encoding: utf-8
+
+require "rubygems"
+require "bundler/setup"
+require "rake"
+
+begin
+ Bundler.setup(:default, :development)
+rescue Bundler::BundlerError => exception
+ $stderr.puts exception.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit exception.status_code
+end
+
+require "rspec/core"
+require "rspec/core/rake_task"
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = FileList["spec/**/*_spec.rb"]
+end
+
+RSpec::Core::RakeTask.new(:rcov) do |spec|
+ spec.pattern = "spec/**/*_spec.rb"
+end
+
+task default: :spec
24 graphite-ruby.gemspec
@@ -0,0 +1,24 @@
+# encoding: utf-8
+
+lib = File.expand_path("../lib", __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+
+Gem::Specification.new do |gem|
+ gem.name = "graphite-ruby"
+ gem.version = "1.0.0"
+ gem.authors = ["Jey Balachandran"]
+ gem.email = ["jey.balachandran@gmail.com"]
+ gem.summary = %q{Sends measurements to Graphite via UDP}
+ gem.description = %q{Sends measurements to Graphite via UDP}
+ gem.homepage = "http://github.com/jeyb/graphite-ruby"
+ gem.license = "MIT"
+ gem.files = `git ls-files`.split($/)
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+
+ gem.required_ruby_version = ">= 1.9.2"
+
+ gem.add_development_dependency "debugger", "~> 1.2.2"
+ gem.add_development_dependency "rake"
+ gem.add_development_dependency "rspec", "~> 2.12.0"
+end
29 lib/graphite.rb
@@ -0,0 +1,29 @@
+require "socket"
+
+class Graphite
+ def initialize(token, host, port = 2003)
+ @token, @host, @port = token, host, port
+ end
+
+ def measure(stat, value = nil)
+ if block_given?
+ start = Time.now
+ yield
+ value = ((Time.now - start) * 1000).round
+ end
+
+ send_to_socket("#{stat} #{value}") unless value.nil?
+ end
+
+ private
+
+ def send_to_socket(message)
+ return if defined?(Rails) && !Rails.env.production?
+
+ socket.send("#{@token}.#{message}", 0, @host, @port) rescue nil
+ end
+
+ def socket
+ @socket ||= UDPSocket.new
+ end
+end
19 spec/lib/graphite_spec.rb
@@ -0,0 +1,19 @@
+require "spec_helper"
+
+describe Graphite do
+ let(:client) { Graphite.new("12345", "example.com") }
+
+ subject { Honeypot::Client.new(configuration) }
+
+ it "measures a value" do
+ client.should_receive(:send_to_socket).with("stat 123")
+ client.measure("stat", 123)
+ end
+
+ it "measures a block execution time" do
+ client.should_receive(:send_to_socket).with(/stat 1\d{3}/)
+ client.measure "stat" do
+ sleep 1
+ end
+ end
+end
7 spec/spec_helper.rb
@@ -0,0 +1,7 @@
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require "graphite"
+
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

0 comments on commit 712b686

Please sign in to comment.