lookout-statsd 2.0.3 → 3.0.0
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 +7 -7
- data/README.md +6 -6
- data/lib/{statsd.rb → lookout/statsd.rb} +54 -22
- data/spec/spec_helper.rb +1 -1
- data/spec/statsd/rails/action_timer_filter_spec.rb +6 -6
- data/spec/statsd_spec.rb +92 -46
- data/statsd.gemspec +1 -1
- metadata +29 -20
- data/lib/statsd/test.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
|
4
|
-
|
5
|
-
SHA512:
|
6
|
-
|
7
|
-
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
data.tar.gz: 7a997f961f7e49bc67f2987e0afc4b831630b244
|
4
|
+
metadata.gz: bcf20394e43bbf29ac3831d0f103f5cb1732be47
|
5
|
+
SHA512:
|
6
|
+
data.tar.gz: d7a5153f7f8453991f87d48c58e697914c1050ca91a9e68927d91ba84a835c47e8c996ece0cfbbc8eb9459e201f12913edf09464e317b2a0b13e4ab87e930b12
|
7
|
+
metadata.gz: c0b58cc86a12178b2a6e58d17d6de15ef2fac204ad6402e6da7744de10bed5c9182054b5a4aba4b13321c9951a34b6a34c1cc442a5bbf673beb6c74465d8a7d9
|
data/README.md
CHANGED
@@ -10,8 +10,8 @@ A very simple client to format and send metrics to a StatsD server.
|
|
10
10
|
In your client code:
|
11
11
|
|
12
12
|
require 'rubygems'
|
13
|
-
require 'statsd'
|
14
|
-
STATSD =
|
13
|
+
require 'lookout/statsd'
|
14
|
+
STATSD = Lookout::StatsdClient.new(:host => 'localhost', :port => 8125)
|
15
15
|
|
16
16
|
STATSD.increment('some_counter') # basic incrementing
|
17
17
|
STATSD.increment('system.nested_counter', 0.1) # incrementing with sampling (10%)
|
@@ -28,11 +28,11 @@ client will always resolve the address unless `host` is set to 'localhost' or
|
|
28
28
|
'127.0.0.1'.
|
29
29
|
|
30
30
|
require 'rubygems'
|
31
|
-
require 'statsd'
|
31
|
+
require 'lookout/statsd'
|
32
32
|
|
33
|
-
STATSD =
|
34
|
-
|
35
|
-
|
33
|
+
STATSD = Lookout::StatsdClient.new(:host => 'specialstats.host.example',
|
34
|
+
:port => '8125',
|
35
|
+
:resolve_always => false)
|
36
36
|
|
37
37
|
STATSD.increment('some_counter') # basic incrementing
|
38
38
|
|
@@ -2,20 +2,41 @@ require 'socket'
|
|
2
2
|
require 'resolv'
|
3
3
|
require 'forwardable'
|
4
4
|
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
module Lookout
|
6
|
+
class Statsd
|
7
|
+
# initialize singleton instance to be an instance of
|
8
|
+
# +Lookout::StatsdClient+, with the given options
|
9
|
+
def self.create_instance(opts={})
|
10
|
+
raise "Already initialized Statsd" if instance_set?
|
11
|
+
@@instance ||= StatsdClient.new(opts)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Explicitly set singleton instance. The instance must follow the
|
15
|
+
# same API as +Lookout::StatsdClient+
|
16
|
+
def self.set_instance(instance)
|
17
|
+
raise "Already initialized Statsd" if instance_set?
|
18
|
+
@@instance = instance
|
19
|
+
end
|
11
20
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
21
|
+
# Clear singleton instance, for use in testing ONLY
|
22
|
+
def self.clear_instance
|
23
|
+
@@instance = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
# Check if the instance has been set
|
27
|
+
def self.instance_set?
|
28
|
+
defined?(@@instance) && !!@@instance
|
29
|
+
end
|
30
|
+
|
31
|
+
# Access singleton instance, which must have been initialized with
|
32
|
+
# .create_instance or .set_instance
|
33
|
+
def self.instance
|
34
|
+
raise "Statsd has not been initialized" unless instance_set?
|
35
|
+
@@instance
|
36
|
+
end
|
16
37
|
end
|
17
38
|
|
18
|
-
class
|
39
|
+
class StatsdClient
|
19
40
|
attr_accessor :host, :port, :prefix, :resolve_always, :batch_size
|
20
41
|
|
21
42
|
def initialize(opts={})
|
@@ -73,14 +94,26 @@ module Statsd
|
|
73
94
|
send_stats(stats.map { |s| "#{p}#{s}:#{delta}|c" }, sample_rate)
|
74
95
|
end
|
75
96
|
|
76
|
-
# +
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
97
|
+
# +stat_or_stats+ may either be a Hash OR a String. If it's a
|
98
|
+
# String, then value must be specified. Other statsd client gems
|
99
|
+
# have mostly standardized on using the String+value format, but
|
100
|
+
# this gem traditionally supported just a Hash. This now supports
|
101
|
+
# both for compatibility.
|
102
|
+
def gauge(stat_or_stats, value=nil, opts=nil)
|
103
|
+
# Can't use duck-typing here, since String responds to :map
|
104
|
+
if stat_or_stats.is_a?(Hash)
|
105
|
+
send_stats(stat_or_stats.map { |s,val|
|
106
|
+
if @prefix
|
107
|
+
s = "#{@prefix}.#{s}"
|
108
|
+
end
|
109
|
+
"#{s}:#{val}|g"
|
110
|
+
})
|
111
|
+
else
|
112
|
+
if @prefix
|
113
|
+
stat_or_stats = "#{@prefix}.#{stat_or_stats}"
|
114
|
+
end
|
115
|
+
send_stats("#{stat_or_stats}:#{value}|g")
|
116
|
+
end
|
84
117
|
end
|
85
118
|
|
86
119
|
def send_data(*args)
|
@@ -170,7 +203,7 @@ module Statsd
|
|
170
203
|
# that some care is taken if setting very large batch sizes. If the batch size
|
171
204
|
# exceeds the allowed packet size for UDP on your network, communication
|
172
205
|
# troubles may occur and data will be lost.
|
173
|
-
class Batch <
|
206
|
+
class Batch < Lookout::StatsdClient
|
174
207
|
|
175
208
|
attr_accessor :batch_size
|
176
209
|
|
@@ -228,9 +261,8 @@ module Statsd
|
|
228
261
|
# Use params[:controller] insted of controller.controller_name to get full path.
|
229
262
|
controller_name = controller.params[:controller].gsub("/", ".")
|
230
263
|
key = "requests.#{controller_name}.#{controller.params[:action]}"
|
231
|
-
Statsd.instance.timing(key, &block)
|
264
|
+
Lookout::Statsd.instance.timing(key, &block)
|
232
265
|
end
|
233
266
|
end
|
234
267
|
end
|
235
|
-
|
236
268
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Lookout::Rails::ActionTimerFilter do
|
4
4
|
|
5
5
|
describe ".filter" do
|
6
6
|
before(:all) do
|
7
|
-
Statsd.create_instance
|
7
|
+
Lookout::Statsd.create_instance
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should log the appropriate data with simple controller" do
|
11
11
|
controller = mock_controller('control', 'act')
|
12
|
-
Statsd.instance.should_receive(:timing).with("requests.control.act")
|
13
|
-
|
12
|
+
Lookout::Statsd.instance.should_receive(:timing).with("requests.control.act")
|
13
|
+
Lookout::Rails::ActionTimerFilter.filter(controller) {}
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should log the appropriate data with complex controller" do
|
17
17
|
controller = mock_controller('api/v1/control', 'act')
|
18
|
-
Statsd.instance.should_receive(:timing).with("requests.api.v1.control.act")
|
19
|
-
|
18
|
+
Lookout::Statsd.instance.should_receive(:timing).with("requests.api.v1.control.act")
|
19
|
+
Lookout::Rails::ActionTimerFilter.filter(controller) {}
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
data/spec/statsd_spec.rb
CHANGED
@@ -1,69 +1,83 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Statsd do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
if Statsd.class_variable_defined?(:@@instance)
|
8
|
-
Statsd.send(:remove_class_variable, :@@instance)
|
9
|
-
end
|
10
|
-
end
|
3
|
+
describe Lookout::Statsd do
|
4
|
+
before(:each) do
|
5
|
+
described_class.clear_instance
|
6
|
+
end
|
11
7
|
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
after(:each) do
|
9
|
+
described_class.clear_instance
|
10
|
+
end
|
15
11
|
|
12
|
+
describe '.create_instance' do
|
16
13
|
it 'should create an instance' do
|
17
|
-
|
18
|
-
|
14
|
+
described_class.create_instance
|
15
|
+
described_class.instance.should_not be nil
|
19
16
|
end
|
20
17
|
|
21
18
|
context 'if an instance has been created' do
|
22
19
|
before :each do
|
23
|
-
|
20
|
+
described_class.create_instance
|
24
21
|
end
|
25
22
|
it 'should raise if called twice' do
|
26
|
-
expect {
|
23
|
+
expect { described_class.create_instance }.to raise_error
|
27
24
|
end
|
28
25
|
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '.set_instance' do
|
29
|
+
let(:instance) { double('Statsd') }
|
30
|
+
|
31
|
+
it 'should set instance' do
|
32
|
+
described_class.set_instance(instance)
|
33
|
+
expect(described_class.instance).to eq instance
|
34
|
+
end
|
29
35
|
|
36
|
+
context 'if an instance has been created' do
|
37
|
+
before :each do
|
38
|
+
described_class.set_instance(instance)
|
39
|
+
end
|
40
|
+
it 'should raise if called twice' do
|
41
|
+
expect { described_class.set_instance(instance) }.to raise_error
|
42
|
+
end
|
43
|
+
end
|
30
44
|
end
|
31
45
|
|
32
46
|
describe '#instance' do
|
33
47
|
it 'should raise if not created' do
|
34
|
-
expect {
|
48
|
+
expect { described_class.instance }.to raise_error
|
35
49
|
end
|
36
50
|
end
|
37
51
|
end
|
38
52
|
|
39
|
-
describe
|
53
|
+
describe Lookout::StatsdClient do
|
40
54
|
describe '#initialize' do
|
41
55
|
it 'should work without arguments' do
|
42
|
-
c =
|
56
|
+
c = Lookout::StatsdClient.new
|
43
57
|
c.should_not be nil
|
44
58
|
end
|
45
59
|
|
46
60
|
it 'should accept a :host keyword argument' do
|
47
61
|
host = 'zombo.com'
|
48
|
-
c =
|
62
|
+
c = Lookout::StatsdClient.new(:host => host)
|
49
63
|
c.host.should match(host)
|
50
64
|
end
|
51
65
|
|
52
66
|
it 'should accept a :port keyword argument' do
|
53
67
|
port = 1337
|
54
|
-
c =
|
68
|
+
c = Lookout::StatsdClient.new(:port => port)
|
55
69
|
c.port.should == port
|
56
70
|
end
|
57
71
|
|
58
72
|
it 'should accept a :prefix keyword argument' do
|
59
73
|
prefix = 'dev'
|
60
|
-
c =
|
74
|
+
c = Lookout::StatsdClient.new(:prefix => prefix)
|
61
75
|
c.prefix.should match(prefix)
|
62
76
|
end
|
63
77
|
|
64
78
|
it 'should accept a :resolve_always keyword argument' do
|
65
79
|
lookup = false
|
66
|
-
c =
|
80
|
+
c = Lookout::StatsdClient.new(:resolve_always => lookup)
|
67
81
|
c.resolve_always.should be(lookup)
|
68
82
|
end
|
69
83
|
|
@@ -71,14 +85,14 @@ describe Statsd::Client do
|
|
71
85
|
|
72
86
|
context 'when host is localhost or 127.0.0.1' do
|
73
87
|
it ':resolve_always should default to false' do
|
74
|
-
c =
|
88
|
+
c = Lookout::StatsdClient.new(:host => 'localhost')
|
75
89
|
c.resolve_always.should be(false)
|
76
90
|
end
|
77
91
|
end
|
78
92
|
|
79
93
|
context 'when host is not local' do
|
80
94
|
it ':resolve_always should default to true' do
|
81
|
-
c =
|
95
|
+
c = Lookout::StatsdClient.new(:host => 'statsd.example.example')
|
82
96
|
c.resolve_always.should be(true)
|
83
97
|
end
|
84
98
|
end
|
@@ -90,14 +104,14 @@ describe Statsd::Client do
|
|
90
104
|
describe '#send_stats' do
|
91
105
|
|
92
106
|
it 'should use cached resolve address when :resolve_always is false' do
|
93
|
-
c =
|
107
|
+
c = Lookout::StatsdClient.new(:resolve_always => false)
|
94
108
|
sock = c.instance_variable_get(:@socket)
|
95
109
|
expect(sock).to receive(:send).with(anything, 0)
|
96
110
|
c.increment('foo')
|
97
111
|
end
|
98
112
|
|
99
113
|
it 'should always resolve address when :resolve_always is true' do
|
100
|
-
c =
|
114
|
+
c = Lookout::StatsdClient.new(:resolve_always => true)
|
101
115
|
sock = c.instance_variable_get(:@socket)
|
102
116
|
expect(sock).to receive(:send).with(anything, 0, c.host, c.port)
|
103
117
|
c.increment('foo')
|
@@ -105,7 +119,7 @@ describe Statsd::Client do
|
|
105
119
|
end
|
106
120
|
|
107
121
|
describe '#timing' do
|
108
|
-
let(:c) {
|
122
|
+
let(:c) { Lookout::StatsdClient.new }
|
109
123
|
|
110
124
|
it 'should pass the sample rate along' do
|
111
125
|
sample = 10
|
@@ -145,7 +159,7 @@ describe Statsd::Client do
|
|
145
159
|
end
|
146
160
|
|
147
161
|
describe '#increment' do
|
148
|
-
let(:c) {
|
162
|
+
let(:c) { Lookout::StatsdClient.new }
|
149
163
|
|
150
164
|
it 'should update the counter by 1' do
|
151
165
|
c.should_receive(:update_counter).with('foo', 1, anything())
|
@@ -154,7 +168,7 @@ describe Statsd::Client do
|
|
154
168
|
end
|
155
169
|
|
156
170
|
describe '#decrement' do
|
157
|
-
let(:c) {
|
171
|
+
let(:c) { Lookout::StatsdClient.new }
|
158
172
|
|
159
173
|
it 'should update the counter by -1' do
|
160
174
|
c.should_receive(:update_counter).with('foo', -1, anything())
|
@@ -163,7 +177,7 @@ describe Statsd::Client do
|
|
163
177
|
end
|
164
178
|
|
165
179
|
describe '#update_counter' do
|
166
|
-
let(:c) {
|
180
|
+
let(:c) { Lookout::StatsdClient.new }
|
167
181
|
|
168
182
|
it 'should prepend the prefix if it has one' do
|
169
183
|
c.prefix = 'dev'
|
@@ -179,40 +193,72 @@ describe Statsd::Client do
|
|
179
193
|
end
|
180
194
|
|
181
195
|
describe '#gauge' do
|
182
|
-
let(:c) {
|
196
|
+
let(:c) { Lookout::StatsdClient.new }
|
183
197
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
198
|
+
context "called with a Hash" do
|
199
|
+
it 'should encode the values correctly' do
|
200
|
+
c.should_receive(:send_stats).with do |array|
|
201
|
+
array.should include('foo:1|g')
|
202
|
+
array.should include('bar:2|g')
|
203
|
+
end
|
204
|
+
c.gauge('foo' => 1, 'bar' => 2)
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'should prepend the prefix if it has one' do
|
208
|
+
c.prefix = 'dev'
|
209
|
+
c.should_receive(:send_stats).with(['dev.foo:1|g'])
|
210
|
+
c.gauge('foo' => 1)
|
188
211
|
end
|
189
|
-
c.gauge('foo' => 1, 'bar' => 2)
|
190
212
|
end
|
191
213
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
214
|
+
context "called with String+Value" do
|
215
|
+
context "without specifying options" do
|
216
|
+
it 'should encode the values correctly' do
|
217
|
+
c.should_receive(:send_stats).with('foo:1|g')
|
218
|
+
c.gauge('foo', 1)
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'should prepend the prefix if it has one' do
|
222
|
+
c.prefix = 'dev'
|
223
|
+
c.should_receive(:send_stats).with('dev.foo:1|g')
|
224
|
+
c.gauge('foo', 1)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context "specifying options" do
|
229
|
+
let(:opts) { {:ignored => true} }
|
230
|
+
|
231
|
+
it 'should encode the values correctly' do
|
232
|
+
c.should_receive(:send_stats).with('foo:1|g')
|
233
|
+
c.gauge('foo', 1, opts)
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'should prepend the prefix if it has one' do
|
237
|
+
c.prefix = 'dev'
|
238
|
+
c.should_receive(:send_stats).with('dev.foo:1|g')
|
239
|
+
c.gauge('foo', 1, opts)
|
240
|
+
end
|
241
|
+
end
|
196
242
|
end
|
197
243
|
end
|
198
244
|
|
199
245
|
describe '#batch' do
|
200
|
-
let(:c) {
|
246
|
+
let(:c) { Lookout::StatsdClient.new }
|
201
247
|
subject { c.batch { |b| b.increment('foo'); b.increment('bar'); } }
|
202
248
|
|
203
249
|
it 'should take a block and put increments into a buffer' do
|
204
|
-
|
250
|
+
Lookout::Batch.any_instance do |b|
|
205
251
|
b.backlog.should_receive(:<<).exactly.twice
|
206
252
|
end
|
207
|
-
|
253
|
+
Lookout::Batch.any_instance.should_receive(:flush).and_call_original
|
208
254
|
c.should_receive(:send_data).once
|
209
255
|
subject
|
210
256
|
end
|
211
257
|
end
|
212
258
|
end
|
213
259
|
|
214
|
-
describe
|
215
|
-
let(:c) {
|
260
|
+
describe Lookout::Batch do
|
261
|
+
let(:c) { Lookout::StatsdClient.new :host => 'foo.com', :prefix => 'my.app', :port => 1234, :batch_size => 20 }
|
216
262
|
|
217
263
|
it 'should delegate fields correctly' do
|
218
264
|
c.batch do |b|
|
@@ -318,4 +364,4 @@ describe Statsd::Batch do
|
|
318
364
|
end
|
319
365
|
end
|
320
366
|
end
|
321
|
-
end
|
367
|
+
end
|
data/statsd.gemspec
CHANGED
metadata
CHANGED
@@ -1,32 +1,36 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: lookout-statsd
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- R. Tyler Croy
|
8
8
|
- Andrew Coldham
|
9
9
|
- Ben VandenBos
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
|
13
|
+
|
14
|
+
date: 2016-07-13 00:00:00 Z
|
14
15
|
dependencies: []
|
16
|
+
|
15
17
|
description: A simple ruby statsd client.
|
16
|
-
email:
|
18
|
+
email:
|
17
19
|
- rtyler.croy@mylookout.com
|
18
20
|
executables: []
|
21
|
+
|
19
22
|
extensions: []
|
23
|
+
|
20
24
|
extra_rdoc_files: []
|
21
|
-
|
22
|
-
|
23
|
-
-
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- .travis.yml
|
24
29
|
- Gemfile
|
25
30
|
- Guardfile
|
26
31
|
- README.md
|
27
32
|
- Rakefile
|
28
|
-
- lib/statsd.rb
|
29
|
-
- lib/statsd/test.rb
|
33
|
+
- lib/lookout/statsd.rb
|
30
34
|
- netcat-example.sh
|
31
35
|
- spec/spec_helper.rb
|
32
36
|
- spec/statsd/rails/action_timer_filter_spec.rb
|
@@ -34,25 +38,30 @@ files:
|
|
34
38
|
- statsd.gemspec
|
35
39
|
homepage: https://github.com/lookout/statsd
|
36
40
|
licenses: []
|
41
|
+
|
37
42
|
metadata: {}
|
43
|
+
|
38
44
|
post_install_message:
|
39
45
|
rdoc_options: []
|
40
|
-
|
46
|
+
|
47
|
+
require_paths:
|
41
48
|
- lib
|
42
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
-
requirements:
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
44
51
|
- - ">="
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version:
|
47
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
-
requirements:
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
49
56
|
- - ">="
|
50
|
-
- !ruby/object:Gem::Version
|
57
|
+
- !ruby/object:Gem::Version
|
51
58
|
version: 1.3.6
|
52
59
|
requirements: []
|
60
|
+
|
53
61
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.
|
62
|
+
rubygems_version: 2.0.16
|
55
63
|
signing_key:
|
56
64
|
specification_version: 4
|
57
65
|
summary: Ruby statsd client.
|
58
66
|
test_files: []
|
67
|
+
|
data/lib/statsd/test.rb
DELETED