nsa 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -15
- data/lib/nsa/statsd/subscriber.rb +11 -1
- 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: e00f8a3beffa73912bccaa929bbfd50fbfc89775
|
4
|
+
data.tar.gz: 6f3bc08197a8975d66bf970a74769d7ba74d2506
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d36ef682fb0278c9e8b1e54e051cf01df1f0c499a437889d204e339552aae977dd1398724f9dca174a98f830f0bc1ec3793f0be25d1f942e172fe7c3f090b7aa
|
7
|
+
data.tar.gz: db4e5a11691f10d8bb9e123d59502de8bf85280b61809d660e54f632325678e23c49897b3bdebfa7dd6862f42f4bc17a73a5145f98903830fb0bae22a96cbc31
|
data/README.md
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
Listen to Rails `ActiveSupport::Notifications` and deliver to a [Statsd](https://github.com/reinh/statsd) backend.
|
4
4
|
This gem also supports writing your own custom collectors.
|
5
5
|
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/nsa.svg)](https://badge.fury.io/rb/nsa)
|
7
|
+
[![Build Status](https://travis-ci.org/localshred/nsa.svg)](https://travis-ci.org/localshred/nsa)
|
8
|
+
|
6
9
|
## Installation
|
7
10
|
|
8
11
|
Add this line to your application's Gemfile:
|
@@ -29,12 +32,12 @@ collectors you'd like to run. Each `collect` method specifies a Collector to use
|
|
29
32
|
and the additional key namespace.
|
30
33
|
|
31
34
|
```ruby
|
32
|
-
|
35
|
+
statsd = ::Statsd.new(ENV["STATSD_HOST"], ENV["STATSD_PORT"])
|
33
36
|
application_name = ::Rails.application.class.parent_name.underscore
|
34
37
|
application_env = ENV["PLATFORM_ENV"] || ::Rails.env
|
35
|
-
|
38
|
+
statsd.namespace = [ application_name, application_env ].join(".")
|
36
39
|
|
37
|
-
::NSA.inform_statsd(
|
40
|
+
::NSA.inform_statsd(statsd) do |informant|
|
38
41
|
# Load :action_controller collector with a key prefix of :web
|
39
42
|
informant.collect(:action_controller, :web)
|
40
43
|
informant.collect(:active_record, :db)
|
@@ -107,8 +110,18 @@ Writing your own collector is very simple. To take advantage of the keyspace han
|
|
107
110
|
|
108
111
|
1. Create an object/module which responds to `collect`, taking the `key_prefix` as its only argument.
|
109
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`:
|
114
|
+
|
115
|
+
+ `statsd_count(key, value = 1, sample_rate = nil)`
|
116
|
+
+ `statsd_decrement(key, sample_rate = nil)`
|
117
|
+
+ `statsd_gauge(key, value = 1, sample_rate = nil)`
|
118
|
+
+ `statsd_increment(key, sample_rate = nil)`
|
119
|
+
+ `statsd_set(key, value = 1, sample_rate = nil)`
|
120
|
+
+ `statsd_time(key, sample_rate = nil, &block)`
|
121
|
+
+ `statsd_timing(key, value = 1, sample_rate = nil)`
|
110
122
|
|
111
|
-
For example
|
123
|
+
For example, first define your collector. Our (very naive) example will write
|
124
|
+
a gauge metric every 10 seconds of the User count in the db.
|
112
125
|
|
113
126
|
```ruby
|
114
127
|
module UsersCollector
|
@@ -123,9 +136,11 @@ module UsersCollector
|
|
123
136
|
end
|
124
137
|
```
|
125
138
|
|
126
|
-
Then let the informant know about it:
|
139
|
+
Then let the informant know about it in some initializer:
|
127
140
|
|
128
141
|
```ruby
|
142
|
+
# file: config/initializers/statsd.rb
|
143
|
+
|
129
144
|
# $statsd =
|
130
145
|
NSA.inform_statsd($statsd) do |informant|
|
131
146
|
# ...
|
@@ -133,16 +148,6 @@ NSA.inform_statsd($statsd) do |informant|
|
|
133
148
|
end
|
134
149
|
```
|
135
150
|
|
136
|
-
The `NSA::Statsd::Publisher` module exposes the following methods:
|
137
|
-
|
138
|
-
+ `statsd_count(key, value = 1, sample_rate = nil)`
|
139
|
-
+ `statsd_decrement(key, sample_rate = nil)`
|
140
|
-
+ `statsd_gauge(key, value = 1, sample_rate = nil)`
|
141
|
-
+ `statsd_increment(key, sample_rate = nil)`
|
142
|
-
+ `statsd_set(key, value = 1, sample_rate = nil)`
|
143
|
-
+ `statsd_time(key, sample_rate = nil, &block)`
|
144
|
-
+ `statsd_timing(key, value = 1, sample_rate = nil)`
|
145
|
-
|
146
151
|
## Development
|
147
152
|
|
148
153
|
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.
|
@@ -4,8 +4,12 @@ module NSA
|
|
4
4
|
module Statsd
|
5
5
|
module Subscriber
|
6
6
|
|
7
|
+
EXPECTED_RESPONDABLE_METHODS = %i( count decrement gauge increment set time timing ).freeze
|
8
|
+
|
7
9
|
def statsd_subscribe(backend)
|
8
|
-
|
10
|
+
unless backend_valid?(backend)
|
11
|
+
fail "Backend must respond to the following methods:\n\t#{EXPECTED_RESPONDABLE_METHODS.join(", ")}"
|
12
|
+
end
|
9
13
|
|
10
14
|
::ActiveSupport::Notifications.subscribe(/.statsd$/) do |name, start, finish, id, payload|
|
11
15
|
__send_event_to_statsd(backend, name, start, finish, id, payload)
|
@@ -28,6 +32,12 @@ module NSA
|
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
35
|
+
def backend_valid?(backend)
|
36
|
+
EXPECTED_RESPONDABLE_METHODS.all? do |method_name|
|
37
|
+
backend.respond_to?(method_name)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
31
41
|
end
|
32
42
|
end
|
33
43
|
end
|
data/lib/nsa/version.rb
CHANGED