dogstatsd-ruby 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -17
- data/lib/statsd.rb +72 -4
- metadata +82 -70
data/README.md
CHANGED
@@ -15,28 +15,30 @@ First install the library:
|
|
15
15
|
|
16
16
|
Then start instrumenting your code:
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
``` ruby
|
19
|
+
# Load the dogstats module.
|
20
|
+
require 'statsd'
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
# Create a stats instance.
|
23
|
+
statsd = Statsd.new('localhost', 8125)
|
23
24
|
|
24
|
-
|
25
|
-
|
25
|
+
# Increment a counter.
|
26
|
+
statsd.increment('page.views')
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
# Record a gauge 50% of the time.
|
29
|
+
statsd.gauge('users.online', 123, :sample_rate=>0.5)
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
# Sample a histogram
|
32
|
+
statsd.histogram('file.upload.size', 1234)
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
# Time a block of code
|
35
|
+
statsd.time('page.render') do
|
36
|
+
render_page('home.html')
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
39
|
+
# Tag a metric.
|
40
|
+
statsd.histogram('query.time', 10, :tags => ["version:1"])
|
41
|
+
```
|
40
42
|
|
41
43
|
Documentation
|
42
44
|
-------------
|
@@ -55,7 +57,8 @@ To suggest a feature, report a bug, or general discussion, head over
|
|
55
57
|
Change Log
|
56
58
|
----------
|
57
59
|
|
58
|
-
|
60
|
+
- 1.3.0
|
61
|
+
- Added support for submitting events.
|
59
62
|
- 1.2.0
|
60
63
|
- Added global tags.
|
61
64
|
- Added ability to set `namespace` and `tags` from `Statsd#initialize`.
|
data/lib/statsd.rb
CHANGED
@@ -21,6 +21,17 @@ class Statsd
|
|
21
21
|
DEFAULT_HOST = '127.0.0.1'
|
22
22
|
DEFAULT_PORT = 8125
|
23
23
|
|
24
|
+
# Create a dictionary to assign a key to every parameter's name, except for tags (treated differently)
|
25
|
+
# Goal: Simple and fast to add some other parameters
|
26
|
+
OPTS_KEYS = [
|
27
|
+
['date_happened', 'd'],
|
28
|
+
['hostname', 'h'],
|
29
|
+
['aggregation_key', 'k'],
|
30
|
+
['priority', 'p'],
|
31
|
+
['source_type_name', 's'],
|
32
|
+
['alert_type', 't']
|
33
|
+
]
|
34
|
+
|
24
35
|
# A namespace to prepend to all statsd calls. Defaults to no namespace.
|
25
36
|
attr_reader :namespace
|
26
37
|
|
@@ -40,7 +51,7 @@ class Statsd
|
|
40
51
|
|
41
52
|
# Return the current version of the library.
|
42
53
|
def self.VERSION
|
43
|
-
"1.
|
54
|
+
"1.3.0"
|
44
55
|
end
|
45
56
|
|
46
57
|
# @param [String] host your statsd host
|
@@ -112,7 +123,7 @@ class Statsd
|
|
112
123
|
# counters.
|
113
124
|
#
|
114
125
|
# @param [String] stat stat name.
|
115
|
-
# @param [Numeric] gauge value.
|
126
|
+
# @param [Numeric] value gauge value.
|
116
127
|
# @param [Hash] opts the options to create the metric with
|
117
128
|
# @option opts [Numeric] :sample_rate sample rate, 1 for always
|
118
129
|
# @option opts [Array<String>] :tags An array of tags
|
@@ -125,7 +136,7 @@ class Statsd
|
|
125
136
|
# Sends a value to be tracked as a histogram to the statsd server.
|
126
137
|
#
|
127
138
|
# @param [String] stat stat name.
|
128
|
-
# @param [Numeric] histogram value.
|
139
|
+
# @param [Numeric] value histogram value.
|
129
140
|
# @param [Hash] opts the options to create the metric with
|
130
141
|
# @option opts [Numeric] :sample_rate sample rate, 1 for always
|
131
142
|
# @option opts [Array<String>] :tags An array of tags
|
@@ -168,7 +179,7 @@ class Statsd
|
|
168
179
|
# Sends a value to be tracked as a set to the statsd server.
|
169
180
|
#
|
170
181
|
# @param [String] stat stat name.
|
171
|
-
# @param [Numeric] set value.
|
182
|
+
# @param [Numeric] value set value.
|
172
183
|
# @param [Hash] opts the options to create the metric with
|
173
184
|
# @option opts [Numeric] :sample_rate sample rate, 1 for always
|
174
185
|
# @option opts [Array<String>] :tags An array of tags
|
@@ -178,7 +189,64 @@ class Statsd
|
|
178
189
|
send_stats stat, value, :s, opts
|
179
190
|
end
|
180
191
|
|
192
|
+
# This end point allows you to post events to the stream. You can tag them, set priority and even aggregate them with other events.
|
193
|
+
#
|
194
|
+
# Aggregation in the stream is made on hostname/event_type/source_type/aggregation_key.
|
195
|
+
# If there's no event type, for example, then that won't matter;
|
196
|
+
# it will be grouped with other events that don't have an event type.
|
197
|
+
#
|
198
|
+
# @param [String] title Event title
|
199
|
+
# @param [String] text Event text. Supports \n
|
200
|
+
# @param [Hash] opts the additional data about the event
|
201
|
+
# @option opts [Time, nil] :date_happened (nil) Assign a timestamp to the event. Default is now when none
|
202
|
+
# @option opts [String, nil] :hostname (nil) Assign a hostname to the event.
|
203
|
+
# @option opts [String, nil] :aggregation_key (nil) Assign an aggregation key to the event, to group it with some others
|
204
|
+
# @option opts [String, nil] :priority ('normal') Can be "normal" or "low"
|
205
|
+
# @option opts [String, nil] :source_type_name (nil) Assign a source type to the event
|
206
|
+
# @option opts [String, nil] :alert_type ('info') Can be "error", "warning", "info" or "success".
|
207
|
+
# @option opts [Array<String>, nil] :source_type_name (nil) An array of tags
|
208
|
+
# @example Report an aweful event:
|
209
|
+
# $statsd.event('Something terrible happened', 'The end is near if we do nothing', :alert_type=>'warning', :tags=>['end_of_times','urgent'])
|
210
|
+
def event(title, text, opts={})
|
211
|
+
event_string = format_event(title, text, opts)
|
212
|
+
raise "Event #{title} payload is too big (more that 8KB), event discarded" if event_string.length > 8 * 1024
|
213
|
+
|
214
|
+
send_to_socket event_string
|
215
|
+
end
|
216
|
+
def format_event(title, text, opts={})
|
217
|
+
escape_event_content title
|
218
|
+
escape_event_content text
|
219
|
+
event_string_data = "_e{#{title.length},#{text.length}}:#{title}|#{text}"
|
220
|
+
|
221
|
+
# We construct the string to be sent by adding '|key:value' parts to it when needed
|
222
|
+
# All pipes ('|') in the metada are removed. Title and Text can keep theirs
|
223
|
+
OPTS_KEYS.each do |name_key|
|
224
|
+
if name_key[0] != 'tags' && opts[name_key[0].to_sym]
|
225
|
+
value = opts[name_key[0].to_sym]
|
226
|
+
rm_pipes value
|
227
|
+
event_string_data << "|#{name_key[1]}:#{value}"
|
228
|
+
end
|
229
|
+
end
|
230
|
+
tags = opts[:tags] || nil
|
231
|
+
# Tags are joined and added as last part to the string to be sent
|
232
|
+
if tags
|
233
|
+
tags.each do |tag|
|
234
|
+
rm_pipes tag
|
235
|
+
end
|
236
|
+
tags = "#{tags.join(",")}" unless tags.empty?
|
237
|
+
event_string_data << "|##{tags}"
|
238
|
+
end
|
239
|
+
|
240
|
+
raise "Event #{title} payload is too big (more that 8KB), event discarded" if event_string_data.length > 8 * 1024
|
241
|
+
return event_string_data
|
242
|
+
end
|
181
243
|
private
|
244
|
+
def escape_event_content(msg)
|
245
|
+
msg = msg.sub! "\n", "\\n"
|
246
|
+
end
|
247
|
+
def rm_pipes(msg)
|
248
|
+
msg = msg.sub! "|", ""
|
249
|
+
end
|
182
250
|
|
183
251
|
def send_stats(stat, delta, type, opts={})
|
184
252
|
sample_rate = opts[:sample_rate] || 1
|
metadata
CHANGED
@@ -1,114 +1,126 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: dogstatsd-ruby
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 1.3.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Rein Henrichs
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2014-03-27 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: minitest
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
22
|
prerelease: false
|
24
|
-
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: yard
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
33
24
|
none: false
|
34
|
-
requirements:
|
35
|
-
- -
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
38
32
|
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: yard
|
39
36
|
prerelease: false
|
40
|
-
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
38
|
none: false
|
42
|
-
requirements:
|
39
|
+
requirements:
|
43
40
|
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 7
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 6
|
46
|
+
- 0
|
45
47
|
version: 0.6.0
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: jeweler
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ~>
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '1.8'
|
54
48
|
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: jeweler
|
55
52
|
prerelease: false
|
56
|
-
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
54
|
none: false
|
58
|
-
requirements:
|
55
|
+
requirements:
|
59
56
|
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
requirements:
|
67
|
-
- - ! '>='
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version: '0'
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 31
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 8
|
62
|
+
version: "1.8"
|
70
63
|
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: simplecov
|
71
67
|
prerelease: false
|
72
|
-
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
69
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
type: :development
|
78
|
+
version_requirements: *id004
|
78
79
|
description: A Ruby DogStastd client
|
79
80
|
email: code@datadoghq.com
|
80
81
|
executables: []
|
82
|
+
|
81
83
|
extensions: []
|
82
|
-
|
84
|
+
|
85
|
+
extra_rdoc_files:
|
83
86
|
- LICENSE.txt
|
84
87
|
- README.md
|
85
|
-
files:
|
88
|
+
files:
|
86
89
|
- LICENSE.txt
|
87
90
|
- README.md
|
88
91
|
- lib/statsd.rb
|
89
92
|
homepage: http://github.com/datadog/dogstatsd-ruby
|
90
|
-
licenses:
|
93
|
+
licenses:
|
91
94
|
- MIT
|
92
95
|
post_install_message:
|
93
96
|
rdoc_options: []
|
94
|
-
|
97
|
+
|
98
|
+
require_paths:
|
95
99
|
- lib
|
96
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
101
|
none: false
|
98
|
-
requirements:
|
99
|
-
- -
|
100
|
-
- !ruby/object:Gem::Version
|
101
|
-
|
102
|
-
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 3
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
110
|
none: false
|
104
|
-
requirements:
|
105
|
-
- -
|
106
|
-
- !ruby/object:Gem::Version
|
107
|
-
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 3
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
108
118
|
requirements: []
|
119
|
+
|
109
120
|
rubyforge_project:
|
110
|
-
rubygems_version: 1.8.
|
121
|
+
rubygems_version: 1.8.15
|
111
122
|
signing_key:
|
112
123
|
specification_version: 3
|
113
124
|
summary: A Ruby DogStatsd client
|
114
125
|
test_files: []
|
126
|
+
|