statsd-instrument 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/statsd/instrument.rb +11 -1
- data/statsd-instrument.gemspec +1 -1
- data/test/statsd-instrument_test.rb +41 -0
- metadata +4 -5
data/lib/statsd/instrument.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'socket'
|
2
2
|
require 'benchmark'
|
3
|
+
require 'timeout'
|
3
4
|
|
4
5
|
class << Benchmark
|
5
6
|
def ms
|
@@ -7,6 +8,7 @@ class << Benchmark
|
|
7
8
|
end
|
8
9
|
end
|
9
10
|
|
11
|
+
|
10
12
|
module StatsD
|
11
13
|
class << self
|
12
14
|
attr_accessor :host, :port, :mode, :logger, :enabled, :default_sample_rate
|
@@ -14,6 +16,8 @@ module StatsD
|
|
14
16
|
self.enabled = true
|
15
17
|
self.default_sample_rate = 1
|
16
18
|
|
19
|
+
Timeout = defined?(::SystemTimer) ? ::SystemTimer : ::Timeout
|
20
|
+
|
17
21
|
# StatsD.server = 'localhost:1234'
|
18
22
|
def self.server=(conn)
|
19
23
|
self.host, port = conn.split(':')
|
@@ -130,10 +134,16 @@ module StatsD
|
|
130
134
|
command << "|@#{sample_rate}" if sample_rate < 1
|
131
135
|
|
132
136
|
if mode == :production
|
133
|
-
socket.send(command, 0, host, port)
|
137
|
+
socket_wrapper { socket.send(command, 0, host, port) }
|
134
138
|
else
|
135
139
|
logger.info "[StatsD] #{command}"
|
136
140
|
end
|
137
141
|
end
|
142
|
+
|
143
|
+
def self.socket_wrapper(options = {})
|
144
|
+
Timeout.timeout(options.fetch(:timeout, 0.1)) { yield }
|
145
|
+
rescue Timeout::Error, SocketError, IOError, SystemCallError => e
|
146
|
+
logger.error e
|
147
|
+
end
|
138
148
|
end
|
139
149
|
|
data/statsd-instrument.gemspec
CHANGED
@@ -153,4 +153,45 @@ class StatsDTest < Test::Unit::TestCase
|
|
153
153
|
|
154
154
|
StatsD.measure('values.foobar', 42)
|
155
155
|
end
|
156
|
+
|
157
|
+
def test_socket_error_should_not_raise
|
158
|
+
StatsD.mode = :production
|
159
|
+
UDPSocket.any_instance.expects(:send).raises(SocketError)
|
160
|
+
StatsD.measure('values.foobar', 42)
|
161
|
+
StatsD.mode = :test
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_timeout_error_should_not_raise
|
165
|
+
StatsD.mode = :production
|
166
|
+
UDPSocket.any_instance.expects(:send).raises(Timeout::Error)
|
167
|
+
StatsD.measure('values.foobar', 42)
|
168
|
+
StatsD.mode = :test
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_system_call_error_should_not_raise
|
172
|
+
StatsD.mode = :production
|
173
|
+
UDPSocket.any_instance.expects(:send).raises(Errno::ETIMEDOUT)
|
174
|
+
StatsD.measure('values.foobar', 42)
|
175
|
+
StatsD.mode = :test
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_io_error_should_not_raise
|
179
|
+
StatsD.mode = :production
|
180
|
+
UDPSocket.any_instance.expects(:send).raises(IOError)
|
181
|
+
StatsD.measure('values.foobar', 42)
|
182
|
+
StatsD.mode = :test
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_long_request_should_timeout
|
186
|
+
StatsD.mode = :production
|
187
|
+
UDPSocket.any_instance.expects(:send).yields do
|
188
|
+
begin
|
189
|
+
Timeout.timeout(0.5) { sleep 1 }
|
190
|
+
rescue Timeout::Error
|
191
|
+
raise "Allowed long running request"
|
192
|
+
end
|
193
|
+
end
|
194
|
+
StatsD.measure('values.foobar', 42)
|
195
|
+
StatsD.mode = :test
|
196
|
+
end
|
156
197
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statsd-instrument
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
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-02-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mocha
|
16
|
-
requirement: &
|
16
|
+
requirement: &2155684560 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2155684560
|
25
25
|
description: A StatsD client for Ruby apps. Provides metaprogramming methods to inject
|
26
26
|
StatsD instrumentation into your code.
|
27
27
|
email:
|
@@ -65,4 +65,3 @@ specification_version: 3
|
|
65
65
|
summary: A StatsD client for Ruby apps
|
66
66
|
test_files:
|
67
67
|
- test/statsd-instrument_test.rb
|
68
|
-
has_rdoc:
|