nsa 0.2.0 → 0.2.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.md +41 -2
 - data/lib/nsa/statsd/async_publisher.rb +20 -10
 - data/lib/nsa/version.rb +1 -1
 - metadata +1 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 86ff263225b7d77e1d87aa520fb425173097effd
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 723e3953685a9dc67a6c09dbda7e137e5f52a2cc
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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  
     | 
| 
      
 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 =  
     | 
| 
      
 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 
     | 
| 
      
 12 
     | 
    
         
            +
                      statsd_count(key, value)
         
     | 
| 
       11 
13 
     | 
    
         
             
                    end
         
     | 
| 
       12 
14 
     | 
    
         
             
                  end
         
     | 
| 
       13 
15 
     | 
    
         | 
| 
       14 
     | 
    
         
            -
                  def async_statsd_gauge(key, sample_rate =  
     | 
| 
      
 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 
     | 
| 
      
 20 
     | 
    
         
            +
                      statsd_gauge(key, value)
         
     | 
| 
       17 
21 
     | 
    
         
             
                    end
         
     | 
| 
       18 
22 
     | 
    
         
             
                  end
         
     | 
| 
       19 
23 
     | 
    
         | 
| 
       20 
     | 
    
         
            -
                  def async_statsd_set(key, sample_rate =  
     | 
| 
      
 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 
     | 
| 
      
 28 
     | 
    
         
            +
                      statsd_set(key, value)
         
     | 
| 
       23 
29 
     | 
    
         
             
                    end
         
     | 
| 
       24 
30 
     | 
    
         
             
                  end
         
     | 
| 
       25 
31 
     | 
    
         | 
| 
       26 
     | 
    
         
            -
                  def async_statsd_time(key, sample_rate =  
     | 
| 
      
 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,  
     | 
| 
      
 36 
     | 
    
         
            +
                      statsd_time(key, &block)
         
     | 
| 
       29 
37 
     | 
    
         
             
                    end
         
     | 
| 
       30 
38 
     | 
    
         
             
                  end
         
     | 
| 
       31 
39 
     | 
    
         | 
| 
       32 
     | 
    
         
            -
                  def async_statsd_timing(key, sample_rate =  
     | 
| 
      
 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 
     | 
| 
      
 44 
     | 
    
         
            +
                      statsd_timing(key, value)
         
     | 
| 
       35 
45 
     | 
    
         
             
                    end
         
     | 
| 
       36 
46 
     | 
    
         
             
                  end
         
     | 
| 
       37 
47 
     | 
    
         | 
    
        data/lib/nsa/version.rb
    CHANGED