Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes on pelletier:master #2

Merged
merged 5 commits into from Jun 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
25 changes: 25 additions & 0 deletions 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!
6 changes: 6 additions & 0 deletions README.rdoc
Expand Up @@ -16,6 +16,12 @@ To push data to Graphite:
graphite.puts "your.metric.name 3.1415926 #{g.time_now}" graphite.puts "your.metric.name 3.1415926 #{g.time_now}"
end end


Or:
g.send_metrics({
'foo.bar1' => 250,
'foo.bar2' => 114
})

== Note on Patches/Pull Requests == Note on Patches/Pull Requests


* Fork the project. * Fork the project.
Expand Down
14 changes: 13 additions & 1 deletion lib/simple-graphite.rb
@@ -1,19 +1,31 @@
require 'socket' require 'socket'



class Graphite class Graphite

attr_accessor :host, :port, :time attr_accessor :host, :port, :time

def initialize(options = {}) def initialize(options = {})
@host = options[:host] @host = options[:host]
@port = options[:port] @port = options[:port] || 2003
@time = Time.now.to_i @time = Time.now.to_i
end end


def push_to_graphite 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) socket = TCPSocket.new(@host, @port)
yield socket yield socket
socket.close socket.close
end 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 def hostname
Socket.gethostname Socket.gethostname
end end
Expand Down
11 changes: 7 additions & 4 deletions simple-graphite.gemspec
Expand Up @@ -18,19 +18,22 @@ Gem::Specification.new do |s|
] ]
s.files = [ s.files = [
"lib/simple-graphite.rb", "lib/simple-graphite.rb",
"test/simple-graphite_test.rb", "spec/simple-graphite_spec.rb",
"test/test_helper.rb" "spec/spec_helper.rb"
] ]
s.homepage = %q{http://github.com/imeyer/simple-graphite} s.homepage = %q{http://github.com/imeyer/simple-graphite}
s.rdoc_options = ["--charset=UTF-8"] s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7} s.rubygems_version = %q{1.3.7}
s.summary = %q{Simple hook into graphite} s.summary = %q{Simple hook into graphite}
s.test_files = [ s.test_files = [
"test/test_helper.rb", "spec/spec_helper.rb",
"test/simple-graphite_test.rb" "spec/simple-graphite_spec.rb"
] ]


s.add_development_dependency "rspec"
s.add_development_dependency "rspec-mocks"

if s.respond_to? :specification_version then if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3 s.specification_version = 3
Expand Down
43 changes: 43 additions & 0 deletions 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
51 changes: 51 additions & 0 deletions 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
7 changes: 0 additions & 7 deletions test/simple-graphite_test.rb

This file was deleted.

10 changes: 0 additions & 10 deletions test/test_helper.rb

This file was deleted.