em-statsd 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eventmachine-statsd.gemspec
4
+ gemspec
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
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:spec) do |spec|
6
+ spec.libs << 'lib' << 'spec'
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ spec.verbose = true
9
+ end
10
+
11
+ task :default => :spec
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
@@ -0,0 +1,5 @@
1
+ module EM
2
+ module Statsd
3
+ VERSION = "0.0.1"
4
+ end
5
+ 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
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'em/statsd'
7
+ require 'logger'
@@ -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