dogstatsd-ruby 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/datadog/statsd.rb +40 -20
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 58a9207adf16ba8265e91fd56b6315b35ecc9b58
4
- data.tar.gz: 9fc64d84a4b0277752bad6a5b523f1ab1ac989c2
2
+ SHA256:
3
+ metadata.gz: a9743c7a429c0b5932a6f592d55a362582ec6de74fbdca658143e9248c45f2aa
4
+ data.tar.gz: 0cd22bc62deaf61fe5af0a26f607f92e347f2068ae5d12045248c5a3bcccc302
5
5
  SHA512:
6
- metadata.gz: 89574d06e8d66f00a203d6d84b9e20f70b1d64e90fd2dfa644bdab8993a0420065b2e426d574fc0c0239fc69f49a65b0d215359c50ec2fd00dd9b9214f8c9f13
7
- data.tar.gz: d97dc13266fe51bc7d49ebb8e56235aef70a6773e89d890ffe4774e0406489f6a7fadb43818f1000fc70f7520d6baec19d05b395c732cce9ea22ef9107597de9
6
+ metadata.gz: e7f35978f04e268613833a79464b827c71a3a0340840621e4b426fca0242a0a718be518ecc64936ed94e16b80a55e3bc40c11eeb21e365565380d9cbbb3d2ab4
7
+ data.tar.gz: 9c01c4abeac5f00a8f5eb8936a54c7e09cc903fcafa33115e10a3f9f8fd36e098d7c7d0b0ef5bb2c038eb527ca44011ea304beb8e960f71e3fbc81415db4e5a8
@@ -61,13 +61,12 @@ module Datadog
61
61
  # StatsD port. Defaults to 8125.
62
62
  attr_reader :port
63
63
 
64
+ # DogStatsd unix socket path. Not used by default.
65
+ attr_reader :socket_path
66
+
64
67
  # Global tags to be added to every statsd call. Defaults to no tags.
65
68
  attr_reader :tags
66
69
 
67
- # True if we should batch up data before sending it, or false if we
68
- # want to send data immediately.
69
- attr_reader :should_batch
70
-
71
70
  # Buffer containing the statsd message before they are sent in batch
72
71
  attr_reader :buffer
73
72
 
@@ -81,7 +80,7 @@ module Datadog
81
80
 
82
81
  # Return the current version of the library.
83
82
  def self.VERSION
84
- "3.0.0"
83
+ "3.1.0"
85
84
  end
86
85
 
87
86
  # @param [String] host your statsd host
@@ -90,13 +89,14 @@ module Datadog
90
89
  # @option opts [Array<String>] :tags tags to be added to every metric
91
90
  def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT, opts = {}, max_buffer_size=50)
92
91
  self.host, self.port = host, port
92
+ @socket_path = opts[:socket_path]
93
93
  @prefix = nil
94
- @socket = connect_to_socket(host, port)
94
+ @socket = connect_to_socket if @socket_path.nil?
95
95
  self.namespace = opts[:namespace]
96
96
  self.tags = opts[:tags]
97
97
  @buffer = Array.new
98
98
  self.max_buffer_size = max_buffer_size
99
- @should_batch = false
99
+ @batch_nesting_depth = 0
100
100
  end
101
101
 
102
102
  def namespace=(namespace) #:nodoc:
@@ -114,7 +114,7 @@ module Datadog
114
114
 
115
115
  def tags=(tags) #:nodoc:
116
116
  raise ArgumentError, 'tags must be a Array<String>' unless tags.nil? or tags.is_a? Array
117
- @tags = (tags || []).map {|tag| escape_tag_content(tag)}
117
+ @tags = (tags || []).compact.map! {|tag| escape_tag_content(tag)}
118
118
  end
119
119
 
120
120
  # Sends an increment (count = 1) for the given stat to the statsd server.
@@ -303,11 +303,11 @@ module Datadog
303
303
  # s.increment('page.views')
