em-statsd 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +11 -0
- data/em-statsd.gemspec +22 -0
- data/lib/em-statsd/version.rb +5 -0
- data/lib/em/statsd.rb +34 -0
- data/spec/helper.rb +7 -0
- data/spec/statsd_spec.rb +78 -0
- metadata +105 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Tim Snowhite
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# EM::Statsd
|
2
|
+
|
3
|
+
A drop-in extension/replacement to the statsd-ruby gem.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'em-statsd'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install em-statsd
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Replace all occurances of the Statsd constant with
|
22
|
+
EM::Statsd in your app, inside an EM::run block.
|
23
|
+
It all works through the magic of adapter objects.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'em-statsd'
|
27
|
+
require 'em/statsd'
|
28
|
+
|
29
|
+
EM::Statsd.logger = Logger.new(STDERR)
|
30
|
+
EM.run {
|
31
|
+
statsd = EM::Statsd.new('localhost', 12345);
|
32
|
+
EM.next_tick { statsd.increment("kittens") }
|
33
|
+
}
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/em-statsd.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/em-statsd/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Tim Snowhite"]
|
6
|
+
gem.email = ["tsnowhite@taximagic.com"]
|
7
|
+
gem.description = %q{EventMachine Client for Statsd Reporting}
|
8
|
+
gem.summary = %q{Injects an eventmachine connection into statsd-ruby}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "em-statsd"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = EM::Statsd::VERSION
|
17
|
+
gem.add_dependency 'statsd-ruby', "~>0.2"
|
18
|
+
gem.add_dependency 'eventmachine', '~>1.0'
|
19
|
+
gem.add_development_dependency(%q<minitest>, [">= 0"])
|
20
|
+
|
21
|
+
|
22
|
+
end
|
data/lib/em/statsd.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "em-statsd/version"
|
2
|
+
|
3
|
+
#FROM: http://pastebin.com/pKZwrCCZ
|
4
|
+
#BY: A GUEST ON SEP 4TH, 2012
|
5
|
+
require "eventmachine"
|
6
|
+
require "statsd" # statsd-ruby gem
|
7
|
+
|
8
|
+
module EM
|
9
|
+
class Statsd < ::Statsd
|
10
|
+
|
11
|
+
class ConnectionWrapper
|
12
|
+
def initialize(em_connection)
|
13
|
+
@em_connection = em_connection
|
14
|
+
end
|
15
|
+
|
16
|
+
def send(message, flags, host, port)
|
17
|
+
@em_connection.send_datagram(message, host, port)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :socket
|
22
|
+
private :socket
|
23
|
+
|
24
|
+
def initialize(host, port=8125)
|
25
|
+
super
|
26
|
+
# eventmachine forces us to listen on a UDP socket even
|
27
|
+
# though we only
|
28
|
+
# want to send, so we'll just give it a junk address
|
29
|
+
em_connection = EM.open_datagram_socket("0.0.0.0", 0, EM::Connection)
|
30
|
+
@socket = ConnectionWrapper.new(em_connection)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/spec/helper.rb
ADDED
data/spec/statsd_spec.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
require 'tempfile'
|
4
|
+
$host,$port = '127.0.0.1', 12345
|
5
|
+
class Object
|
6
|
+
def with_em_client(port=$port,after_fork = Proc.new{})
|
7
|
+
r,w = IO.pipe
|
8
|
+
client_pid = fork {
|
9
|
+
$stderr.puts "test client starting on #{port}"
|
10
|
+
trap('SIGINT') { puts "shutting down test client"; Kernel.exit!(0) }
|
11
|
+
EM::run {
|
12
|
+
statsd = EM::Statsd.new($host, port)
|
13
|
+
after_fork.call(statsd)
|
14
|
+
r.close
|
15
|
+
EM.next_tick{ w.write("done"); w.close}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
w.close
|
19
|
+
r.read
|
20
|
+
r.close
|
21
|
+
yield if client_pid
|
22
|
+
$stderr.puts "in client" unless client_pid
|
23
|
+
ensure
|
24
|
+
if client_pid
|
25
|
+
Process.kill('INT', client_pid)
|
26
|
+
Process.wait
|
27
|
+
$stderr.puts ["client process",$?.to_i , "finished with ", $?.exitstatus.inspect].join(' ')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
describe EM::Statsd do
|
32
|
+
describe "with a real UDP socket" do
|
33
|
+
before {
|
34
|
+
}
|
35
|
+
require 'stringio'
|
36
|
+
before { EM::Statsd.logger = Logger.new(@log = Tempfile.new("statsd")); @log.sync=true; @log.unlink}
|
37
|
+
after { @log.close; GC.start}
|
38
|
+
it "should write to the log in debug" do
|
39
|
+
EM::Statsd.logger.level = Logger::DEBUG
|
40
|
+
socket = UDPSocket.new
|
41
|
+
port = $port -1
|
42
|
+
socket.bind($host, port)
|
43
|
+
|
44
|
+
with_em_client(port,lambda{|statsd| statsd.increment('foobar')}) do
|
45
|
+
socket.recvfrom(16)
|
46
|
+
@log.rewind
|
47
|
+
@log.read.must_match "Statsd: foobar:1|c"
|
48
|
+
end
|
49
|
+
socket = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should not write to the log unless debug" do
|
53
|
+
EM::Statsd.logger.level = Logger::INFO
|
54
|
+
socket = UDPSocket.new
|
55
|
+
port = $port
|
56
|
+
socket.bind($host, port)
|
57
|
+
|
58
|
+
with_em_client(port,lambda{|statsd| statsd.increment('foobar')}) do
|
59
|
+
socket.recvfrom(16)
|
60
|
+
@log.rewind
|
61
|
+
@log.read.must_be_empty
|
62
|
+
end
|
63
|
+
socket = nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should actually send stuff over the socket" do
|
67
|
+
socket = UDPSocket.new
|
68
|
+
port = $port + 1
|
69
|
+
socket.bind($host, port)
|
70
|
+
|
71
|
+
with_em_client(port,lambda{|statsd| statsd.increment('foobar')}) do
|
72
|
+
message = socket.recvfrom(16).first
|
73
|
+
message.must_equal 'foobar:1|c'
|
74
|
+
end
|
75
|
+
socket =nil
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end if ENV['LIVE']
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-statsd
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tim Snowhite
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: statsd-ruby
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: eventmachine
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: EventMachine Client for Statsd Reporting
|
63
|
+
email:
|
64
|
+
- tsnowhite@taximagic.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- em-statsd.gemspec
|
75
|
+
- lib/em-statsd/version.rb
|
76
|
+
- lib/em/statsd.rb
|
77
|
+
- spec/helper.rb
|
78
|
+
- spec/statsd_spec.rb
|
79
|
+
homepage: ''
|
80
|
+
licenses: []
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 1.8.24
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Injects an eventmachine connection into statsd-ruby
|
103
|
+
test_files:
|
104
|
+
- spec/helper.rb
|
105
|
+
- spec/statsd_spec.rb
|