dogstatsd-ruby 2.0.0 → 2.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +5 -13
  2. data/lib/datadog/statsd.rb +68 -52
  3. metadata +13 -13
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZGI3OTFiN2FiNmZkY2JlODliZTliMDYyMzY2MWRlNTY0ZTJmZDU3Ng==
5
- data.tar.gz: !binary |-
6
- NDgwM2VhNzdmYWUxYWZhMzNmZDI3MTJkMTBlNDBhNGY0MzdkYjVkYw==
2
+ SHA1:
3
+ metadata.gz: 481edf1969341d5a0fcca585196f32b19a0290ee
4
+ data.tar.gz: 743744502b34fa5c2b53afe3e8ce0a924c1cd317
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- OTJhMWQ5OWQwYTEyNmJkODlkZGUwMmJkMDA1MGUzMGRmZTU3YjIyNjFjZDJk
10
- ZTdmYTdhODdmMTJiYjlmOTUwYjVkYjFjN2RkMjU4YTY0MzZlZTgxMGU1ODEz
11
- N2EwNmUzMmNmYWZlZjZjOTI2N2E3ZjliOWRiMzc1NTgxYmU3OWY=
12
- data.tar.gz: !binary |-
13
- ZWJlNzRjNDdkMDM1YzFjOGQzZGRjNGFjZDllMTc4MDdmNzUzZDJhNDc1Mjc2
14
- M2M5MWE5ZjMyMDFiYjZlNWU4ZDAyZDA1ODMzNGZkYjg5YTAzMzg5NzhhMmRh
15
- OTczN2UxYWE1ZGM0MGU0MTVkODgwYzVmYWJhNGIwMzU0NDEwYTY=
6
+ metadata.gz: d5d5f0f2f11c3f713d4907e0dc8b89978ad2dbcb21466705b31e630102f2a36f68eb6bafad1c66a2783f010c66ee1a7f7fffa13ee3a1ca03c85357fc63b8fb56
7
+ data.tar.gz: 5a746d3b48a5c31b5cbd40b06daa896c1faa7d1f8d4686cdcf69d6066bdf1fd5c81bcfaa1e08462df8b234e8a08b3bbde7f445b26819480717a0edc4bdb131be
@@ -24,22 +24,23 @@ module Datadog
24
24
 
25
25
  # Create a dictionary to assign a key to every parameter's name, except for tags (treated differently)
26
26
  # Goal: Simple and fast to add some other parameters
27
- OPTS_KEYS = [
28
- ['date_happened', 'd'],
29
- ['hostname', 'h'],
30
- ['aggregation_key', 'k'],
31
- ['priority', 'p'],
32
- ['source_type_name', 's'],
33
- ['alert_type', 't']
34
- ]
27
+ OPTS_KEYS = {
28
+ :date_happened => :d,
29
+ :hostname => :h,
30
+ :aggregation_key => :k,
31
+ :priority => :p,
32
+ :source_type_name => :s,
33
+ :alert_type => :t,
34
+ }
35
35
 
36
36
  # Service check options
37
- SC_OPT_KEYS = [
38
- ['timestamp', 'd:'],
39
- ['hostname', 'h:'],
40
- ['tags', '#'],
41
- ['message', 'm:']
42
- ]
37
+ SC_OPT_KEYS = {
38
+ :timestamp => 'd:'.freeze,
39
+ :hostname => 'h:'.freeze,
40
+ :tags => '#'.freeze,
41
+ :message => 'm:'.freeze,
42
+ }
43
+
43
44
  OK = 0
44
45
  WARNING = 1
45
46
  CRITICAL = 2
@@ -70,7 +71,7 @@ module Datadog
70
71
 
71
72
  # Return the current version of the library.
72
73
  def self.VERSION
73
- "2.0.0"
74
+ "2.1.0"
74
75
  end
75
76
 
76
77
  # @param [String] host your statsd host
@@ -90,15 +91,15 @@ module Datadog
90
91
 
91
92
  def namespace=(namespace) #:nodoc:
92
93
  @namespace = namespace
93
- @prefix = namespace.nil? ? nil : "#{namespace}."
94
+ @prefix = namespace.nil? ? nil : "#{namespace}.".freeze
94
95
  end
95
96
 
96
97
  def host=(host) #:nodoc:
