statsd-client 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -7
- data/lib/statsd.rb +22 -18
- data/test/statsd_test.rb +51 -30
- metadata +19 -9
data/README.md
CHANGED
@@ -10,21 +10,24 @@ Example
|
|
10
10
|
|
11
11
|
require 'rubygems'
|
12
12
|
require 'statsd'
|
13
|
-
|
13
|
+
|
14
14
|
Statsd.host = 'localhost'
|
15
15
|
Statsd.port = 8125
|
16
|
-
|
16
|
+
|
17
17
|
Statsd.increment('some_counter') # basic incrementing
|
18
18
|
Statsd.increment('system.nested_counter', 0.1) # incrementing with sampling (10%)
|
19
19
|
|
20
20
|
Statsd.decrement(:some_other_counter) # basic decrememting using a symbol
|
21
21
|
Statsd.decrement('system.nested_counter', 0.1) # decrementing with sampling (10%)
|
22
|
-
|
22
|
+
|
23
23
|
Statsd.timing('some_job_time', 20) # reporting job that took 20ms
|
24
24
|
Statsd.timing('some_job_time', 20, 0.05) # reporting job that took 20ms with sampling (5% sampling)
|
25
|
-
|
26
|
-
#
|
27
|
-
Statsd.
|
25
|
+
|
26
|
+
Statsd.gauge('some_gauge', 1337) # sending gauge values
|
27
|
+
Statsd.gauge('system.nested_gauge', 1337, 0.1) # sending gauge with sampling
|
28
|
+
|
29
|
+
# passing a block to `timing` will capture the time it takes to execute
|
30
|
+
Statsd.timing('some_job_time') do
|
28
31
|
# do some job
|
29
32
|
end
|
30
|
-
|
33
|
+
|
data/lib/statsd.rb
CHANGED
@@ -2,22 +2,22 @@ require 'socket'
|
|
2
2
|
require 'resolv'
|
3
3
|
|
4
4
|
class Statsd
|
5
|
-
|
6
|
-
Version = '0.0.
|
7
|
-
|
5
|
+
|
6
|
+
Version = '0.0.7'
|
7
|
+
|
8
8
|
class << self
|
9
|
-
|
9
|
+
|
10
10
|
attr_accessor :host, :port
|
11
|
-
|
11
|
+
|
12
12
|
def host_ip_addr
|
13
13
|
@host_ip_addr ||= Resolv.getaddress(host)
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def host=(h)
|
17
17
|
@host_ip_addr = nil
|
18
18
|
@host = h
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
# +stat+ to log timing for
|
22
22
|
# +time+ is the time to log in ms
|
23
23
|
def timing(stat, time = nil, sample_rate = 1)
|
@@ -28,29 +28,33 @@ class Statsd
|
|
28
28
|
end
|
29
29
|
send_stats("#{stat}:#{time}|ms", sample_rate)
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
|
+
def gauge(stat, value, sample_rate = 1)
|
33
|
+
send_stats("#{stat}:#{value}|g", sample_rate)
|
34
|
+
end
|
35
|
+
|
32
36
|
# +stats+ can be a string or an array of strings
|
33
37
|
def increment(stats, sample_rate = 1)
|
34
38
|
update_counter stats, 1, sample_rate
|
35
39
|
end
|
36
|
-
|
40
|
+
|
37
41
|
# +stats+ can be a string or an array of strings
|
38
42
|
def decrement(stats, sample_rate = 1)
|
39
43
|
update_counter stats, -1, sample_rate
|
40
44
|
end
|
41
|
-
|
45
|
+
|
42
46
|
# +stats+ can be a string or array of strings
|
43
47
|
def update_counter(stats, delta = 1, sample_rate = 1)
|
44
48
|
stats = Array(stats)
|
45
49
|
send_stats(stats.map { |s| "#{s}:#{delta}|c" }, sample_rate)
|
46
50
|
end
|
47
|
-
|
51
|
+
|
48
52
|
private
|
49
|
-
|
53
|
+
|
50
54
|
def send_stats(data, sample_rate = 1)
|
51
55
|
data = Array(data)
|
52
56
|
sampled_data = []
|
53
|
-
|
57
|
+
|
54
58
|
# Apply sample rate if less than one
|
55
59
|
if sample_rate < 1
|
56
60
|
data.each do |d|
|
@@ -60,11 +64,11 @@ class Statsd
|
|
60
64
|
end
|
61
65
|
data = sampled_data
|
62
66
|
end
|
63
|
-
|
67
|
+
|
64
68
|
return if data.empty?
|
65
|
-
|
69
|
+
|
66
70
|
raise "host and port must be set" unless host && port
|
67
|
-
|
71
|
+
|
68
72
|
begin
|
69
73
|
sock = UDPSocket.new
|
70
74
|
data.each do |d|
|
@@ -76,7 +80,7 @@ class Statsd
|
|
76
80
|
end
|
77
81
|
true
|
78
82
|
end
|
79
|
-
|
83
|
+
|
80
84
|
end
|
81
|
-
|
85
|
+
|
82
86
|
end
|
data/test/statsd_test.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
1
|
require File.expand_path('../test_helper', __FILE__)
|
2
2
|
|
3
3
|
class StatsdTest < Test::Unit::TestCase
|
4
|
-
|
4
|
+
|
5
5
|
def setup
|
6
6
|
Statsd.host = 'localhost'
|
7
7
|
Statsd.port = 8125
|
8
8
|
super
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
context "timing" do
|
12
|
-
|
12
|
+
|
13
13
|
should "send with ms" do
|
14
14
|
expected_send('test.stat:23|ms')
|
15
15
|
Statsd.timing('test.stat', 23)
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
should "log when sampled" do
|
19
19
|
fake_rand(0.09)
|
20
20
|
expected_send('test.stat:23|ms@0.1')
|
21
21
|
Statsd.timing('test.stat', 23, 0.1)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
should "not log when not sampled" do
|
25
25
|
fake_rand(0.11)
|
26
26
|
expect_nothing
|
27
|
-
Statsd.timing('test.stat', 23, 0.1)
|
27
|
+
Statsd.timing('test.stat', 23, 0.1)
|
28
28
|
end
|
29
29
|
|
30
30
|
should "work with a block" do
|
@@ -33,11 +33,11 @@ class StatsdTest < Test::Unit::TestCase
|
|
33
33
|
sleep 0.023
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
context "increment" do
|
40
|
-
|
40
|
+
|
41
41
|
should "send number" do
|
42
42
|
expected_send('test.stat:1|c')
|
43
43
|
Statsd.increment('test.stat')
|
@@ -52,13 +52,13 @@ class StatsdTest < Test::Unit::TestCase
|
|
52
52
|
should "not log when not sampled" do
|
53
53
|
fake_rand(0.11)
|
54
54
|
expect_nothing
|
55
|
-
Statsd.increment('test.stat', 0.1)
|
55
|
+
Statsd.increment('test.stat', 0.1)
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
context "decrement" do
|
61
|
-
|
61
|
+
|
62
62
|
should "send number" do
|
63
63
|
expected_send('test.stat:-1|c')
|
64
64
|
Statsd.decrement('test.stat')
|
@@ -73,60 +73,81 @@ class StatsdTest < Test::Unit::TestCase
|
|
73
73
|
should "not log when not sampled" do
|
74
74
|
fake_rand(0.11)
|
75
75
|
expect_nothing
|
76
|
-
Statsd.decrement('test.stat', 0.1)
|
76
|
+
Statsd.decrement('test.stat', 0.1)
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
|
+
context "gauge" do
|
82
|
+
|
83
|
+
should "send number" do
|
84
|
+
expected_send('test.stat:1337|g')
|
85
|
+
Statsd.gauge('test.stat', 1337)
|
86
|
+
end
|
87
|
+
|
88
|
+
should "log when sampled" do
|
89
|
+
fake_rand(0.09)
|
90
|
+
expected_send('test.stat:1337|g@0.1')
|
91
|
+
Statsd.gauge('test.stat', 1337, 0.1)
|
92
|
+
end
|
93
|
+
|
94
|
+
should "not log when not sampled" do
|
95
|
+
fake_rand(0.11)
|
96
|
+
expect_nothing
|
97
|
+
Statsd.gauge('test.stat', 1337, 0.1)
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
81
102
|
context "host" do
|
82
|
-
|
103
|
+
|
83
104
|
should "be gettable and settable" do
|
84
105
|
Statsd.host = 'statsd-01'
|
85
106
|
assert_equal 'statsd-01', Statsd.host
|
86
107
|
end
|
87
|
-
|
108
|
+
|
88
109
|
end
|
89
|
-
|
110
|
+
|
90
111
|
context "port" do
|
91
112
|
|
92
113
|
should "be gettable and settable" do
|
93
114
|
Statsd.port = 1234
|
94
115
|
assert_equal 1234, Statsd.port
|
95
116
|
end
|
96
|
-
|
117
|
+
|
97
118
|
end
|
98
|
-
|
119
|
+
|
99
120
|
context "host_ip_addr" do
|
100
|
-
|
121
|
+
|
101
122
|
should "resolve dns" do
|
102
123
|
Statsd.host = 'localhost'
|
103
124
|
assert loopback?(Statsd.host_ip_addr)
|
104
125
|
end
|
105
|
-
|
126
|
+
|
106
127
|
should "be cleared when host is set" do
|
107
128
|
Statsd.host = 'statsd-01'
|
108
129
|
assert_nil Statsd.instance_variable_get(:@host_ip_addr)
|
109
130
|
end
|
110
|
-
|
131
|
+
|
111
132
|
end
|
112
|
-
|
133
|
+
|
113
134
|
private
|
114
|
-
|
135
|
+
|
115
136
|
def fake_rand(v)
|
116
137
|
Statsd.stubs(:rand).returns(v)
|
117
138
|
end
|
118
|
-
|
139
|
+
|
119
140
|
def expected_send(buf)
|
120
141
|
buf_re = Regexp.new(buf)
|
121
142
|
UDPSocket.any_instance.expects(:send).with(regexp_matches(buf_re), 0, Statsd.host_ip_addr, Statsd.port).once
|
122
143
|
end
|
123
|
-
|
144
|
+
|
124
145
|
def expect_nothing
|
125
146
|
UDPSocket.any_instance.expects(:send).never
|
126
147
|
end
|
127
|
-
|
148
|
+
|
128
149
|
def loopback?(ip)
|
129
150
|
['::1', '127.0.0.1'].include?(ip)
|
130
151
|
end
|
131
|
-
|
152
|
+
|
132
153
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statsd-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-06-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: shoulda-context
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: mocha
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,7 +37,12 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
description: Ruby client for statsd.
|
37
47
|
email:
|
38
48
|
- bvandenbos@gmail.com
|
@@ -62,7 +72,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
72
|
version: '0'
|
63
73
|
segments:
|
64
74
|
- 0
|
65
|
-
hash:
|
75
|
+
hash: -1611389077087709382
|
66
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
77
|
none: false
|
68
78
|
requirements:
|
@@ -71,10 +81,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
81
|
version: '0'
|
72
82
|
segments:
|
73
83
|
- 0
|
74
|
-
hash:
|
84
|
+
hash: -1611389077087709382
|
75
85
|
requirements: []
|
76
86
|
rubyforge_project:
|
77
|
-
rubygems_version: 1.8.
|
87
|
+
rubygems_version: 1.8.21
|
78
88
|
signing_key:
|
79
89
|
specification_version: 3
|
80
90
|
summary: Ruby client for statsd.
|