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 CHANGED
@@ -1,7 +1,7 @@
1
- ---
2
- SHA1:
3
- metadata.gz: d1991db3ab55ae4ba74b693dfcb600b2d809cb96
4
- data.tar.gz: 1718da7a874889f7c94eb18e20db2ae937b97b36
5
- SHA512:
6
- metadata.gz: 793034d20062c3a9a3c5014191b7fdf3e118bd5271170ab35ff3e3aa66c7a2c23541669179820581700979e7441b6d1fe0137b451faca62501c8d0ab01bcf363
7
- data.tar.gz: 57b8807ef73cafd8c6ce406d987500b8f6b118ad02ef23f8a2875650801f0e380045b92348fbe7226bdd7fecb5703693b290f3ff7d3f2903d7b1116ccd2d2423
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 = Statsd::Client.new(:host => 'localhost', :port => 8125)
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 = Statsd::Client.new(:host => 'specialstats.host.example',
34
- :port => '8125',
35
- :resolve_always => false)
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 Statsd
6
- # initialize singleton instance in an initializer
7
- def self.create_instance(opts={})
8
- raise "Already initialized Statsd" if defined? @@instance
9
- @@instance ||= Client.new(opts)
10
- end
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
- # access singleton instance, which must have been initialized with #create_instance
13
- def self.instance
14
- raise "Statsd has not been initialized" unless @@instance
15
- @@instance
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 Client
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
- # +stats+ is a hash
77
- def gauge(stats)
78
- send_stats(stats.map { |s,val|
79
- if @prefix
80
- s = "#{@prefix}.#{s}"
81
- end
82
- "#{s}:#{val}|g"
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 < Statsd::Client
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
@@ -1,3 +1,3 @@
1
1
  $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
2
2
 
3
- require 'statsd'
3
+ require 'lookout/statsd'
@@ -1,22 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Statsd::Rails::ActionTimerFilter do
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
- Statsd::Rails::ActionTimerFilter.filter(controller) {}
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
- Statsd::Rails::ActionTimerFilter.filter(controller) {}
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
 
@@ -1,69 +1,83 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Statsd do
4
- describe '#create_instance' do
5
- before(:each) do
6
- # Make sure prior test hasn't already invoked create_instance
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
- after(:each) do
13
- Statsd.send(:remove_class_variable, :@@instance)
14
- end
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
- Statsd.create_instance
18
- Statsd.instance.should_not be nil
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
- Statsd.create_instance
20
+ described_class.create_instance
24
21
  end
25
22
  it 'should raise if called twice' do
26
- expect { Statsd.create_instance }.to raise_error
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 { Statsd.instance }.to raise_error
48
+ expect { described_class.instance }.to raise_error
35
49
  end
36
50
  end
37
51
  end
38
52
 
39
- describe Statsd::Client do
53
+ describe Lookout::StatsdClient do
40
54
  describe '#initialize' do
41
55
  it 'should work without arguments' do
42
- c = Statsd::Client.new
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 = Statsd::Client.new(:host => host)
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 = Statsd::Client.new(:port => port)
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 = Statsd::Client.new(:prefix => prefix)
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 = Statsd::Client.new(:resolve_always => lookup)
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 = Statsd::Client.new(:host => 'localhost')
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 = Statsd::Client.new(:host => 'statsd.example.example')
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 = Statsd::Client.new(:resolve_always => false)
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 = Statsd::Client.new(:resolve_always => true)
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) { Statsd::Client.new }
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) { Statsd::Client.new }
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) { Statsd::Client.new }
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) { Statsd::Client.new }
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) { Statsd::Client.new }
196
+ let(:c) { Lookout::StatsdClient.new }
183
197
 
184
- it 'should encode the values correctly' do
185
- c.should_receive(:send_stats).with do |array|
186
- array.should include('foo:1|g')
187
- array.should include('bar:2|g')
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
- it 'should prepend the prefix if it has one' do
193
- c.prefix = 'dev'
194
- c.should_receive(:send_stats).with(['dev.foo:1|g'])
195
- c.gauge('foo' => 1)
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) { Statsd::Client.new }
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
- Statsd::Batch.any_instance do |b|
250
+ Lookout::Batch.any_instance do |b|
205
251
  b.backlog.should_receive(:<<).exactly.twice
206
252
  end
207
- Statsd::Batch.any_instance.should_receive(:flush).and_call_original
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 Statsd::Batch do
215
- let(:c) { Statsd::Client.new :host => 'foo.com', :prefix => 'my.app', :port => 1234, :batch_size => 20 }
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "lookout-statsd"
5
- s.version = "2.0.3"
5
+ s.version = "3.0.0"
6
6
  s.platform = Gem::Platform::RUBY
7
7
 
8
8
  s.authors = ['R. Tyler Croy', 'Andrew Coldham', 'Ben VandenBos']
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: 2.0.3
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
- date: 2016-05-20 00:00:00.000000000 Z
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
- files:
22
- - ".gitignore"
23
- - ".travis.yml"
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
- require_paths:
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: '0'
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.5.1
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
+
@@ -1,3 +0,0 @@
1
- require 'statsd'
2
- counters = timers = []
3
- #Statsd::Graphite.flush_stats(counters,timers)