97
- @host = host || '127.0.0.1'
98
+ @host = host || DEFAULT_HOST
98
99
  end
99
100
 
100
101
  def port=(port) #:nodoc:
101
- @port = port || 8125
102
+ @port = port || DEFAULT_PORT
102
103
  end
103
104
 
104
105
  def tags=(tags) #:nodoc:
@@ -112,9 +113,11 @@ module Datadog
112
113
  # @param [Hash] opts the options to create the metric with
113
114
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
114
115
  # @option opts [Array<String>] :tags An array of tags
116
+ # @option opts [Numeric] :by increment value, default 1
115
117
  # @see #count
116
118
  def increment(stat, opts={})
117
- count stat, 1, opts
119
+ incr_value = opts.fetch(:by, 1)
120
+ count stat, incr_value, opts
118
121
  end
119
122
 
120
123
  # Sends a decrement (count = -1) for the given stat to the statsd server.
@@ -123,9 +126,11 @@ module Datadog
123
126
  # @param [Hash] opts the options to create the metric with
124
127
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
125
128
  # @option opts [Array<String>] :tags An array of tags
129
+ # @option opts [Numeric] :by decrement value, default 1
126
130
  # @see #count
127
131
  def decrement(stat, opts={})
128
- count stat, -1, opts
132
+ decr_value = - opts.fetch(:by, 1)
133
+ count stat, decr_value, opts
129
134
  end
130
135
 
131
136
  # Sends an arbitrary count for the given stat to the statsd server.
@@ -198,12 +203,9 @@ module Datadog
198
203
  # $statsd.time('account.activate') { @account.activate! }
199
204
  def time(stat, opts={})
200
205
  start = Time.now
201
- result = yield
202
- time_since(stat, start, opts)
203
- result
204
- rescue
206
+ return yield
207
+ ensure
205
208
  time_since(stat, start, opts)
206
- raise
207
209
  end
208
210
  # Sends a value to be tracked as a set to the statsd server.
209
211
  #
@@ -234,23 +236,24 @@ module Datadog
234
236
  service_check_string = format_service_check(name, status, opts)
235
237
  send_to_socket service_check_string
236
238
  end
239
+
237
240
  def format_service_check(name, status, opts={})
238
241
  sc_string = "_sc|#{name}|#{status}"
239
242
 
240
- SC_OPT_KEYS.each do |name_key|
241
- if opts[name_key[0].to_sym]
242
- if name_key[0] == 'tags'
243
- tags = opts[:tags].map {|tag| escape_tag_content(tag) }
244
- tags = "#{tags.join(",")}" unless tags.empty?
245
- sc_string << "|##{tags}"
246
- elsif name_key[0] == 'message'
247
- message = remove_pipes(opts[:message])
248
- escaped_message = escape_service_check_message(message)
249
- sc_string << "|m:#{escaped_message}"
250
- else
251
- value = remove_pipes(opts[name_key[0].to_sym])
252
- sc_string << "|#{name_key[1]}#{value}"
253
- end
243
+ SC_OPT_KEYS.each do |key, shorthand_key|
244
+ next unless opts[key]
245
+
246
+ if key == :tags
247
+ tags = opts[:tags].map {|tag| escape_tag_content(tag) }
248
+ tags = "#{tags.join(COMMA)}" unless tags.empty?
249
+ sc_string << "|##{tags}"
250
+ elsif key == :message
251
+ message = remove_pipes(opts[:message])
252
+ escaped_message = escape_service_check_message(message)
253
+ sc_string << "|m:#{escaped_message}"
254
+ else
255
+ value = remove_pipes(opts[key])
256
+ sc_string << "|#{shorthand_key}#{value}"
254
257
  end
255
258
  end
256
259
  return sc_string
@@ -303,38 +306,51 @@ module Datadog
303
306
 
304
307
  # We construct the string to be sent by adding '|key:value' parts to it when needed
305
308
  # All pipes ('|') in the metadata are removed. Title and Text can keep theirs
306
- OPTS_KEYS.each do |name_key|
307
- if name_key[0] != 'tags' && opts[name_key[0].to_sym]
308
- value = remove_pipes(opts[name_key[0].to_sym])
309
- event_string_data << "|#{name_key[1]}:#{value}"
309
+ OPTS_KEYS.each do |key, shorthand_key|
310
+ if key != :tags && opts[key]
311
+ value = remove_pipes(opts[key])
312
+ event_string_data << "|#{shorthand_key}:#{value}"
310
313
  end
