Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Changes on pelletier:master #2

Merged
merged 5 commits into from

2 participants

@pelletier

Hi,

Here is the pull request you asked for (here).

Cheers,
Thomas

@imeyer
Owner

Hey Thomas,

I'd love to get these changes into the gem.. send me a PR with your changes and I'll merge them!

  • Ian
@imeyer
Owner

Thanks a lot!! :D

@imeyer imeyer merged commit 33b0163 into imeyer:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
3  Gemfile
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gemspec
View
25 Gemfile.lock
@@ -0,0 +1,25 @@
+PATH
+ remote: .
+ specs:
+ simple-graphite (1.1.1)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ diff-lcs (1.1.3)
+ rspec (2.10.0)
+ rspec-core (~> 2.10.0)
+ rspec-expectations (~> 2.10.0)
+ rspec-mocks (~> 2.10.0)
+ rspec-core (2.10.0)
+ rspec-expectations (2.10.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.10.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rspec
+ rspec-mocks
+ simple-graphite!
View
6 README.rdoc
@@ -16,6 +16,12 @@ To push data to Graphite:
graphite.puts "your.metric.name 3.1415926 #{g.time_now}"
end
+Or:
+ g.send_metrics({
+ 'foo.bar1' => 250,
+ 'foo.bar2' => 114
+ })
+
== Note on Patches/Pull Requests
* Fork the project.
View
14 lib/simple-graphite.rb
@@ -1,19 +1,31 @@
require 'socket'
+
class Graphite
+
attr_accessor :host, :port, :time
+
def initialize(options = {})
@host = options[:host]
- @port = options[:port]
+ @port = options[:port] || 2003
@time = Time.now.to_i
end
def push_to_graphite
+ raise "You need to provide both the hostname and the port" if @host.nil? || @port.nil?
socket = TCPSocket.new(@host, @port)
yield socket
socket.close
end
+ def send_metrics(metrics_hash)
+ current_time = time_now
+ push_to_graphite do |graphite|
+ graphite.puts((metrics_hash.map { |k,v| [k, v, current_time].join(' ') + "\n" }).join(''))
+ end
+ current_time
+ end
+
def hostname
Socket.gethostname
end
View
11 simple-graphite.gemspec
@@ -18,8 +18,8 @@ Gem::Specification.new do |s|
]
s.files = [
"lib/simple-graphite.rb",
- "test/simple-graphite_test.rb",
- "test/test_helper.rb"
+ "spec/simple-graphite_spec.rb",
+ "spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/imeyer/simple-graphite}
s.rdoc_options = ["--charset=UTF-8"]
@@ -27,10 +27,13 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.7}
s.summary = %q{Simple hook into graphite}
s.test_files = [
- "test/test_helper.rb",
- "test/simple-graphite_test.rb"
+ "spec/spec_helper.rb",
+ "spec/simple-graphite_spec.rb"
]
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "rspec-mocks"
+
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
View
43 spec/simple-graphite_spec.rb
@@ -0,0 +1,43 @@
+require 'spec_helper'
+require 'simple-graphite'
+
+
+describe Graphite do
+
+ it "has a default port" do
+ a = Graphite.new
+ a.port.should == 2003
+ end
+
+ it "needs to have the hostname and the port" do
+ a = Graphite.new
+ lambda {a.push_to_graphite}.should raise_error(RuntimeError, "You need to provide both the hostname and the port")
+ end
+
+ it "returns time" do
+ a = Graphite.new
+ a.time_now.should == Time.now.to_i
+ end
+
+ it "has pushes row messages to graphite without restriction" do
+ ftcp = mock_tcp()
+ a = Graphite.new(:host => 'localhost')
+ a.push_to_graphite do |graphite|
+ graphite.puts "hello world"
+ end
+
+ ftcp.buffer.should == "hello world"
+ end
+
+ it "sends metrics from an hash" do
+ ftcp = mock_tcp()
+ a = Graphite.new(:host => 'localhost')
+
+ time = a.send_metrics({
+ 'foo.bar' => 200,
+ 'foo.test' => 10.2
+ })
+ ftcp.buffer.should == "foo.bar 200 #{time}\nfoo.test 10.2 #{time}\n"
+ end
+
+end
View
51 spec/spec_helper.rb
@@ -0,0 +1,51 @@
+require 'socket'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+end
+
+TCP_NEW = TCPSocket.method(:new) unless defined? TCP_NEW
+
+class FakeTCPSocket
+ attr :buffer, true
+
+ def initialize
+ @buffer = ""
+ end
+
+ def readline
+ end
+
+ def flush
+ end
+
+ def write(some_text)
+ @buffer += some_text
+ end
+
+ def readchar
+ end
+
+ def read(num)
+ end
+
+ def close
+ end
+
+ def puts(some_text)
+ @buffer += some_text
+ end
+
+end
+
+def mock_tcp()
+ ftcp = FakeTCPSocket.new
+ TCPSocket.stub!(:new).and_return { ftcp }
+ ftcp
+end
+
+def unmock_tcp
+ TCKSocket.stub!(:new).and_return {TCP_NEW.call}
+end
View
7 test/simple-graphite_test.rb
@@ -1,7 +0,0 @@
-require 'test_helper'
-
-class EtsyGraphiteTest < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
- end
-end
View
10 test/test_helper.rb
@@ -1,10 +0,0 @@
-require 'rubygems'
-require 'test/unit'
-require 'shoulda'
-
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'simple-graphite'
-
-class Test::Unit::TestCase
-end
Something went wrong with that request. Please try again.