statsd-instrument 1.6.1 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +66 -23
- data/lib/statsd/instrument.rb +8 -0
- data/lib/statsd/instrument/version.rb +1 -1
- data/test/statsd_test.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4332f173929c941bac2a915fed3be82bc447dec3
|
4
|
+
data.tar.gz: 52aa66f9144a09740da7767d59e4846ff38d5ffe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57f6a5a32129aa3b62d76565e42d941bba4c83602454f4e05fb1383b201b826a1fd6aa120d74da5e1cd10340a528ed51ab0bcfb3e7eedc343ad8f3f2571033b9
|
7
|
+
data.tar.gz: 8b76f2cdf7d83ce7dd9054b882129797b5d884cabd16c699fb6e29c7e3692723ecb6d361b6df91dca4b53be6e0b9de755735fd0df356fc6c880522f79a88cd18
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -19,16 +19,29 @@ This is the same as what Etsy uses (mentioned in the README for http://github.co
|
|
19
19
|
## Configuration
|
20
20
|
|
21
21
|
``` ruby
|
22
|
-
|
23
|
-
|
22
|
+
# The UDP endpoint to which you want to submit your metrics.
|
23
|
+
# This is set to the environment variable STATSD_ADDR if it is set.
|
24
|
+
StatsD.server = 'statsd.myservice.com:8125'
|
25
|
+
|
26
|
+
# Events are only actually submitted in production mode. For any other value, thewy are logged instead
|
27
|
+
# This value is set by to the value of the RAILS_ENV or RACK_ENV environment variable if it is set.
|
24
28
|
StatsD.mode = :production
|
25
|
-
StatsD.prefix = 'my_app' # An optional prefix to be added to each stat.
|
26
|
-
StatsD.default_sample_rate = 0.1 # Sample 10% of events. By default all events are reported.
|
27
|
-
```
|
28
29
|
|
29
|
-
|
30
|
+
# Logger to which commands are logged when not in :production mode.
|
31
|
+
# In production only errors are logged to the console.
|
32
|
+
StatsD.logger = Rails.logger
|
33
|
+
|
34
|
+
# An optional prefix to be added to each stat.
|
35
|
+
StatsD.prefix = 'my_app'
|
36
|
+
|
37
|
+
# Sample 10% of events. By default all events are reported, which may overload your network or server.
|
38
|
+
# You can, and should vary this on a per metric basis, depending on frequency and accuracy requirements
|
39
|
+
StatsD.default_sample_rate = 0.1
|
40
|
+
|
41
|
+
|
42
|
+
```
|
30
43
|
|
31
|
-
There are several implementations of StatsD out there, all with slight protocol variations. You can this library to use the proper protocol by informing it about what implementation you use. By default, it will use the protocol of the original Etsy implementation.
|
44
|
+
There are several implementations of StatsD out there, all with slight protocol variations. You can this library to use the proper protocol by informing it about what implementation you use. By default, it will use the `STATSD_IMPLEMENTATION` environment variable, if it is not set it will use the protocol of the original Etsy implementation.
|
32
45
|
|
33
46
|
```
|
34
47
|
StatsD.implementation = :datadog # Enable datadog extensions: tags and histograms
|
@@ -41,7 +54,9 @@ StatsD keys look like 'admin.logins.api.success'. Each dot in the key represents
|
|
41
54
|
|
42
55
|
## Usage
|
43
56
|
|
44
|
-
|
57
|
+
You can either use the basic methods to submit stats over StatsD, or you can use the metaprogramming methods to instrument your methods with some basic stats (call counts, successes & failures, and timings).
|
58
|
+
|
59
|
+
#### StatsD.measure
|
45
60
|
|
46
61
|
Lets you benchmark how long the execution of a specific method takes.
|
47
62
|
|
@@ -54,15 +69,8 @@ StatsD.measure('GoogleBase.insert') do
|
|
54
69
|
GoogleBase.insert(product)
|
55
70
|
end
|
56
71
|
```
|
57
|
-
|
58
|
-
Rather than using this method directly it's more common to use the metaprogramming methods made available.
|
59
|
-
|
60
|
-
``` ruby
|
61
|
-
GoogleBase.extend StatsD::Instrument
|
62
|
-
GoogleBase.statsd_measure :insert, 'GoogleBase.insert'
|
63
|
-
```
|
64
72
|
|
65
|
-
|
73
|
+
#### StatsD.increment
|
66
74
|
|
67
75
|
Lets you increment a key in statsd to keep a count of something. If the specified key doesn't exist it will create it for you.
|
68
76
|
|
@@ -76,9 +84,28 @@ StatsD.increment('GoogleBase.insert', 10)
|
|
76
84
|
StatsD.increment('GoogleBase.insert', 1, 0.1)
|
77
85
|
```
|
78
86
|
|
79
|
-
|
87
|
+
#### StatsD.gauge
|
80
88
|
|
81
|
-
|
89
|
+
A gauge is a single numerical value value that tells you the state of the system at a point in time. A good example would be the number of messages in a queue.
|
90
|
+
|
91
|
+
``` ruby
|
92
|
+
StatsD.gauge('GoogleBase.queued', 12, 1.0)
|
93
|
+
```
|
94
|
+
|
95
|
+
Normally, you shouldn't update this value too often, and therefore there is no need to sample this kind metric.
|
96
|
+
|
97
|
+
#### StatsD.set
|
98
|
+
|
99
|
+
A set keeps track of the number of unique values that have been seen. This is a good fit for keeping track of the number of unique visitors. The value can be a string.
|
100
|
+
|
101
|
+
``` ruby
|
102
|
+
# Submit the customer ID to the set. It will only be counted if it hasn't been seen before.
|
103
|
+
StatsD.set('GoogleBase.customers', "12345", 1.0)
|
104
|
+
```
|
105
|
+
|
106
|
+
Because you are counting unique values, the results of using a sampling value less than 1.0 can lead to unexpected, hard to interpret results.
|
107
|
+
|
108
|
+
### Metaprogramming Methods
|
82
109
|
|
83
110
|
As mentioned, it's most common to use the provided metaprogramming methods. This lets you define all of your instrumentation in one file and not litter your code with instrumentation details. You should enable a class for instrumentation by extending it with the `StatsD::Instrument` class.
|
84
111
|
|
@@ -88,7 +115,15 @@ GoogleBase.extend StatsD::Instrument
|
|
88
115
|
|
89
116
|
Then use the methods provided below to instrument methods in your class.
|
90
117
|
|
91
|
-
|
118
|
+
#### statsd\_measure
|
119
|
+
|
120
|
+
This will measure how long a method takes to run, and submits the result to the given key.
|
121
|
+
|
122
|
+
``` ruby
|
123
|
+
GoogleBase.statsd_measure :insert, 'GoogleBase.insert'
|
124
|
+
```
|
125
|
+
|
126
|
+
#### statsd\_count
|
92
127
|
|
93
128
|
This will increment the given key even if the method doesn't finish (ie. raises).
|
94
129
|
|
@@ -98,7 +133,7 @@ GoogleBase.statsd_count :insert, 'GoogleBase.insert'
|
|
98
133
|
|
99
134
|
Note how I used the 'GoogleBase.insert' key above when measuring this method, and I reused here when counting the method calls. StatsD automatically separates these two kinds of stats into namespaces so there won't be a key collision here.
|
100
135
|
|
101
|
-
|
136
|
+
#### statsd\_count\_if
|
102
137
|
|
103
138
|
This will only increment the given key if the method executes successfully.
|
104
139
|
|
@@ -116,7 +151,7 @@ end
|
|
116
151
|
|
117
152
|
In the above example we will only increment the key in statsd if the result of the block returns true. So the method is returning a Net::HTTP response and we're checking the status code.
|
118
153
|
|
119
|
-
|
154
|
+
#### statsd\_count\_success
|
120
155
|
|
121
156
|
Similar to statsd_count_if, except this will increment one key in the case of success and another key in the case of failure.
|
122
157
|
|
@@ -170,8 +205,16 @@ Tested on several Ruby versions using Travis CI:
|
|
170
205
|
|
171
206
|
* Ruby 1.8.7
|
172
207
|
* Ruby Enterprise Edition 1.8.7
|
173
|
-
* Ruby 1.9.2
|
174
208
|
* Ruby 1.9.3
|
175
209
|
* Ruby 2.0.0
|
210
|
+
* Ruby 2.1.0
|
211
|
+
|
212
|
+
## Contributing
|
213
|
+
|
214
|
+
This project is MIT licensed and welcomes outside contributions.
|
176
215
|
|
177
|
-
|
216
|
+
1. Fork the repository, and create a feature branch.
|
217
|
+
2. Implement the feature, and add tests that cover the new changes functionality.
|
218
|
+
3. Update the README.
|
219
|
+
4. Create a pull request. Make sure that you get a CI pass on it.
|
220
|
+
5. Ping @jstorimer and/or @wvanbergen for review.
|
data/lib/statsd/instrument.rb
CHANGED
@@ -156,6 +156,11 @@ module StatsD
|
|
156
156
|
collect(key, value, :h, sample_rate_or_epoch, tags)
|
157
157
|
end
|
158
158
|
|
159
|
+
# uniques:765|s
|
160
|
+
def self.set(key, value, sample_rate_or_epoch = default_sample_rate, tags = nil)
|
161
|
+
collect(key, value, :s, sample_rate_or_epoch, tags)
|
162
|
+
end
|
163
|
+
|
159
164
|
private
|
160
165
|
|
161
166
|
def self.invalidate_socket
|
@@ -209,6 +214,8 @@ module StatsD
|
|
209
214
|
when :h
|
210
215
|
raise NotImplementedError, "Histograms only supported on DataDog implementation." unless self.implementation == :datadog
|
211
216
|
command << '|h'
|
217
|
+
when :s
|
218
|
+
command << '|s'
|
212
219
|
end
|
213
220
|
|
214
221
|
command << "|@#{sample_rate}" if sample_rate < 1 || (self.implementation == :statsite && sample_rate > 1)
|
@@ -226,3 +233,4 @@ StatsD.enabled = true
|
|
226
233
|
StatsD.default_sample_rate = 1.0
|
227
234
|
StatsD.implementation = ENV.fetch('STATSD_IMPLEMENTATION', 'statsd').to_sym
|
228
235
|
StatsD.server = ENV['STATSD_ADDR'] if ENV.has_key?('STATSD_ADDR')
|
236
|
+
StatsD.mode = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
data/test/statsd_test.rb
CHANGED
@@ -38,6 +38,11 @@ class StatsDTest < Test::Unit::TestCase
|
|
38
38
|
StatsD.gauge('values.foobar', 12)
|
39
39
|
end
|
40
40
|
|
41
|
+
def test_statsd_set
|
42
|
+
StatsD.expects(:collect).with('values.foobar', 12, :s, 1.0, nil)
|
43
|
+
StatsD.set('values.foobar', 12)
|
44
|
+
end
|
45
|
+
|
41
46
|
def test_statsd_histogram_on_datadog
|
42
47
|
StatsD.stubs(:implementation).returns(:datadog)
|
43
48
|
StatsD.expects(:collect).with('values.hg', 12.33, :h, 0.2, ['tag_123', 'key-name:value123'])
|
@@ -76,6 +81,14 @@ class StatsDTest < Test::Unit::TestCase
|
|
76
81
|
StatsD.gauge('fooy', 42, 0.01)
|
77
82
|
end
|
78
83
|
|
84
|
+
def test_supports_set_syntax
|
85
|
+
StatsD.expects(:write_packet).with('unique:10.0.0.10|s')
|
86
|
+
StatsD.set('unique', '10.0.0.10')
|
87
|
+
|
88
|
+
StatsD.expects(:write_packet).with('unique:10.0.0.10|s|@0.01')
|
89
|
+
StatsD.set('unique', '10.0.0.10', 0.01)
|
90
|
+
end
|
91
|
+
|
79
92
|
def test_support_timing_syntax
|
80
93
|
StatsD.expects(:write_packet).with('duration:1.23|ms')
|
81
94
|
StatsD.measure('duration', 1.23)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statsd-instrument
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesse Storimer
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|