311
314
  end
315
+
312
316
  # Tags are joined and added as last part to the string to be sent
313
317
  full_tags = (tags + (opts[:tags] || [])).map {|tag| escape_tag_content(tag) }
314
318
  unless full_tags.empty?
315
- event_string_data << "|##{full_tags.join(',')}"
319
+ event_string_data << "|##{full_tags.join(COMMA)}"
316
320
  end
317
321
 
318
- raise "Event #{title} payload is too big (more that 8KB), event discarded" if event_string_data.length > 8 * 1024
322
+ raise "Event #{title} payload is too big (more that 8KB), event discarded" if event_string_data.length > 8192 # 8 * 1024 = 8192
319
323
  return event_string_data
320
324
  end
321
325
 
322
326
  private
323
327
 
328
+ NEW_LINE = "\n".freeze
329
+ ESC_NEW_LINE = "\\n".freeze
330
+ COMMA = ",".freeze
331
+ BLANK = "".freeze
332
+ PIPE = "|".freeze
333
+ DOT = ".".freeze
334
+ DOUBLE_COLON = "::".freeze
335
+ UNDERSCORE = "_".freeze
336
+
337
+ private_constant :NEW_LINE, :ESC_NEW_LINE, :COMMA, :BLANK, :PIPE, :DOT,
338
+ :DOUBLE_COLON, :UNDERSCORE
339
+
324
340
  def escape_event_content(msg)
325
- msg.gsub "\n", "\\n"
341
+ msg.gsub NEW_LINE, ESC_NEW_LINE
326
342
  end
327
343
 
328
344
  def escape_tag_content(tag)
329
- remove_pipes(tag).gsub ",", ""
345
+ remove_pipes(tag).gsub COMMA, BLANK
330
346
  end
331
347
 
332
348
  def remove_pipes(msg)
333
- msg.gsub "|", ""
349
+ msg.gsub PIPE, BLANK
334
350
  end
335
351
 
336
352
  def escape_service_check_message(msg)
337
- msg.gsub('m:', 'm\:').gsub "\n", "\\n"
353
+ escape_event_content(msg).gsub('m:'.freeze, 'm\:'.freeze)
338
354
  end
339
355
 
340
356
  def time_since(stat, start, opts)
@@ -345,10 +361,10 @@ module Datadog
345
361
  sample_rate = opts[:sample_rate] || 1
346
362
  if sample_rate == 1 or rand < sample_rate
347
363
  # Replace Ruby module scoping with '.' and reserved chars (: | @) with underscores.
348
- stat = stat.to_s.gsub('::', '.').tr(':|@', '_')
364
+ stat = stat.to_s.gsub(DOUBLE_COLON, DOT).tr(':|@'.freeze, UNDERSCORE)
349
365
  rate = "|@#{sample_rate}" unless sample_rate == 1
350
366
  ts = (tags || []) + (opts[:tags] || []).map {|tag| escape_tag_content(tag)}
351
- tags = "|##{ts.join(",")}" unless ts.empty?
367
+ tags = "|##{ts.join(COMMA)}" unless ts.empty?
352
368
  send_stat "#{@prefix}#{stat}:#{delta}|#{type}#{rate}#{tags}"
353
369
  end
354
370
  end
@@ -361,7 +377,7 @@ module Datadog
361
377
  end
362
378
 
363
379
  def flush_buffer()
364
- send_to_socket(@buffer.join("\n"))
380
+ send_to_socket(@buffer.join(NEW_LINE))
365
381
  @buffer = Array.new
366
382
  end
367
383
 
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dogstatsd-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rein Henrichs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-22 00:00:00.000000000 Z
11
+ date: 2016-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: yard
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.6.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.6.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: jeweler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.8'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.8'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: simplecov
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: A Ruby DogStastd client
@@ -87,17 +87,17 @@ require_paths:
87
87
  - lib
88
88
  required_ruby_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - ! '>='
90
+ - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - ! '>='
95
+ - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.4.5
100
+ rubygems_version: 2.4.3
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: A Ruby DogStatsd client