forked from DataDog/dogstatsd-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatsd.rb
More file actions
396 lines (346 loc) · 14.1 KB
/
statsd.rb
File metadata and controls
396 lines (346 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
require 'socket'
# = Datadog::Statsd: A DogStatsd client (https://www.datadoghq.com)
#
# @example Set up a global Statsd client for a server on localhost:8125
# require 'datadog/statsd'
# $statsd = Datadog::Statsd.new 'localhost', 8125
# @example Send some stats
# $statsd.increment 'page.views'
# $statsd.timing 'page.load', 320
# $statsd.gauge 'users.online', 100
# @example Use {#time} to time the execution of a block
# $statsd.time('account.activate') { @account.activate! }
# @example Create a namespaced statsd client and increment 'account.activate'
# statsd = Datadog::Statsd.new 'localhost', 8125, :namespace => 'account'
# statsd.increment 'activate'
# @example Create a statsd client with global tags
# statsd = Datadog::Statsd.new 'localhost', 8125, :tags => 'tag1:true'
module Datadog
class Statsd
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 8125
# Create a dictionary to assign a key to every parameter's name, except for tags (treated differently)
# Goal: Simple and fast to add some other parameters
OPTS_KEYS = {
:date_happened => :d,
:hostname => :h,
:aggregation_key => :k,
:priority => :p,
:source_type_name => :s,
:alert_type => :t,
}
# Service check options
SC_OPT_KEYS = {
:timestamp => 'd:'.freeze,
:hostname => 'h:'.freeze,
:tags => '#'.freeze,
:message => 'm:'.freeze,
}
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
# A namespace to prepend to all statsd calls. Defaults to no namespace.
attr_reader :namespace
# StatsD host. Defaults to 127.0.0.1.
attr_reader :host
# StatsD port. Defaults to 8125.
attr_reader :port
# Global tags to be added to every statsd call. Defaults to no tags.
attr_reader :tags
# Buffer containing the statsd message before they are sent in batch
attr_reader :buffer
# Maximum number of metrics in the buffer before it is flushed
attr_accessor :max_buffer_size
class << self
# Set to a standard logger instance to enable debug logging.
attr_accessor :logger
end
# Return the current version of the library.
def self.VERSION
"2.0.0"
end
# @param [String] host your statsd host
# @param [Integer] port your statsd port
# @option opts [String] :namespace set a namespace to be prepended to every metric name
# @option opts [Array<String>] :tags tags to be added to every metric
def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT, opts = {}, max_buffer_size=50)
self.host, self.port = host, port
@prefix = nil
@socket = UDPSocket.new
self.namespace = opts[:namespace]
self.tags = opts[:tags]
@buffer = Array.new
self.max_buffer_size = max_buffer_size
alias :send_stat :send_to_socket
end
def namespace=(namespace) #:nodoc:
@namespace = namespace
@prefix = namespace.nil? ? nil : "#{namespace}.".freeze
end
def host=(host) #:nodoc:
@host = host || DEFAULT_HOST
end
def port=(port) #:nodoc:
@port = port || DEFAULT_PORT
end
def tags=(tags) #:nodoc:
raise ArgumentError, 'tags must be a Array<String>' unless tags.nil? or tags.is_a? Array
@tags = (tags || []).map {|tag| escape_tag_content(tag)}
end
# Sends an increment (count = 1) for the given stat to the statsd server.
#
# @param [String] stat stat name
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
# @see #count
def increment(stat, opts={})
count stat, 1, opts
end
# Sends a decrement (count = -1) for the given stat to the statsd server.
#
# @param [String] stat stat name
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
# @see #count
def decrement(stat, opts={})
count stat, -1, opts
end
# Sends an arbitrary count for the given stat to the statsd server.
#
# @param [String] stat stat name
# @param [Integer] count count
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
def count(stat, count, opts={})
send_stats stat, count, :c, opts
end
# Sends an arbitary gauge value for the given stat to the statsd server.
#
# This is useful for recording things like available disk space,
# memory usage, and the like, which have different semantics than
# counters.
#
# @param [String] stat stat name.
# @param [Numeric] value gauge value.
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
# @example Report the current user count:
# $statsd.gauge('user.count', User.count)
def gauge(stat, value, opts={})
send_stats stat, value, :g, opts
end
# Sends a value to be tracked as a histogram to the statsd server.
#
# @param [String] stat stat name.
# @param [Numeric] value histogram value.
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
# @example Report the current user count:
# $statsd.histogram('user.count', User.count)
def histogram(stat, value, opts={})
send_stats stat, value, :h, opts
end
# Sends a timing (in ms) for the given stat to the statsd server. The
# sample_rate determines what percentage of the time this report is sent. The
# statsd server then uses the sample_rate to correctly track the average
# timing for the stat.
#
# @param [String] stat stat name
# @param [Integer] ms timing in milliseconds
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
def timing(stat, ms, opts={})
send_stats stat, ms, :ms, opts
end
# Reports execution time of the provided block using {#timing}.
#
# If the block fails, the stat is still reported, then the error
# is reraised
#
# @param [String] stat stat name
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
# @yield The operation to be timed
# @see #timing
# @example Report the time (in ms) taken to activate an account
# $statsd.time('account.activate') { @account.activate! }
def time(stat, opts={})
start = Time.now
result = yield
time_since(stat, start, opts)
result
rescue
time_since(stat, start, opts)
raise
end
# Sends a value to be tracked as a set to the statsd server.
#
# @param [String] stat stat name.
# @param [Numeric] value set value.
# @param [Hash] opts the options to create the metric with
# @option opts [Numeric] :sample_rate sample rate, 1 for always
# @option opts [Array<String>] :tags An array of tags
# @example Record a unique visitory by id:
# $statsd.set('visitors.uniques', User.id)
def set(stat, value, opts={})
send_stats stat, value, :s, opts
end
# This method allows you to send custom service check statuses.
#
# @param [String] name Service check name
# @param [String] status Service check status.
# @param [Hash] opts the additional data about the service check
# @option opts [Integer, nil] :timestamp (nil) Assign a timestamp to the event. Default is now when none
# @option opts [String, nil] :hostname (nil) Assign a hostname to the event.
# @option opts [Array<String>, nil] :tags (nil) An array of tags
# @option opts [String, nil] :message (nil) A message to associate with this service check status
# @example Report a critical service check status
# $statsd.service_check('my.service.check', Statsd::CRITICAL, :tags=>['urgent'])
def service_check(name, status, opts={})
service_check_string = format_service_check(name, status, opts)
send_to_socket service_check_string
end
def format_service_check(name, status, opts={})
sc_string = "_sc|#{name}|#{status}"
SC_OPT_KEYS.each do |key, shorthand_key|
next unless opts[key]
if key == :tags
tags = opts[:tags].map {|tag| escape_tag_content(tag) }
tags = "#{tags.join(COMMA)}" unless tags.empty?
sc_string << "|##{tags}"
elsif key == :message
message = remove_pipes(opts[:message])
escaped_message = escape_service_check_message(message)
sc_string << "|m:#{escaped_message}"
else
value = remove_pipes(opts[key])
sc_string << "|#{shorthand_key}#{value}"
end
end
return sc_string
end
# This end point allows you to post events to the stream. You can tag them, set priority and even aggregate them with other events.
#
# Aggregation in the stream is made on hostname/event_type/source_type/aggregation_key.
# If there's no event type, for example, then that won't matter;
# it will be grouped with other events that don't have an event type.
#
# @param [String] title Event title
# @param [String] text Event text. Supports \n
# @param [Hash] opts the additional data about the event
# @option opts [Integer, nil] :date_happened (nil) Assign a timestamp to the event. Default is now when none
# @option opts [String, nil] :hostname (nil) Assign a hostname to the event.
# @option opts [String, nil] :aggregation_key (nil) Assign an aggregation key to the event, to group it with some others
# @option opts [String, nil] :priority ('normal') Can be "normal" or "low"
# @option opts [String, nil] :source_type_name (nil) Assign a source type to the event
# @option opts [String, nil] :alert_type ('info') Can be "error", "warning", "info" or "success".
# @option opts [Array<String>] :tags tags to be added to every metric
# @example Report an awful event:
# $statsd.event('Something terrible happened', 'The end is near if we do nothing', :alert_type=>'warning', :tags=>['end_of_times','urgent'])
def event(title, text, opts={})
event_string = format_event(title, text, opts)
raise "Event #{title} payload is too big (more that 8KB), event discarded" if event_string.length > 8 * 1024
send_to_socket event_string
end
# Send several metrics in the same UDP Packet
# They will be buffered and flushed when the block finishes
#
# @example Send several metrics in one packet:
# $statsd.batch do |s|
# s.gauge('users.online',156)
# s.increment('page.views')
# end
def batch()
alias :send_stat :send_to_buffer
yield self
flush_buffer
alias :send_stat :send_to_socket
end
def format_event(title, text, opts={})
escaped_title = escape_event_content(title)
escaped_text = escape_event_content(text)
event_string_data = "_e{#{escaped_title.length},#{escaped_text.length}}:#{escaped_title}|#{escaped_text}"
# We construct the string to be sent by adding '|key:value' parts to it when needed
# All pipes ('|') in the metadata are removed. Title and Text can keep theirs
OPTS_KEYS.each do |key, shorthand_key|
if key != :tags && opts[key]
value = remove_pipes(opts[key])
event_string_data << "|#{shorthand_key}:#{value}"
end
end
# Tags are joined and added as last part to the string to be sent
full_tags = (tags + (opts[:tags] || [])).map {|tag| escape_tag_content(tag) }
unless full_tags.empty?
event_string_data << "|##{full_tags.join(COMMA)}"
end
raise "Event #{title} payload is too big (more that 8KB), event discarded" if event_string_data.length > 8192 # 8 * 1024 = 8192
return event_string_data
end
private
NEW_LINE = "\n".freeze
ESC_NEW_LINE = "\\n".freeze
COMMA = ",".freeze
BLANK = "".freeze
PIPE = "|".freeze
DOT = ".".freeze
DOUBLE_COLON = "::".freeze
UNDERSCORE = "_".freeze
private_constant :NEW_LINE, :ESC_NEW_LINE, :COMMA, :BLANK, :PIPE, :DOT,
:DOUBLE_COLON, :UNDERSCORE
def escape_event_content(msg)
msg.gsub NEW_LINE, ESC_NEW_LINE
end
def escape_tag_content(tag)
remove_pipes(tag).gsub COMMA, BLANK
end
def remove_pipes(msg)
msg.gsub PIPE, BLANK
end
def escape_service_check_message(msg)
escape_event_content(msg).gsub('m:'.freeze, 'm\:'.freeze)
end
def time_since(stat, start, opts)
timing(stat, ((Time.now - start) * 1000).round, opts)
end
def send_stats(stat, delta, type, opts={})
sample_rate = opts[:sample_rate] || 1
if sample_rate == 1 or rand < sample_rate
# Replace Ruby module scoping with '.' and reserved chars (: | @) with underscores.
stat = stat.to_s.gsub(DOUBLE_COLON, DOT).tr(':|@'.freeze, UNDERSCORE)
rate = "|@#{sample_rate}" unless sample_rate == 1
ts = (tags || []) + (opts[:tags] || []).map {|tag| escape_tag_content(tag)}
tags = "|##{ts.join(COMMA)}" unless ts.empty?
send_stat "#{@prefix}#{stat}:#{delta}|#{type}#{rate}#{tags}"
end
end
def send_to_buffer(message)
@buffer << message
if @buffer.length >= @max_buffer_size
flush_buffer
end
end
def flush_buffer()
send_to_socket(@buffer.join(NEW_LINE))
@buffer = Array.new
end
def send_to_socket(message)
self.class.logger.debug { "Statsd: #{message}" } if self.class.logger
@socket.send(message, 0, @host, @port)
rescue => boom
self.class.logger.error { "Statsd: #{boom.class} #{boom}" } if self.class.logger
nil
end
# Close the underlying socket
def close()
@socket.close
end
end
end