statsd-ruby-tcp 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/README.rdoc +5 -5
- data/lib/statsd.rb +20 -20
- data/lib/statsd/monotonic_time.rb +1 -1
- data/spec/statsd_admin_spec.rb +5 -5
- data/spec/statsd_spec.rb +27 -27
- data/statsd-ruby-tcp.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ccd5371be8f0fa5a26268f59601961d9182065ef9a9d1053ab6605dce11772b
|
4
|
+
data.tar.gz: b5cdb9fc7980e0eb1191eb925da266f996a5d15bb43e25640061c6d09db7a87f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48f6a375acfcecde102ebaff454cf4c08e80331ccaba4179648c817ef9a97076fd2ad504e262a53d6b0cacbd1c287a270a2f904746e7c01d7db2ec71889c7f27
|
7
|
+
data.tar.gz: 2f987d662b65ab4d5673029e5b4f46710f6bab794d2fbb6bb16cd1e29ce65cba2faaf36fb805ee30c75e3276ce559ddab11590d7b893e12b15b2471a9e1c6751
|
data/README.rdoc
CHANGED
@@ -9,11 +9,11 @@ Bundler:
|
|
9
9
|
|
10
10
|
= Basic Usage
|
11
11
|
|
12
|
-
# Set up a global
|
13
|
-
$statsd =
|
12
|
+
# Set up a global StatsdTcp client for a server on localhost:9125
|
13
|
+
$statsd = StatsdTcp.new 'localhost', 9125
|
14
14
|
|
15
|
-
# Set up a global
|
16
|
-
$statsd =
|
15
|
+
# Set up a global StatsdTcp client for a server on IPv6 port 9125
|
16
|
+
$statsd = StatsdTcp.new '::1', 9125
|
17
17
|
|
18
18
|
# Send some stats
|
19
19
|
$statsd.increment 'garets'
|
@@ -24,7 +24,7 @@ Bundler:
|
|
24
24
|
$statsd.time('account.activate') { @account.activate! }
|
25
25
|
|
26
26
|
# Create a namespaced statsd client and increment 'account.activate'
|
27
|
-
statsd =
|
27
|
+
statsd = StatsdTcp.new('localhost').tap{|sd| sd.namespace = 'account'}
|
28
28
|
statsd.increment 'activate'
|
29
29
|
|
30
30
|
= Testing
|
data/lib/statsd.rb
CHANGED
@@ -4,12 +4,12 @@ require 'json'
|
|
4
4
|
|
5
5
|
require 'statsd/monotonic_time'
|
6
6
|
|
7
|
-
# =
|
7
|
+
# = StatsdTcp: A StatsdTcp client (https://github.com/etsy/statsd)
|
8
8
|
#
|
9
|
-
# @example Set up a global
|
10
|
-
# $statsd =
|
11
|
-
# @example Set up a global
|
12
|
-
# $statsd =
|
9
|
+
# @example Set up a global StatsdTcp client for a server on localhost:8125
|
10
|
+
# $statsd = StatsdTcp.new 'localhost', 8125
|
11
|
+
# @example Set up a global StatsdTcp client for a server on IPv6 port 8125
|
12
|
+
# $statsd = StatsdTcp.new '::1', 8125
|
13
13
|
# @example Send some stats
|
14
14
|
# $statsd.increment 'garets'
|
15
15
|
# $statsd.timing 'glork', 320
|
@@ -17,34 +17,34 @@ require 'statsd/monotonic_time'
|
|
17
17
|
# @example Use {#time} to time the execution of a block
|
18
18
|
# $statsd.time('account.activate') { @account.activate! }
|
19
19
|
# @example Create a namespaced statsd client and increment 'account.activate'
|
20
|
-
# statsd =
|
20
|
+
# statsd = StatsdTcp.new('localhost').tap{|sd| sd.namespace = 'account'}
|
21
21
|
# statsd.increment 'activate'
|
22
22
|
#
|
23
|
-
#
|
23
|
+
# StatsdTcp instances are thread safe for general usage, by utilizing the thread
|
24
24
|
# safe nature of UDP sends. The attributes are stateful, and are not
|
25
25
|
# mutexed, it is expected that users will not change these at runtime in
|
26
26
|
# threaded environments. If users require such use cases, it is recommend that
|
27
|
-
# users either mutex around their
|
27
|
+
# users either mutex around their StatsdTcp object, or create separate objects for
|
28
28
|
# each namespace / host+port combination.
|
29
|
-
class
|
29
|
+
class StatsdTcp
|
30
30
|
|
31
31
|
# = Batch: A batching statsd proxy
|
32
32
|
#
|
33
33
|
# @example Batch a set of instruments using Batch and manual flush:
|
34
|
-
# $statsd =
|
35
|
-
# batch =
|
34
|
+
# $statsd = StatsdTcp.new 'localhost', 8125
|
35
|
+
# batch = StatsdTcp::Batch.new($statsd)
|
36
36
|
# batch.increment 'garets'
|
37
37
|
# batch.timing 'glork', 320
|
38
38
|
# batch.gauge 'bork', 100
|
39
39
|
# batch.flush
|
40
40
|
#
|
41
|
-
# Batch is a subclass of
|
42
|
-
# normal
|
43
|
-
# (that inherit defaults from the supplied
|
41
|
+
# Batch is a subclass of StatsdTcp, but with a constructor that proxies to a
|
42
|
+
# normal StatsdTcp instance. It has it's own batch_size and namespace parameters
|
43
|
+
# (that inherit defaults from the supplied StatsdTcp instance). It is recommended
|
44
44
|
# that some care is taken if setting very large batch sizes. If the batch size
|
45
45
|
# exceeds the allowed packet size for UDP on your network, communication
|
46
46
|
# troubles may occur and data will be lost.
|
47
|
-
class Batch <
|
47
|
+
class Batch < StatsdTcp
|
48
48
|
|
49
49
|
extend Forwardable
|
50
50
|
def_delegators :@statsd,
|
@@ -57,7 +57,7 @@ class Statsd
|
|
57
57
|
|
58
58
|
attr_accessor :batch_size, :batch_byte_size, :flush_interval
|
59
59
|
|
60
|
-
# @param [
|
60
|
+
# @param [StatsdTcp] statsd requires a configured StatsdTcp instance
|
61
61
|
def initialize(statsd)
|
62
62
|
@statsd = statsd
|
63
63
|
@batch_size = statsd.batch_size
|
@@ -236,10 +236,10 @@ class Statsd
|
|
236
236
|
end
|
237
237
|
|
238
238
|
def send_to_socket(message)
|
239
|
-
self.class.logger.debug { "
|
239
|
+
self.class.logger.debug { "StatsdTcp: #{message}" } if self.class.logger
|
240
240
|
@socket.write(message.to_s + "\n")
|
241
241
|
rescue => boom
|
242
|
-
self.class.logger.error { "
|
242
|
+
self.class.logger.error { "StatsdTcp: #{boom.class} #{boom}" } if self.class.logger
|
243
243
|
nil
|
244
244
|
end
|
245
245
|
|
@@ -468,7 +468,7 @@ class Statsd
|
|
468
468
|
protected
|
469
469
|
|
470
470
|
def send_to_socket(message)
|
471
|
-
self.class.logger.debug { "
|
471
|
+
self.class.logger.debug { "StatsdTcp: #{message}" } if self.class.logger
|
472
472
|
|
473
473
|
retries = 0
|
474
474
|
n = 0
|
@@ -490,7 +490,7 @@ class Statsd
|
|
490
490
|
end
|
491
491
|
n
|
492
492
|
rescue => boom
|
493
|
-
self.class.logger.error { "
|
493
|
+
self.class.logger.error { "StatsdTcp: #{boom.class} #{boom}" } if self.class.logger
|
494
494
|
nil
|
495
495
|
end
|
496
496
|
|
data/spec/statsd_admin_spec.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe StatsdTcp::Admin do
|
4
4
|
|
5
5
|
before do
|
6
|
-
class
|
6
|
+
class StatsdTcp::Admin
|
7
7
|
o, $VERBOSE = $VERBOSE, nil
|
8
8
|
alias connect_old connect
|
9
9
|
def connect
|
@@ -12,12 +12,12 @@ describe Statsd::Admin do
|
|
12
12
|
end
|
13
13
|
$VERBOSE = o
|
14
14
|
end
|
15
|
-
@admin =
|
15
|
+
@admin = StatsdTcp::Admin.new('localhost', 1234)
|
16
16
|
@socket = @admin.instance_variable_set(:@socket, FakeTCPSocket.new)
|
17
17
|
end
|
18
18
|
|
19
19
|
after do
|
20
|
-
class
|
20
|
+
class StatsdTcp::Admin
|
21
21
|
o, $VERBOSE = $VERBOSE, nil
|
22
22
|
alias connect connect_old
|
23
23
|
$VERBOSE = o
|
@@ -31,7 +31,7 @@ describe Statsd::Admin do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should default the host to 127.0.0.1 and port to 8126" do
|
34
|
-
statsd =
|
34
|
+
statsd = StatsdTcp::Admin.new
|
35
35
|
statsd.host.must_equal '127.0.0.1'
|
36
36
|
statsd.port.must_equal 8126
|
37
37
|
end
|
data/spec/statsd_spec.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe StatsdTcp do
|
4
4
|
before do
|
5
|
-
class
|
5
|
+
class StatsdTcp
|
6
6
|
o, $VERBOSE = $VERBOSE, nil
|
7
7
|
alias connect_old connect
|
8
8
|
def connect
|
@@ -12,12 +12,12 @@ describe Statsd do
|
|
12
12
|
$VERBOSE = o
|
13
13
|
end
|
14
14
|
|
15
|
-
@statsd =
|
15
|
+
@statsd = StatsdTcp.new('localhost', 1234)
|
16
16
|
@socket = @statsd.instance_variable_set(:@socket, FakeUDPSocket.new)
|
17
17
|
end
|
18
18
|
|
19
19
|
after do
|
20
|
-
class
|
20
|
+
class StatsdTcp
|
21
21
|
o, $VERBOSE = $VERBOSE, nil
|
22
22
|
alias connect connect_old
|
23
23
|
$VERBOSE = o
|
@@ -31,7 +31,7 @@ describe Statsd do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should default the host to 127.0.0.1 and port to 8125" do
|
34
|
-
statsd =
|
34
|
+
statsd = StatsdTcp.new
|
35
35
|
statsd.host.must_equal '127.0.0.1'
|
36
36
|
statsd.port.must_equal 8125
|
37
37
|
end
|
@@ -287,18 +287,18 @@ describe Statsd do
|
|
287
287
|
|
288
288
|
describe "with logging" do
|
289
289
|
require 'stringio'
|
290
|
-
before {
|
290
|
+
before { StatsdTcp.logger = Logger.new(@log = StringIO.new)}
|
291
291
|
|
292
292
|
it "should write to the log in debug" do
|
293
|
-
|
293
|
+
StatsdTcp.logger.level = Logger::DEBUG
|
294
294
|
|
295
295
|
@statsd.increment('foobar')
|
296
296
|
|
297
|
-
@log.string.must_match "
|
297
|
+
@log.string.must_match "StatsdTcp: foobar:1|c"
|
298
298
|
end
|
299
299
|
|
300
300
|
it "should not write to the log unless debug" do
|
301
|
-
|
301
|
+
StatsdTcp.logger.level = Logger::INFO
|
302
302
|
|
303
303
|
@statsd.increment('foobar')
|
304
304
|
|
@@ -312,10 +312,10 @@ describe Statsd do
|
|
312
312
|
end
|
313
313
|
|
314
314
|
it "should replace ruby constant delimeter with graphite package name" do
|
315
|
-
class
|
316
|
-
@statsd.increment(
|
315
|
+
class StatsdTcp::SomeClass; end
|
316
|
+
@statsd.increment(StatsdTcp::SomeClass, 1)
|
317
317
|
|
318
|
-
@socket.recv.must_equal ['
|
318
|
+
@socket.recv.must_equal ['StatsdTcp.SomeClass:1|c']
|
319
319
|
end
|
320
320
|
|
321
321
|
describe "custom delimiter" do
|
@@ -324,10 +324,10 @@ describe Statsd do
|
|
324
324
|
end
|
325
325
|
|
326
326
|
it "should replace ruby constant delimiter with custom delimiter" do
|
327
|
-
class
|
328
|
-
@statsd.increment(
|
327
|
+
class StatsdTcp::SomeOtherClass; end
|
328
|
+
@statsd.increment(StatsdTcp::SomeOtherClass, 1)
|
329
329
|
|
330
|
-
@socket.recv.must_equal ['
|
330
|
+
@socket.recv.must_equal ['StatsdTcp-SomeOtherClass:1|c']
|
331
331
|
end
|
332
332
|
end
|
333
333
|
|
@@ -340,7 +340,7 @@ describe Statsd do
|
|
340
340
|
describe "handling socket errors" do
|
341
341
|
before do
|
342
342
|
require 'stringio'
|
343
|
-
|
343
|
+
StatsdTcp.logger = Logger.new(@log = StringIO.new)
|
344
344
|
@socket.instance_eval { def write(*) raise SocketError end }
|
345
345
|
end
|
346
346
|
|
@@ -350,7 +350,7 @@ describe Statsd do
|
|
350
350
|
|
351
351
|
it "should log socket errors" do
|
352
352
|
@statsd.increment('foobar')
|
353
|
-
@log.string.must_match '
|
353
|
+
@log.string.must_match 'StatsdTcp: SocketError'
|
354
354
|
end
|
355
355
|
end
|
356
356
|
|
@@ -450,7 +450,7 @@ describe Statsd do
|
|
450
450
|
end
|
451
451
|
|
452
452
|
it "should not flush to the socket if the backlog is empty" do
|
453
|
-
batch =
|
453
|
+
batch = StatsdTcp::Batch.new(@statsd)
|
454
454
|
batch.flush
|
455
455
|
@socket.recv.must_be :nil?
|
456
456
|
|
@@ -460,19 +460,19 @@ describe Statsd do
|
|
460
460
|
end
|
461
461
|
|
462
462
|
it "should support setting namespace for the underlying instance" do
|
463
|
-
batch =
|
463
|
+
batch = StatsdTcp::Batch.new(@statsd)
|
464
464
|
batch.namespace = 'ns'
|
465
465
|
@statsd.namespace.must_equal 'ns'
|
466
466
|
end
|
467
467
|
|
468
468
|
it "should support setting host for the underlying instance" do
|
469
|
-
batch =
|
469
|
+
batch = StatsdTcp::Batch.new(@statsd)
|
470
470
|
batch.host = '1.2.3.4'
|
471
471
|
@statsd.host.must_equal '1.2.3.4'
|
472
472
|
end
|
473
473
|
|
474
474
|
it "should support setting port for the underlying instance" do
|
475
|
-
batch =
|
475
|
+
batch = StatsdTcp::Batch.new(@statsd)
|
476
476
|
batch.port = 42
|
477
477
|
@statsd.port.must_equal 42
|
478
478
|
end
|
@@ -489,7 +489,7 @@ describe Statsd do
|
|
489
489
|
|
490
490
|
end
|
491
491
|
|
492
|
-
describe
|
492
|
+
describe StatsdTcp do
|
493
493
|
describe "with a real UDP socket" do
|
494
494
|
it "should actually send stuff over the socket" do
|
495
495
|
family = Addrinfo.udp(UDPSocket.getaddress('localhost'), 0).afamily
|
@@ -499,7 +499,7 @@ describe Statsd do
|
|
499
499
|
socket.bind(host, port)
|
500
500
|
port = socket.addr[1]
|
501
501
|
|
502
|
-
statsd =
|
502
|
+
statsd = StatsdTcp.new(host, port)
|
503
503
|
statsd.increment('foobar')
|
504
504
|
message = socket.recvfrom(16).first
|
505
505
|
message.must_equal 'foobar:1|c'
|
@@ -515,7 +515,7 @@ describe Statsd do
|
|
515
515
|
socket.bind(host, port)
|
516
516
|
port = socket.addr[1]
|
517
517
|
|
518
|
-
statsd =
|
518
|
+
statsd = StatsdTcp.new(host, port)
|
519
519
|
statsd.increment('foobar')
|
520
520
|
message = socket.recvfrom(16).first
|
521
521
|
message.must_equal 'foobar:1|c'
|
@@ -531,7 +531,7 @@ describe Statsd do
|
|
531
531
|
socket.bind(host, port)
|
532
532
|
port = socket.addr[1]
|
533
533
|
|
534
|
-
statsd =
|
534
|
+
statsd = StatsdTcp.new(host, port)
|
535
535
|
statsd.increment('foobar')
|
536
536
|
message = socket.recvfrom(16).first
|
537
537
|
message.must_equal 'foobar:1|c'
|
@@ -551,7 +551,7 @@ describe Statsd do
|
|
551
551
|
socket = nil
|
552
552
|
Thread.new { socket = server.accept }
|
553
553
|
|
554
|
-
statsd =
|
554
|
+
statsd = StatsdTcp.new(host, port, :tcp)
|
555
555
|
statsd.increment('foobar')
|
556
556
|
|
557
557
|
Timeout.timeout(5) do
|
@@ -575,7 +575,7 @@ describe Statsd do
|
|
575
575
|
socket = nil
|
576
576
|
Thread.new { socket = server.accept }
|
577
577
|
|
578
|
-
statsd =
|
578
|
+
statsd = StatsdTcp.new(host, port, :tcp)
|
579
579
|
statsd.increment('foobar')
|
580
580
|
|
581
581
|
Timeout.timeout(5) do
|
data/statsd-ruby-tcp.gemspec
CHANGED