nsa 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1a996fa5af6727cff0da62c3bea2c03121e0e39
4
- data.tar.gz: b60c21f202615307e3c5c2d0bda20db393fcd79d
3
+ metadata.gz: 86ff263225b7d77e1d87aa520fb425173097effd
4
+ data.tar.gz: 723e3953685a9dc67a6c09dbda7e137e5f52a2cc
5
5
  SHA512:
6
- metadata.gz: 22112240d62da1536e1dc24e3f9c904d0c65ab6d8152376a2a124f9f21a6243b7961ed935577f92f3b02c2da460b4001ed51c9ac09aa180420b9dc56ac839e6f
7
- data.tar.gz: 51a0f98d21e227da7ee8a65df55e272b828e906066d99959c7a78bccf66d540ac59ffd8ca7069f9e850f9d1b3fedbeb742c7e3230c2fbc7dd3a6f7dac7c8e55d
6
+ metadata.gz: 41a998fa86f209cc2803eee4f233e7c94ff39e465cf920f37c09cb76245091fa04d15b6023f592e64a9a4abc7c30de322c06dbc603753473a30f1d41ba4f7330
7
+ data.tar.gz: 99da7edeabcc395718a7470c24003ed7dd79fef900f138b6ae88b9938b0aa49b8e30ba06be83e9fe46db8a3f5c4b2147eff6fc8d6d2fd9c5cedf341e776f4812
data/README.md CHANGED
@@ -109,8 +109,10 @@ Metrics recorded:
109
109
  Writing your own collector is very simple. To take advantage of the keyspace handling you must:
110
110
 
111
111
  1. Create an object/module which responds to `collect`, taking the `key_prefix` as its only argument.
112
- 2. Include or extend your class/module with `NSA::Statsd::Publisher`.
113
- 3. Call any of the `statsd_*` prefixed methods provided by the `Publisher`:
112
+ 2. Include or extend your class/module with `NSA::Statsd::Publisher` or `NSA::Statsd::Publisher`.
113
+ 3. Call any of the `statsd_*` prefixed methods provided by the included Publisher:
114
+
115
+ __`Publisher` methods:__
114
116
 
115
117
  + `statsd_count(key, value = 1, sample_rate = nil)`
116
118
  + `statsd_decrement(key, sample_rate = nil)`
@@ -120,10 +122,23 @@ Writing your own collector is very simple. To take advantage of the keyspace han
120
122
  + `statsd_time(key, sample_rate = nil, &block)`
121
123
  + `statsd_timing(key, value = 1, sample_rate = nil)`
122
124
 
125
+ __`AsyncPublisher` methods:__
126
+
127
+ + `async_statsd_count(key, sample_rate = nil, &block)`
128
+ + `async_statsd_gauge(key, sample_rate = nil, &block)`
129
+ + `async_statsd_set(key, sample_rate = nil, &block)`
130
+ + `async_statsd_time(key, sample_rate = nil, &block)`
131
+ + `async_statsd_timing(key, sample_rate = nil, &block)`
132
+
133
+ ___Note:___ When using the `AsyncPublisher`, the value is derived from the block. This is useful
134
+ when the value is not near at hand and has a relatively high cost to compute (e.g. db query)
135
+ and you don't want your current thread to wait.
136
+
123
137
  For example, first define your collector. Our (very naive) example will write
124
138
  a gauge metric every 10 seconds of the User count in the db.
125
139
 
126
140
  ```ruby
141
+ # Publishing User.count gauge using a collector
127
142
  module UsersCollector
128
143
  extend ::NSA::Statsd::Publisher
129
144
 
@@ -148,6 +163,30 @@ NSA.inform_statsd($statsd) do |informant|
148
163
  end
149
164
  ```
150
165
 
166
+ You could also implement the provided example not as a Collector, but using
167
+ `AsyncPublisher` directly in your ActiveRecord model:
168
+
169
+ ```ruby
170
+ # Publishing User.count gauge using AsyncPublisher methods
171
+ class User < ActiveRecord::Base
172
+ include NSA::Statsd::AsyncPublisher
173
+
174
+ after_commit :write_count_gauge, :on => [ :create, :destroy ]
175
+
176
+ # ...
177
+
178
+ private
179
+
180
+ def write_count_gauge
181
+ async_statsd_gauge("models.User.all.count") { ::User.count }
182
+ end
183
+
184
+ end
185
+ ```
186
+
187
+ Using this technique, publishing the `User.count` stat gauge will not hold up
188
+ the thread responsible for creating the record (and processing more callbacks).
189
+
151
190
  ## Development
152
191
 
153
192
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -5,33 +5,43 @@ module NSA
5
5
  module AsyncPublisher
6
6
  include ::NSA::Statsd::Publisher
7
7
 
8
- def async_statsd_count(key, sample_rate = nil, &block)
8
+ def async_statsd_count(key, sample_rate = 1, &block)
9
+ return unless sample_rate == 1 || rand < sample_rate
10
+
9
11
  ::Concurrent::Promise.execute(&block).then do |value|
10
- statsd_count(key, value, sample_rate)
12
+ statsd_count(key, value)
11
13
  end
12
14
  end
13
15
 
14
- def async_statsd_gauge(key, sample_rate = nil, &block)
16
+ def async_statsd_gauge(key, sample_rate = 1, &block)
17
+ return unless sample_rate == 1 || rand < sample_rate
18
+
15
19
  ::Concurrent::Promise.execute(&block).then do |value|
16
- statsd_gauge(key, value, sample_rate)
20
+ statsd_gauge(key, value)
17
21
  end
18
22
  end
19
23
 
20
- def async_statsd_set(key, sample_rate = nil, &block)
24
+ def async_statsd_set(key, sample_rate = 1, &block)
25
+ return unless sample_rate == 1 || rand < sample_rate
26
+
21
27
  ::Concurrent::Promise.execute(&block).then do |value|
22
- statsd_set(key, value, sample_rate)
28
+ statsd_set(key, value)
23
29
  end
24
30
  end
25
31
 
26
- def async_statsd_time(key, sample_rate = nil, &block)
32
+ def async_statsd_time(key, sample_rate = 1, &block)
33
+ return unless sample_rate == 1 || rand < sample_rate
34
+
27
35
  ::Concurrent::Future.execute do
28
- statsd_time(key, sample_rate, &block)
36
+ statsd_time(key, &block)
29
37
  end
30
38
  end
31
39
 
32
- def async_statsd_timing(key, sample_rate = nil, &block)
40
+ def async_statsd_timing(key, sample_rate = 1, &block)
41
+ return unless sample_rate == 1 || rand < sample_rate
42
+
33
43
  ::Concurrent::Promise.execute(&block).then do |value|
34
- statsd_timing(key, value, sample_rate)
44
+ statsd_timing(key, value)
35
45
  end
36
46
  end
37
47
 
@@ -1,3 +1,3 @@
1
1
  module NSA
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nsa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - BJ Neilsen