304
304
  # end
305
305
  def batch()
306
- @should_batch = true
306
+ @batch_nesting_depth += 1
307
307
  yield self
308
- flush_buffer
309
308
  ensure
310
- @should_batch = false
309
+ @batch_nesting_depth -= 1
310
+ flush_buffer if @batch_nesting_depth == 0
311
311
  end
312
312
 
313
313
  def format_event(title, text, opts={})
@@ -358,10 +358,11 @@ module Datadog
358
358
  end
359
359
 
360
360
  def escape_tag_content(tag)
361
- remove_pipes(tag).gsub COMMA, BLANK
361
+ remove_pipes(tag.to_s).gsub COMMA, BLANK
362
362
  end
363
363
 
364
364
  def escape_tag_content!(tag)
365
+ tag = tag.to_s
365
366
  tag.gsub!(PIPE, BLANK)
366
367
  tag.gsub!(COMMA, BLANK)
367
368
  tag
@@ -411,7 +412,7 @@ module Datadog
411
412
 
412
413
 
413
414
  tag_arr = opts[:tags].to_a
414
- tag_arr.map! { |tag| t = tag.dup; escape_tag_content!(t); t }
415
+ tag_arr.map! { |tag| t = tag.to_s.dup; escape_tag_content!(t); t }
415
416
  ts = tags.to_a + tag_arr
416
417
  unless ts.empty?
417
418
  full_stat << PIPE
@@ -424,7 +425,7 @@ module Datadog
424
425
  end
425
426
 
426
427
  def send_stat(message)
427
- if @should_batch
428
+ if @batch_nesting_depth > 0
428
429
  @buffer << message
429
430
  flush_buffer if @buffer.length >= @max_buffer_size
430
431
  else
@@ -432,27 +433,46 @@ module Datadog
432
433
  end
433
434
  end
434
435
 
435
- def flush_buffer()
436
+ def flush_buffer
437
+ return @buffer if @buffer.empty?
436
438
  send_to_socket(@buffer.join(NEW_LINE))
437
439
  @buffer = Array.new
438
440
  end
439
441
 
440
- def connect_to_socket(host, port)
441
- socket = UDPSocket.new
442
- socket.connect(host, port)
442
+ def connect_to_socket
443
+ if !@socket_path.nil?
444
+ socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
445
+ socket.connect(Socket.pack_sockaddr_un(@socket_path))
446
+ else
447
+ socket = UDPSocket.new
448
+ socket.connect(@host, @port)
449
+ end
443
450
  socket
444
451
  end
445
452
 
453
+ def sock
454
+ @socket ||= connect_to_socket
455
+ end
456
+
446
457
  def send_to_socket(message)
447
458
  self.class.logger.debug { "Statsd: #{message}" } if self.class.logger
448
- @socket.send(message, 0)
459
+ if @socket_path.nil?
460
+ sock.send(message, 0)
461
+ else
462
+ sock.sendmsg_nonblock(message)
463
+ end
449
464
  rescue => boom
465
+ if @socket_path && (boom.is_a?(Errno::ECONNREFUSED) ||
466
+ boom.is_a?(Errno::ECONNRESET) ||
467
+ boom.is_a?(Errno::ENOENT))
468
+ return @socket = nil
469
+ end
450
470
  # Try once to reconnect if the socket has been closed
451
471
  retries ||= 1
452
472
  if retries <= 1 && boom.is_a?(IOError) && boom.message =~ /closed stream/i
453
473
  retries += 1
454
474
  begin
455
- @socket = connect_to_socket(host, port)
475
+ @socket = connect_to_socket
456
476
  retry
457
477
  rescue => e
458
478
  boom = e
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dogstatsd-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.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: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2017-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -97,9 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.6.8
100
+ rubygems_version: 2.7.1
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: A Ruby DogStatsd client
104
104
  test_files: []
105
- has_rdoc: