statsd-client 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/Gemfile.lock +18 -0
- data/Rakefile +13 -0
- data/lib/statsd.rb +12 -4
- data/statsd-client.gemspec +4 -0
- data/test/statsd_test.rb +124 -0
- data/test/test_helper.rb +7 -0
- metadata +35 -5
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/Rakefile
ADDED
data/lib/statsd.rb
CHANGED
@@ -1,13 +1,23 @@
|
|
1
1
|
require 'socket'
|
2
|
+
require 'resolv'
|
2
3
|
|
3
4
|
class Statsd
|
4
5
|
|
5
|
-
Version = '0.0.
|
6
|
+
Version = '0.0.4'
|
6
7
|
|
7
8
|
class << self
|
8
9
|
|
9
10
|
attr_accessor :host, :port
|
10
11
|
|
12
|
+
def host_ip_addr
|
13
|
+
@host_ip_addr ||= Resolv.getaddress(host)
|
14
|
+
end
|
15
|
+
|
16
|
+
def host=(h)
|
17
|
+
@host_ip_addr = nil
|
18
|
+
@host = h
|
19
|
+
end
|
20
|
+
|
11
21
|
# +stat+ to log timing for
|
12
22
|
# +time+ is the time to log in ms
|
13
23
|
def timing(stat, time, sample_rate = 1)
|
@@ -53,7 +63,7 @@ class Statsd
|
|
53
63
|
begin
|
54
64
|
sock = UDPSocket.new
|
55
65
|
data.each do |d|
|
56
|
-
sock.send(d, 0,
|
66
|
+
sock.send(d, 0, host_ip_addr, port)
|
57
67
|
end
|
58
68
|
rescue # silent but deadly
|
59
69
|
ensure
|
@@ -61,9 +71,7 @@ class Statsd
|
|
61
71
|
end
|
62
72
|
true
|
63
73
|
end
|
64
|
-
|
65
74
|
|
66
75
|
end
|
67
76
|
|
68
|
-
|
69
77
|
end
|
data/statsd-client.gemspec
CHANGED
@@ -16,5 +16,9 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
17
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
18
18
|
s.require_path = 'lib'
|
19
|
+
|
20
|
+
s.add_development_dependency("shoulda-context")
|
21
|
+
s.add_development_dependency("mocha")
|
22
|
+
|
19
23
|
end
|
20
24
|
|
data/test/statsd_test.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class StatsdTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
Statsd.host = 'localhost'
|
7
|
+
Statsd.port = 8125
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
context "timing" do
|
12
|
+
|
13
|
+
should "send with ms" do
|
14
|
+
expected_send('test.stat:23|ms')
|
15
|
+
Statsd.timing('test.stat', 23)
|
16
|
+
end
|
17
|
+
|
18
|
+
should "log when sampled" do
|
19
|
+
fake_rand(0.09)
|
20
|
+
expected_send('test.stat:23|ms@0.1')
|
21
|
+
Statsd.timing('test.stat', 23, 0.1)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "not log when not sampled" do
|
25
|
+
fake_rand(0.11)
|
26
|
+
expect_nothing
|
27
|
+
Statsd.timing('test.stat', 23, 0.1)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context "increment" do
|
33
|
+
|
34
|
+
should "send number" do
|
35
|
+
expected_send('test.stat:1|c')
|
36
|
+
Statsd.increment('test.stat')
|
37
|
+
end
|
38
|
+
|
39
|
+
should "log when sampled" do
|
40
|
+
fake_rand(0.09)
|
41
|
+
expected_send('test.stat:1|c@0.1')
|
42
|
+
Statsd.increment('test.stat', 0.1)
|
43
|
+
end
|
44
|
+
|
45
|
+
should "not log when not sampled" do
|
46
|
+
fake_rand(0.11)
|
47
|
+
expect_nothing
|
48
|
+
Statsd.increment('test.stat', 0.1)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
context "decrement" do
|
54
|
+
|
55
|
+
should "send number" do
|
56
|
+
expected_send('test.stat:-1|c')
|
57
|
+
Statsd.decrement('test.stat')
|
58
|
+
end
|
59
|
+
|
60
|
+
should "log when sampled" do
|
61
|
+
fake_rand(0.09)
|
62
|
+
expected_send('test.stat:-1|c@0.1')
|
63
|
+
Statsd.decrement('test.stat', 0.1)
|
64
|
+
end
|
65
|
+
|
66
|
+
should "not log when not sampled" do
|
67
|
+
fake_rand(0.11)
|
68
|
+
expect_nothing
|
69
|
+
Statsd.decrement('test.stat', 0.1)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
context "host" do
|
75
|
+
|
76
|
+
should "be gettable and settable" do
|
77
|
+
Statsd.host = 'statsd-01'
|
78
|
+
assert_equal 'statsd-01', Statsd.host
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
context "port" do
|
84
|
+
|
85
|
+
should "be gettable and settable" do
|
86
|
+
Statsd.port = 1234
|
87
|
+
assert_equal 1234, Statsd.port
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
context "host_ip_addr" do
|
93
|
+
|
94
|
+
should "resolve dns" do
|
95
|
+
Statsd.host = 'localhost'
|
96
|
+
assert loopback?(Statsd.host_ip_addr)
|
97
|
+
end
|
98
|
+
|
99
|
+
should "be cleared when host is set" do
|
100
|
+
Statsd.host = 'statsd-01'
|
101
|
+
assert_nil Statsd.instance_variable_get(:@host_ip_addr)
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def fake_rand(v)
|
109
|
+
Statsd.stubs(:rand).returns(v)
|
110
|
+
end
|
111
|
+
|
112
|
+
def expected_send(buf)
|
113
|
+
UDPSocket.any_instance.expects(:send).with(buf, 0, Statsd.host_ip_addr, Statsd.port).once
|
114
|
+
end
|
115
|
+
|
116
|
+
def expect_nothing
|
117
|
+
UDPSocket.any_instance.expects(:send).never
|
118
|
+
end
|
119
|
+
|
120
|
+
def loopback?(ip)
|
121
|
+
['::1', '127.0.0.1'].include?(ip)
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben VandenBos
|
@@ -14,10 +14,35 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-04-20 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: shoulda-context
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: mocha
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
21
46
|
description: Ruby client for statsd.
|
22
47
|
email:
|
23
48
|
- bvandenbos@gmail.com
|
@@ -29,9 +54,14 @@ extra_rdoc_files: []
|
|
29
54
|
|
30
55
|
files:
|
31
56
|
- .gitignore
|
57
|
+
- Gemfile
|
58
|
+
- Gemfile.lock
|
32
59
|
- README.md
|
60
|
+
- Rakefile
|
33
61
|
- lib/statsd.rb
|
34
62
|
- statsd-client.gemspec
|
63
|
+
- test/statsd_test.rb
|
64
|
+
- test/test_helper.rb
|
35
65
|
has_rdoc: true
|
36
66
|
homepage: http://github.com/bvandenbos/statsd-client
|
37
67
|
licenses: []
|