dogstatsd-ruby 4.0.0 → 5.3.3

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.
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ class Statsd
5
+ class Timer
6
+ def initialize(interval, &callback)
7
+ @mx = Mutex.new
8
+ @cv = ConditionVariable.new
9
+ @interval = interval
10
+ @callback = callback
11
+ @stop = true
12
+ end
13
+
14
+ def start
15
+ return unless stop?
16
+
17
+ @stop = false
18
+ @thread = Thread.new do
19
+ last_execution_time = current_time
20
+ @mx.synchronize do
21
+ until @stop
22
+ timeout = @interval - (current_time - last_execution_time)
23
+ @cv.wait(@mx, timeout > 0 ? timeout : 0)
24
+ last_execution_time = current_time
25
+ @callback.call
26
+ end
27
+ end
28
+ end
29
+ @thread.name = 'Statsd Timer' unless Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')
30
+ end
31
+
32
+ def stop
33
+ return if @thread.nil?
34
+
35
+ @stop = true
36
+ @mx.synchronize do
37
+ @cv.signal
38
+ end
39
+ @thread.join
40
+ @thread = nil
41
+ end
42
+
43
+ def stop?
44
+ @thread.nil? || @thread.stop?
45
+ end
46
+
47
+ private
48
+
49
+ if Process.const_defined?(:CLOCK_MONOTONIC)
50
+ def current_time
51
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
52
+ end
53
+ else
54
+ def current_time
55
+ Time.now
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Datadog
6
+ class Statsd
7
+ class UDPConnection < Connection
8
+ # StatsD host.
9
+ attr_reader :host
10
+
11
+ # StatsD port.
12
+ attr_reader :port
13
+
14
+ def initialize(host, port, **kwargs)
15
+ super(**kwargs)
16
+
17
+ @host = host
18
+ @port = port
19
+ @socket = nil
20
+ end
21
+
22
+ def close
23
+ @socket.close if @socket
24
+ @socket = nil
25
+ end
26
+
27
+ private
28
+
29
+ def connect
30
+ close if @socket
31
+
32
+ @socket = UDPSocket.new
33
+ @socket.connect(host, port)
34
+ end
35
+
36
+ # send_message is writing the message in the socket, it may create the socket if nil
37
+ # It is not thread-safe but since it is called by either the Sender bg thread or the
38
+ # SingleThreadSender (which is using a mutex while Flushing), only one thread must call
39
+ # it at a time.
40
+ def send_message(message)
41
+ connect unless @socket
42
+ @socket.send(message, 0)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Datadog
6
+ class Statsd
7
+ class UDSConnection < Connection
8
+ class BadSocketError < StandardError; end
9
+
10
+ # DogStatsd unix socket path
11
+ attr_reader :socket_path
12
+
13
+ def initialize(socket_path, **kwargs)
14
+ super(**kwargs)
15
+
16
+ @socket_path = socket_path
17
+ @socket = nil
18
+ end
19
+
20
+ def close
21
+ @socket.close if @socket
22
+ @socket = nil
23
+ end
24
+
25
+ private
26
+
27
+ def connect
28
+ close if @socket
29
+
30
+ @socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
31
+ @socket.connect(Socket.pack_sockaddr_un(@socket_path))
32
+ end
33
+
34
+ # send_message is writing the message in the socket, it may create the socket if nil
35
+ # It is not thread-safe but since it is called by either the Sender bg thread or the
36
+ # SingleThreadSender (which is using a mutex while Flushing), only one thread must call
37
+ # it at a time.
38
+ def send_message(message)
39
+ connect unless @socket
40
+ @socket.sendmsg_nonblock(message)
41
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ENOENT => e
42
+ # TODO: FIXME: This error should be considered as a retryable error in the
43
+ # Connection class. An even better solution would be to make BadSocketError inherit
44
+ # from a specific retryable error class in the Connection class.
45
+ raise BadSocketError, "#{e.class}: #{e}"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Datadog
6
+ class Statsd
7
+ VERSION = '5.3.3'
8
+ end
9
+ end