dawanda-statsd-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.3.0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.5.2"
7
+ gem "rcov", ">= 0"
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.0.0)
26
+ jeweler (~> 1.5.2)
27
+ rcov
28
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tom Taylor
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ statsd-client
2
+ =============
3
+
4
+ A Ruby client for [StatsD](https://github.com/etsy/statsd), Etsy's daemon for easy stats aggregation.
5
+
6
+ Usage
7
+ -----
8
+
9
+ client = Statsd::Client.new(host, port)
10
+ client.timing('example.stat.1', 350)
11
+ client.increment('example.stat.2')
12
+ client.decrement('example.stat.2')
13
+
14
+ Documentation
15
+ -------------
16
+
17
+ [On rdoc.info](http://rdoc.info/github/tomtaylor/statsd-client/master/frames)
18
+
19
+ Copyright
20
+ ---------
21
+
22
+ Copyright (c) 2011 Tom Taylor. See LICENSE.txt for
23
+ further details.
24
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "dawanda-statsd-client"
16
+ gem.homepage = "http://github.com/dawanda/statsd-client"
17
+ gem.license = "MIT"
18
+ gem.summary = "Client for statds"
19
+ gem.description = %Q{A Ruby client for statsd}
20
+ gem.email = "tom@tomtaylor.co.uk"
21
+ gem.authors = ["Tom Taylor"]
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
24
+
25
+ require 'rspec/core'
26
+ require 'rspec/core/rake_task'
27
+ RSpec::Core::RakeTask.new(:spec) do |spec|
28
+ spec.pattern = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "statsd-client #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/statsd-send ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'optparse'
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
6
+
7
+ require 'statsd-client'
8
+
9
+ options = {}
10
+ OptionParser.new do |opts|
11
+ opts.banner = <<BANNER
12
+ Report to statsd (https://github.com/etsy/statsd) from console
13
+ Needs statsd-client.yml in /etc or ~/, see statsd-client.example.yml for example
14
+
15
+ Usage:
16
+ statsd-send inc users.login --by=5
17
+ statsd-send dec users.login --by=5
18
+ statsd-send time pages.homepage 333
19
+
20
+ Options:
21
+ BANNER
22
+
23
+ opts.on("-h", "--help","Show this.") { puts opts; exit }
24
+ opts.on('-v', '--version','Show Version'){ puts Statsd::Client::VERSION; exit}
25
+ opts.on('--by x','Value for inc/dec') do |value|
26
+ options[:by] = value.to_i
27
+ end
28
+ opts.on('--host x', 'Use this host, when reporting') do |host|
29
+ options[:host] = host
30
+ end
31
+ end.parse!
32
+
33
+ command, metric, value = ARGV[0..2]
34
+ if metric.to_s.empty?
35
+ puts "Usage instructions: statsd-send --help"
36
+ exit
37
+ end
38
+
39
+ if command == 'time'
40
+ Statsd.time(metric, value)
41
+ else
42
+ Statsd.send(command, metric, options)
43
+ end
44
+
@@ -0,0 +1,154 @@
1
+ # @author Tom Taylor
2
+ require 'socket'
3
+ require 'yaml'
4
+
5
+ class Statsd
6
+ class << self
7
+ def increment(metric, options={})
8
+ value = options.is_a?(Fixnum) ? options : (options[:by] || 1)
9
+ client.update_stats(metric, value*factor, (options[:sample_rate] || 1))
10
+ end
11
+ alias_method :inc, :increment
12
+
13
+ def decrement(metric, options={})
14
+ value = options.is_a?(Fixnum) ? options : (options[:by] || 1)
15
+ client.update_stats(metric, value*factor*(-1), (options[:sample_rate] || 1))
16
+ end
17
+ alias_method :dec, :decrement
18
+
19
+ def timing(metric, value)
20
+ client.timing(metric, value)
21
+ end
22
+ alias_method :time, :timing
23
+
24
+ def client
25
+ return Statsd::DummyClient if deactivated?
26
+ @client ||= Statsd::Client.new(host, port)
27
+ end
28
+
29
+ def config
30
+ home = File.expand_path('~')
31
+ files = ["#{home}/.statsd-client.yml", '/etc/statsd-client.yml']
32
+ files.each do |file|
33
+ return YAML.load(File.read(file)) if File.exist?(file)
34
+ end
35
+ raise "No config found: #{files.join(' or ')}"
36
+ end
37
+
38
+ def host
39
+ config['host'] || 'localhost'
40
+ end
41
+
42
+ def port
43
+ config['port'] || 8125
44
+ end
45
+
46
+ # statds reports with default configs 1/10 of actual value
47
+ def factor
48
+ config['factor'] || 10
49
+ end
50
+
51
+ def deactivated?
52
+ config['deactivated'] || false
53
+ end
54
+ end
55
+
56
+ class DummyClient
57
+ def self.timing(*args)
58
+ end
59
+
60
+ def self.update_stats(*args)
61
+ end
62
+ end
63
+
64
+
65
+ class Client
66
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
67
+ attr_reader :host, :port
68
+
69
+ # Initializes a Statsd client.
70
+ #
71
+ # @param [String] host
72
+ # @param [Integer] port
73
+ def initialize(host = 'localhost', port = 8125)
74
+ @host, @port = host, port
75
+ end
76
+
77
+ # Sends timing statistics.
78
+ #
79
+ # @param [Array, String] stats name of statistic(s) being updated
80
+ # @param [Integer] time in miliseconds
81
+ # @param [Integer, Float] sample_rate
82
+ def timing(stats, time, sample_rate = 1)
83
+ data = "#{time}|ms"
84
+ update_stats(stats, data, sample_rate)
85
+ end
86
+
87
+ # Increments a counter
88
+ #
89
+ # @param [Array, String] stats name of statistic(s) being updated
90
+ # @param [Integer, Float] sample_rate
91
+ def increment(stats, sample_rate = 1)
92
+ update_stats(stats, 1, sample_rate)
93
+ end
94
+
95
+ # Decrements a counter
96
+ #
97
+ # @param [Array, String] stats name of statistic(s) being updated
98
+ # @param [Integer, Float] sample_rate
99
+ def decrement(stats, sample_rate = 1)
100
+ update_stats(stats, -1, sample_rate)
101
+ end
102
+
103
+ # Updates one or more counters by an arbitrary amount
104
+ #
105
+ # @param [Array, String] stats name of statistic(s) being updated
106
+ # @param [Integer, Float] delta
107
+ # @param [Integer, Float] sample_rate
108
+ def update_stats(stats, delta = 1, sample_rate = 1)
109
+ stats = [stats] unless stats.kind_of?(Array)
110
+
111
+ data = {}
112
+
113
+ delta = delta.to_s
114
+ stats.each do |stat|
115
+ # if it's got a |ms in it, we know it's a timing stat, so don't append
116
+ # the |c.
117
+ data[stat] = delta.include?('|ms') ? delta : "#{delta}|c"
118
+ end
119
+
120
+ send(data, sample_rate)
121
+ end
122
+
123
+ private
124
+
125
+ def send(data, sample_rate = 1)
126
+ puts "sending #{data}"
127
+ sampled_data = {}
128
+
129
+ if sample_rate < 1
130
+ if Kernel.rand <= sample_rate
131
+ data.each do |k,v|
132
+ sampled_data[k] = "#{v}|@#{sample_rate}"
133
+ end
134
+ end
135
+ else
136
+ sampled_data = data
137
+ end
138
+
139
+ socket = UDPSocket.new
140
+
141
+ begin
142
+ sampled_data.each do |k,v|
143
+ message = [k,v].join(':')
144
+ socket.send(message, 0, self.host, self.port)
145
+ end
146
+ rescue Exception => e
147
+ puts "Unexpected error: #{e}"
148
+ ensure
149
+ socket.close
150
+ end
151
+ end
152
+
153
+ end
154
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'statsd-client'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
@@ -0,0 +1,148 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Statsd::Client do
4
+
5
+ describe "being initialized" do
6
+
7
+ describe "without any parameters" do
8
+ subject { Statsd::Client.new }
9
+ it { subject.host.should == 'localhost' }
10
+ it { subject.port.should == 8125 }
11
+ end
12
+
13
+ describe "with a host and port parameter" do
14
+ subject { Statsd::Client.new('example.com', 5555) }
15
+ it { subject.host.should == 'example.com' }
16
+ it { subject.port.should == 5555 }
17
+ end
18
+
19
+ end
20
+
21
+ describe "sending a timing statistic" do
22
+
23
+ subject { Statsd::Client.new }
24
+ before(:each) do
25
+ @udp_socket = mock(UDPSocket, :close => true)
26
+ UDPSocket.should_receive(:new).and_return(@udp_socket)
27
+ end
28
+
29
+ describe "without a sample rate" do
30
+
31
+ after(:each) { subject.timing('test', 350) }
32
+
33
+ it "should send a packet with the message 'test:350|ms'" do
34
+ @udp_socket.should_receive(:send).with("test:350|ms", 0, subject.host, subject.port)
35
+ end
36
+
37
+ end
38
+
39
+ describe "with sample rate of 0.5 (where it has been sampled for sending)" do
40
+
41
+ before(:each) { Kernel.should_receive(:rand).and_return(0.1) }
42
+ after(:each) { subject.timing('test', 350, 0.5) }
43
+
44
+ it "should send a packet with the message 'test:350|ms|@0.5" do
45
+ @udp_socket.should_receive(:send).with("test:350|ms|@0.5", 0, subject.host, subject.port)
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ describe "incrementing a statistic" do
53
+
54
+ subject { Statsd::Client.new }
55
+ before(:each) do
56
+ @udp_socket = mock(UDPSocket, :close => true)
57
+ UDPSocket.should_receive(:new).and_return(@udp_socket)
58
+ end
59
+
60
+ describe "without a sample rate" do
61
+
62
+ after(:each) { subject.increment('test') }
63
+
64
+ it "should send a packet with the message 'test:1|c'" do
65
+ @udp_socket.should_receive(:send).with("test:1|c", 0, subject.host, subject.port)
66
+ end
67
+
68
+ end
69
+
70
+ describe "with sample rate of 0.5 (where it has been sampled for sending)" do
71
+
72
+ before(:each) { Kernel.should_receive(:rand).and_return(0.1) }
73
+ after(:each) { subject.increment('test', 0.5) }
74
+
75
+ it "should send a packet with the message 'test:1|c|@0.5" do
76
+ @udp_socket.should_receive(:send).with("test:1|c|@0.5", 0, subject.host, subject.port)
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ describe "decrementing a statistic" do
84
+
85
+ subject { Statsd::Client.new }
86
+ before(:each) do
87
+ @udp_socket = mock(UDPSocket, :close => true)
88
+ UDPSocket.should_receive(:new).and_return(@udp_socket)
89
+ end
90
+
91
+ describe "without a sample rate" do
92
+
93
+ after(:each) { subject.decrement('test') }
94
+
95
+ it "should send a packet with the message 'test:-1|c'" do
96
+ @udp_socket.should_receive(:send).with("test:-1|c", 0, subject.host, subject.port)
97
+ end
98
+
99
+ end
100
+
101
+ describe "with sample rate of 0.5 (where it has been sampled for sending)" do
102
+
103
+ before(:each) { Kernel.should_receive(:rand).and_return(0.1) }
104
+ after(:each) { subject.decrement('test', 0.5) }
105
+
106
+ it "should send a packet with the message 'test:-1|c|@0.5" do
107
+ @udp_socket.should_receive(:send).with("test:-1|c|@0.5", 0, subject.host, subject.port)
108
+ end
109
+
110
+ end
111
+
112
+ end
113
+
114
+ describe "updating many statistics" do
115
+
116
+ subject { Statsd::Client.new }
117
+ before(:each) do
118
+ @udp_socket = mock(UDPSocket, :close => true)
119
+ UDPSocket.should_receive(:new).and_return(@udp_socket)
120
+ end
121
+
122
+ describe "without a sample rate" do
123
+
124
+ after(:each) { subject.update_stats(['test.1', 'test.2'], 1) }
125
+
126
+ it "should send two packets'" do
127
+ @udp_socket.should_receive(:send).with("test.1:1|c", 0, subject.host, subject.port).once
128
+ @udp_socket.should_receive(:send).with("test.2:1|c", 0, subject.host, subject.port).once
129
+ end
130
+
131
+ end
132
+
133
+ describe "with sample rate of 0.5 (where it has been sampled for sending)" do
134
+
135
+ before(:each) { Kernel.should_receive(:rand).and_return(0.1) }
136
+ after(:each) { subject.update_stats(['test.1', 'test.2'], 1, 0.5) }
137
+
138
+ it "should send a two packets" do
139
+ @udp_socket.should_receive(:send).with("test.1:1|c|@0.5", 0, subject.host, subject.port).once
140
+ @udp_socket.should_receive(:send).with("test.2:1|c|@0.5", 0, subject.host, subject.port).once
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+
@@ -0,0 +1,5 @@
1
+ ---
2
+ host: localhost
3
+ factor: 10
4
+ port: 8125
5
+ deactivated: false
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dawanda-statsd-client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tom Taylor
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-23 00:00:00 +01:00
19
+ default_executable: statsd-send
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 2
30
+ - 3
31
+ - 0
32
+ version: 2.3.0
33
+ type: :development
34
+ name: rspec
35
+ prerelease: false
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ type: :development
50
+ name: bundler
51
+ prerelease: false
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 1
62
+ - 5
63
+ - 2
64
+ version: 1.5.2
65
+ type: :development
66
+ name: jeweler
67
+ prerelease: false
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ name: rcov
81
+ prerelease: false
82
+ version_requirements: *id004
83
+ description: A Ruby client for statsd
84
+ email: tom@tomtaylor.co.uk
85
+ executables:
86
+ - statsd-send
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ files:
93
+ - .document
94
+ - .rspec
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - VERSION
101
+ - bin/statsd-send
102
+ - lib/statsd-client.rb
103
+ - spec/spec_helper.rb
104
+ - spec/statsd-client_spec.rb
105
+ - statsd-client.example.yml
106
+ has_rdoc: true
107
+ homepage: http://github.com/dawanda/statsd-client
108
+ licenses:
109
+ - MIT
110
+ post_install_message:
111
+ rdoc_options: []
112
+
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ requirements: []
134
+
135
+ rubyforge_project:
136
+ rubygems_version: 1.3.7
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: Client for statds
140
+ test_files:
141
+ - spec/spec_helper.rb
142
+ - spec/statsd-client_spec.rb