dogstatsd-ruby 3.3.0 → 5.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'connection'
4
+
5
+ module Datadog
6
+ class Statsd
7
+ class UDPConnection < Connection
8
+ DEFAULT_HOST = '127.0.0.1'
9
+ DEFAULT_PORT = 8125
10
+
11
+ # StatsD host. Defaults to 127.0.0.1.
12
+ attr_reader :host
13
+
14
+ # StatsD port. Defaults to 8125.
15
+ attr_reader :port
16
+
17
+ def initialize(host, port, **kwargs)
18
+ super(**kwargs)
19
+
20
+ @host = host || ENV.fetch('DD_AGENT_HOST', DEFAULT_HOST)
21
+ @port = port || ENV.fetch('DD_DOGSTATSD_PORT', DEFAULT_PORT).to_i
22
+ @socket = nil
23
+ end
24
+
25
+ def close
26
+ @socket.close if @socket
27
+ @socket = nil
28
+ end
29
+
30
+ private
31
+
32
+ def connect
33
+ close if @socket
34
+
35
+ @socket = UDPSocket.new
36
+ @socket.connect(host, port)
37
+ end
38
+
39
+ # send_message is writing the message in the socket, it may create the socket if nil
40
+ # It is not thread-safe but since it is called by either the Sender bg thread or the
41
+ # SingleThreadSender (which is using a mutex while Flushing), only one thread must call
42
+ # it at a time.
43
+ def send_message(message)
44
+ connect unless @socket
45
+ @socket.send(message, 0)
46
+ end
47
+ end
48
+ end
49
+ 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.2'
8
+ end
9
+ end