dogstatsd-ruby 4.8.3 → 5.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +165 -21
- data/lib/datadog/statsd/connection.rb +16 -18
- data/lib/datadog/statsd/connection_cfg.rb +125 -0
- data/lib/datadog/statsd/forwarder.rb +138 -0
- data/lib/datadog/statsd/message_buffer.rb +105 -0
- data/lib/datadog/statsd/sender.rb +184 -0
- data/lib/datadog/statsd/serialization/tag_serializer.rb +5 -1
- data/lib/datadog/statsd/single_thread_sender.rb +82 -0
- data/lib/datadog/statsd/telemetry.rb +43 -24
- data/lib/datadog/statsd/timer.rb +61 -0
- data/lib/datadog/statsd/udp_connection.rb +25 -14
- data/lib/datadog/statsd/uds_connection.rb +19 -8
- data/lib/datadog/statsd/version.rb +1 -1
- data/lib/datadog/statsd.rb +153 -69
- metadata +21 -11
- data/lib/datadog/statsd/batch.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8069234fd7b43382f85606fa53a858c502025937168c275347a0ec5552f566fc
|
4
|
+
data.tar.gz: 81945812b5810033cc2f511364d3a962a1c932e771d4faa2a4366391fe447414
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b589a6f599dc547f980587c8902eed4012b4d627fd6abe28c21829f299ee695e33f38f2fe386fb3d9f7828327134f69831f0365ee4ff16d84b7de3830178081
|
7
|
+
data.tar.gz: 05cb95baf12dbe3b2f17c835c8278f32198ce21d2d66ca14b4714aa90a12dd6a4976c48e9d9f326ca51cc66d95c83dd269e55aa6fe6e08409c61cec8866a9798
|
data/README.md
CHANGED
@@ -22,22 +22,81 @@ To instantiate a DogStatsd client:
|
|
22
22
|
# Import the library
|
23
23
|
require 'datadog/statsd'
|
24
24
|
|
25
|
-
# Create a DogStatsD client instance
|
25
|
+
# Create a DogStatsD client instance
|
26
26
|
statsd = Datadog::Statsd.new('localhost', 8125)
|
27
|
+
# ...
|
28
|
+
# release resources used by the client instance
|
29
|
+
statsd.close()
|
27
30
|
```
|
28
31
|
Or if you want to connect over Unix Domain Socket:
|
29
32
|
```ruby
|
30
33
|
# Connection over Unix Domain Socket
|
31
34
|
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file')
|
35
|
+
# ...
|
36
|
+
# release resources used by the client instance
|
37
|
+
statsd.close()
|
32
38
|
```
|
33
39
|
|
34
|
-
Find a list of all the available options for your DogStatsD Client in the [DogStatsD-ruby rubydoc](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/Datadog/Statsd) or in the [Datadog public DogStatsD documentation](https://docs.datadoghq.com/developers/dogstatsd/?
|
40
|
+
Find a list of all the available options for your DogStatsD Client in the [DogStatsD-ruby rubydoc](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/Datadog/Statsd) or in the [Datadog public DogStatsD documentation](https://docs.datadoghq.com/developers/dogstatsd/?code-lang=ruby#client-instantiation-parameters).
|
41
|
+
|
42
|
+
### Migrating from v4.x to v5.x
|
43
|
+
|
44
|
+
If you are already using DogStatsD-ruby v4.x and you want to migrate to a version v5.x, the major
|
45
|
+
change concerning you is the new [threading model](#threading-model):
|
46
|
+
|
47
|
+
In practice, it means two things:
|
48
|
+
|
49
|
+
1. Now that the client is buffering metrics before sending them, you have to call `Datadog::Statsd#flush(sync: true)` if you want synchronous behavior. In most cases, this is not needed, as the sender thread will automatically flush the buffered metrics if the buffer gets full or when you are closing the instance.
|
50
|
+
|
51
|
+
2. You have to make sure you are either:
|
52
|
+
|
53
|
+
* Using a singleton instance of the DogStatsD client instead of creating a new instance whenever you need one; this will let the buffering mechanism flush metrics regularly
|
54
|
+
* Or properly disposing of the DogStatsD client instance when it is not needed anymore using the method `Datadog::Statsd#close`
|
55
|
+
|
56
|
+
If you have issues with the sender thread or the buffering mode, you can instantiate a client that behaves exactly as in v4.x (i.e. no sender thread and flush on every metric submission):
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# Create a DogStatsD client instance using UDP
|
60
|
+
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true, buffer_max_pool_size: 1)
|
61
|
+
# ...
|
62
|
+
statsd.close()
|
63
|
+
```
|
64
|
+
|
65
|
+
or
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# Create a DogStatsD client instance using UDS
|
69
|
+
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file', single_thread: true, buffer_max_pool_size: 1)
|
70
|
+
# ...
|
71
|
+
statsd.close()
|
72
|
+
```
|
73
|
+
|
74
|
+
### v5.x Common Pitfalls
|
75
|
+
|
76
|
+
Version v5.x of `dogstatsd-ruby` is using a sender thread for flushing. This provides better performance, but you need to consider the following pitfalls:
|
77
|
+
|
78
|
+
1. Applications that use `fork` after having created the dogstatsd instance: the child process will automatically spawn a new sender thread to flush metrics.
|
79
|
+
|
80
|
+
2. Applications that create multiple instances of the client without closing them: it is important to `#close` all instances to free the thread and the socket they are using otherwise you will leak those resources.
|
81
|
+
|
82
|
+
If you are using [Sidekiq](https://github.com/mperham/sidekiq), please make sure to close the client instances that are instantiated. [See this example on using DogStatsD-ruby v5.x with Sidekiq](https://github.com/DataDog/dogstatsd-ruby/blob/master/examples/sidekiq_example.rb).
|
83
|
+
|
84
|
+
Applications that run into issues but can't apply these recommendations should use the `single_thread` mode which disables the use of the sender thread.
|
85
|
+
Here is how to instantiate a client in this mode:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true)
|
89
|
+
# ...
|
90
|
+
# release resources used by the client instance and flush last metrics
|
91
|
+
statsd.close()
|
92
|
+
```
|
35
93
|
|
36
94
|
### Origin detection over UDP
|
37
95
|
|
38
|
-
Origin detection is a method to detect which pod DogStatsD packets are coming from in order to add the pod's tags to the tag list.
|
96
|
+
Origin detection is a method to detect which pod DogStatsD packets are coming from, in order to add the pod's tags to the tag list.
|
97
|
+
|
98
|
+
To enable origin detection over UDP, add the following lines to your application manifest:
|
39
99
|
|
40
|
-
To enable origin detection over UDP, add the following lines to your application manifest
|
41
100
|
```yaml
|
42
101
|
env:
|
43
102
|
- name: DD_ENTITY_ID
|
@@ -45,49 +104,134 @@ env:
|
|
45
104
|
fieldRef:
|
46
105
|
fieldPath: metadata.uid
|
47
106
|
```
|
107
|
+
|
48
108
|
The DogStatsD client attaches an internal tag, `entity_id`. The value of this tag is the content of the `DD_ENTITY_ID` environment variable, which is the pod’s UID.
|
49
109
|
|
50
110
|
## Usage
|
51
111
|
|
52
|
-
In order to use DogStatsD metrics, events, and Service Checks the Agent must be [running and available](https://docs.datadoghq.com/developers/dogstatsd/?tab=ruby).
|
112
|
+
In order to use DogStatsD metrics, events, and Service Checks the Datadog Agent must be [running and available](https://docs.datadoghq.com/developers/dogstatsd/?tab=ruby).
|
53
113
|
|
54
114
|
### Metrics
|
55
115
|
|
56
|
-
After the client is created, you can start sending custom metrics to Datadog. See the dedicated [Metric Submission: DogStatsD documentation](https://docs.datadoghq.com/
|
116
|
+
After the client is created, you can start sending custom metrics to Datadog. See the dedicated [Metric Submission: DogStatsD documentation](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?tab=ruby) to see how to submit all supported metric types to Datadog with working code examples:
|
57
117
|
|
58
|
-
* [Submit a COUNT metric](https://docs.datadoghq.com/
|
59
|
-
* [Submit a GAUGE metric](https://docs.datadoghq.com/
|
60
|
-
* [Submit a SET metric](https://docs.datadoghq.com/
|
61
|
-
* [Submit a HISTOGRAM metric](https://docs.datadoghq.com/
|
62
|
-
* [Submit a DISTRIBUTION metric](https://docs.datadoghq.com/
|
118
|
+
* [Submit a COUNT metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#count).
|
119
|
+
* [Submit a GAUGE metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#gauge).
|
120
|
+
* [Submit a SET metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#set)
|
121
|
+
* [Submit a HISTOGRAM metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#histogram)
|
122
|
+
* [Submit a DISTRIBUTION metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#distribution)
|
63
123
|
|
64
|
-
Some options are suppported when submitting metrics, like [applying a Sample Rate to your metrics](https://docs.datadoghq.com/
|
124
|
+
Some options are suppported when submitting metrics, like [applying a Sample Rate to your metrics](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?tab=ruby#metric-submission-options) or [tagging your metrics with your custom tags](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?tab=ruby#metric-tagging). Find all the available functions to report metrics in the [DogStatsD-ruby rubydoc](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/Datadog/Statsd).
|
65
125
|
|
66
126
|
### Events
|
67
127
|
|
68
|
-
After the client is created, you can start sending events to your Datadog Event Stream. See the dedicated [Event Submission: DogStatsD documentation](https://docs.datadoghq.com/
|
128
|
+
After the client is created, you can start sending events to your Datadog Event Stream. See the dedicated [Event Submission: DogStatsD documentation](https://docs.datadoghq.com/events/guides/dogstatsd/?code-lang=ruby) to see how to submit an event to Datadog your Event Stream.
|
69
129
|
|
70
130
|
### Service Checks
|
71
131
|
|
72
132
|
After the client is created, you can start sending Service Checks to Datadog. See the dedicated [Service Check Submission: DogStatsD documentation](https://docs.datadoghq.com/developers/service_checks/dogstatsd_service_checks_submission/?tab=ruby) to see how to submit a Service Check to Datadog.
|
73
133
|
|
74
|
-
### Maximum
|
134
|
+
### Maximum packet size in high-throughput scenarios
|
75
135
|
|
76
136
|
In order to have the most efficient use of this library in high-throughput scenarios,
|
77
|
-
|
78
|
-
and UDP (1432 bytes)
|
79
|
-
|
80
|
-
|
137
|
+
recommended values for the maximum packet size have already been set for both UDS (8192 bytes)
|
138
|
+
and UDP (1432 bytes).
|
139
|
+
|
140
|
+
However, if are in control of your network and want to use a different value for the maximum packet
|
141
|
+
size, you can do it by setting the `buffer_max_payload_size` parameter:
|
81
142
|
|
82
143
|
```ruby
|
83
|
-
# Create a DogStatsD client instance.
|
84
144
|
statsd = Datadog::Statsd.new('localhost', 8125, buffer_max_payload_size: 4096)
|
145
|
+
# ...
|
146
|
+
statsd.close()
|
147
|
+
```
|
148
|
+
|
149
|
+
## Threading model
|
150
|
+
|
151
|
+
Starting with version 5.0, `dogstatsd-ruby` employs a new threading model where one instance of `Datadog::Statsd` can be shared between threads and where data sending is non-blocking (asynchronous).
|
152
|
+
|
153
|
+
When you instantiate a `Datadog::Statsd`, a sender thread is spawned. This thread will be called the Sender thread, as it is modeled by the [Sender](../lib/datadog/statsd/sender.rb) class. You can make use of `single_thread: true` to disable this behavior.
|
154
|
+
|
155
|
+
This thread is stopped when you close the statsd client (`Datadog::Statsd#close`). Instantiating a lot of statsd clients without calling `#close` after they are not needed anymore will most likely lead to threads being leaked.
|
156
|
+
|
157
|
+
The sender thread has the following logic (from `Datadog::Statsd::Sender#send_loop`):
|
158
|
+
|
159
|
+
```
|
160
|
+
while the sender message queue is not closed do
|
161
|
+
read message from sender message queue
|
162
|
+
|
163
|
+
if message is a Control message to flush
|
164
|
+
flush buffer in connection
|
165
|
+
else if message is a Control message to synchronize
|
166
|
+
synchronize with calling thread
|
167
|
+
else
|
168
|
+
add message to the buffer
|
169
|
+
end
|
170
|
+
end while
|
171
|
+
```
|
172
|
+
|
173
|
+
There are three different kinds of messages:
|
174
|
+
|
175
|
+
1. a control message to flush the buffer in the connection
|
176
|
+
2. a control message to synchronize any thread with the sender thread
|
177
|
+
3. a message to append to the buffer
|
178
|
+
|
179
|
+
There is also an implicit message which closes the queue which will cause the sender thread to finish processing and exit.
|
180
|
+
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
statsd = Datadog::Statsd.new('localhost', 8125)
|
85
184
|
```
|
86
185
|
|
186
|
+
The message queue's maximum size (in messages) is given by the `sender_queue_size` argument, and has appropriate defaults for UDP (2048), UDS (512) and `single_thread: true` (1).
|
187
|
+
|
188
|
+
The `buffer_flush_interval`, if enabled, is implemented with an additional thread which manages the timing of those flushes. This additional thread is used even if `single_thread: true`.
|
189
|
+
|
190
|
+
### Usual workflow
|
191
|
+
|
192
|
+
You push metrics to the statsd client which writes them quickly to the sender message queue. The sender thread receives those message, buffers them and flushes them to the connection when the buffer limit is reached.
|
193
|
+
|
194
|
+
### Flushing
|
195
|
+
|
196
|
+
When calling `Datadog::Statsd#flush`, a specific control message (`:flush`) is sent to the sender thread. When the sender thread receives it, it flushes its internal buffer into the connection.
|
197
|
+
|
198
|
+
### Rendez-vous
|
199
|
+
|
200
|
+
It is possible to ensure a message has been consumed by the sender thread and written to the buffer by simply calling a rendez-vous right after. This is done when you are doing a synchronous flush using `Datadog::Statsd#flush(sync: true)`.
|
201
|
+
|
202
|
+
Doing so means the caller thread is blocked and waiting until the data has been flushed by the sender thread.
|
203
|
+
|
204
|
+
This is useful when preparing to exit the application or when checking unit tests.
|
205
|
+
|
206
|
+
### Thread-safety
|
207
|
+
|
208
|
+
By default, instances of `Datadog::Statsd` are thread-safe and we recommend that a single instance be reused by all application threads (even in applications that employ forking). The sole exception is the `#close` method — this method is not yet thread safe (work in progress here [#209](https://github.com/DataDog/dogstatsd-ruby/pull/209)).
|
209
|
+
|
210
|
+
When using the `single_thread: true` mode, instances of `Datadog::Statsd` are still thread-safe, but you may run into contention on heavily-threaded applications, so we don’t recommend (for performance reasons) reusing these instances.
|
211
|
+
|
212
|
+
### Delaying serialization
|
213
|
+
|
214
|
+
By default, message serialization happens synchronously whenever stat methods such as `#increment` gets called, blocking the caller. If the blocking is impacting your program's performance, you may want to consider the `delay_serialization: true` mode.
|
215
|
+
|
216
|
+
The `delay_serialization: true` mode delays the serialization of metrics to avoid the wait when submitting metrics. Serialization will still have to happen at some point, but it might be postponed until a more convenient time, such as after an HTTP request has completed.
|
217
|
+
|
218
|
+
In `single_thread: true` mode, you'll probably want to set `sender_queue_size:` from it's default of `1` to some greater value, so that it can benefit from `delay_serialization: true`. Messages will then be queued unserialized in the sender queue and processed normally whenever `sender_queue_size` is reached or `#flush` is called. You might set `sender_queue_size: Float::INFINITY` to allow for an unbounded queue that will only be processed on explicit `#flush`.
|
219
|
+
|
220
|
+
In `single_thread: false` mode, `delay_serialization: true`, will cause serialization to happen inside the sender thread.
|
221
|
+
|
222
|
+
## Versioning
|
223
|
+
|
224
|
+
This Ruby gem is using [Semantic Versioning](https://guides.rubygems.org/patterns/#semantic-versioning) but please note that supported Ruby versions can change in a minor release of this library.
|
225
|
+
As much as possible, we will add a "future deprecation" message in the minor release preceding the one dropping the support.
|
226
|
+
|
227
|
+
## Ruby Versions
|
228
|
+
|
229
|
+
This gem supports and is tested on Ruby minor versions 2.1 through 3.1.
|
230
|
+
Support for Ruby 2.0 was dropped in version 5.4.0.
|
231
|
+
|
87
232
|
## Credits
|
88
233
|
|
89
|
-
dogstatsd-ruby is forked from
|
90
|
-
client](https://github.com/reinh/statsd).
|
234
|
+
dogstatsd-ruby is forked from Rein Henrichs' [original Statsd client](https://github.com/reinh/statsd).
|
91
235
|
|
92
236
|
Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
|
93
237
|
further details.
|
@@ -3,32 +3,24 @@
|
|
3
3
|
module Datadog
|
4
4
|
class Statsd
|
5
5
|
class Connection
|
6
|
-
def initialize(telemetry)
|
6
|
+
def initialize(telemetry: nil, logger: nil)
|
7
7
|
@telemetry = telemetry
|
8
|
+
@logger = logger
|
8
9
|
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
begin
|
13
|
-
@socket && @socket.close if instance_variable_defined?(:@socket)
|
14
|
-
rescue StandardError => boom
|
15
|
-
logger.error { "Statsd: #{boom.class} #{boom}" } if logger
|
16
|
-
end
|
17
|
-
@socket = nil
|
11
|
+
def reset_telemetry
|
12
|
+
telemetry.reset if telemetry
|
18
13
|
end
|
19
14
|
|
15
|
+
# not thread safe: `Sender` instances that use this are required to properly synchronize or sequence calls to this method
|
20
16
|
def write(payload)
|
21
17
|
logger.debug { "Statsd: #{payload}" } if logger
|
22
18
|
|
23
|
-
flush_telemetry = telemetry.flush?
|
24
|
-
|
25
|
-
payload += telemetry.flush if flush_telemetry
|
26
|
-
|
27
19
|
send_message(payload)
|
28
20
|
|
29
|
-
telemetry.
|
21
|
+
telemetry.sent(packets: 1, bytes: payload.length) if telemetry
|
30
22
|
|
31
|
-
|
23
|
+
true
|
32
24
|
rescue StandardError => boom
|
33
25
|
# Try once to reconnect if the socket has been closed
|
34
26
|
retries ||= 1
|
@@ -39,23 +31,29 @@ module Datadog
|
|
39
31
|
retries += 1
|
40
32
|
begin
|
41
33
|
close
|
34
|
+
connect
|
42
35
|
retry
|
43
36
|
rescue StandardError => e
|
44
37
|
boom = e
|
45
38
|
end
|
46
39
|
end
|
47
40
|
|
48
|
-
telemetry.
|
41
|
+
telemetry.dropped_writer(packets: 1, bytes: payload.length) if telemetry
|
49
42
|
logger.error { "Statsd: #{boom.class} #{boom}" } if logger
|
50
43
|
nil
|
51
44
|
end
|
52
45
|
|
53
46
|
private
|
47
|
+
|
54
48
|
attr_reader :telemetry
|
55
49
|
attr_reader :logger
|
56
50
|
|
57
|
-
def
|
58
|
-
|
51
|
+
def connect
|
52
|
+
raise 'Should be implemented by subclass'
|
53
|
+
end
|
54
|
+
|
55
|
+
def close
|
56
|
+
raise 'Should be implemented by subclass'
|
59
57
|
end
|
60
58
|
end
|
61
59
|
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
module Datadog
|
2
|
+
class Statsd
|
3
|
+
class ConnectionCfg
|
4
|
+
attr_reader :host
|
5
|
+
attr_reader :port
|
6
|
+
attr_reader :socket_path
|
7
|
+
attr_reader :transport_type
|
8
|
+
|
9
|
+
def initialize(host: nil, port: nil, socket_path: nil)
|
10
|
+
initialize_with_constructor_args(host: host, port: port, socket_path: socket_path) ||
|
11
|
+
initialize_with_env_vars ||
|
12
|
+
initialize_with_defaults
|
13
|
+
end
|
14
|
+
|
15
|
+
def make_connection(**params)
|
16
|
+
case @transport_type
|
17
|
+
when :udp
|
18
|
+
UDPConnection.new(@host, @port, **params)
|
19
|
+
when :uds
|
20
|
+
UDSConnection.new(@socket_path, **params)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
ERROR_MESSAGE = "Valid environment variables combination for connection configuration:\n" +
|
27
|
+
" - DD_DOGSTATSD_URL for UDP or UDS connection.\n" +
|
28
|
+
" Example for UDP: DD_DOGSTATSD_URL='udp://localhost:8125'\n" +
|
29
|
+
" Example for UDS: DD_DOGSTATSD_URL='unix:///path/to/unix.sock'\n" +
|
30
|
+
" or\n" +
|
31
|
+
" - DD_AGENT_HOST and DD_DOGSTATSD_PORT for an UDP connection. E.g. DD_AGENT_HOST='localhost' DD_DOGSTATSD_PORT=8125\n" +
|
32
|
+
" or\n" +
|
33
|
+
" - DD_DOGSTATSD_SOCKET for an UDS connection: E.g. DD_DOGSTATSD_SOCKET='/path/to/unix.sock'\n" +
|
34
|
+
" Note that DD_DOGSTATSD_URL has priority on other environment variables."
|
35
|
+
|
36
|
+
DEFAULT_HOST = '127.0.0.1'
|
37
|
+
DEFAULT_PORT = 8125
|
38
|
+
|
39
|
+
UDP_PREFIX = 'udp://'
|
40
|
+
UDS_PREFIX = 'unix://'
|
41
|
+
|
42
|
+
def initialize_with_constructor_args(host: nil, port: nil, socket_path: nil)
|
43
|
+
try_initialize_with(host: host, port: port, socket_path: socket_path,
|
44
|
+
error_message:
|
45
|
+
"Both UDP: (host/port #{host}:#{port}) and UDS (socket_path #{socket_path}) " +
|
46
|
+
"constructor arguments were given. Use only one or the other.",
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialize_with_env_vars()
|
51
|
+
try_initialize_with(
|
52
|
+
dogstatsd_url: ENV['DD_DOGSTATSD_URL'],
|
53
|
+
host: ENV['DD_AGENT_HOST'],
|
54
|
+
port: ENV['DD_DOGSTATSD_PORT'] && ENV['DD_DOGSTATSD_PORT'].to_i,
|
55
|
+
socket_path: ENV['DD_DOGSTATSD_SOCKET'],
|
56
|
+
error_message: ERROR_MESSAGE,
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
def initialize_with_defaults()
|
61
|
+
try_initialize_with(host: DEFAULT_HOST, port: DEFAULT_PORT)
|
62
|
+
end
|
63
|
+
|
64
|
+
def try_initialize_with(dogstatsd_url: nil, host: nil, port: nil, socket_path: nil, error_message: ERROR_MESSAGE)
|
65
|
+
if (host || port) && socket_path
|
66
|
+
raise ArgumentError, error_message
|
67
|
+
end
|
68
|
+
|
69
|
+
if dogstatsd_url
|
70
|
+
host, port, socket_path = parse_dogstatsd_url(str: dogstatsd_url.to_s)
|
71
|
+
end
|
72
|
+
|
73
|
+
if host || port
|
74
|
+
@host = host || DEFAULT_HOST
|
75
|
+
@port = port || DEFAULT_PORT
|
76
|
+
@socket_path = nil
|
77
|
+
@transport_type = :udp
|
78
|
+
return true
|
79
|
+
elsif socket_path
|
80
|
+
@host = nil
|
81
|
+
@port = nil
|
82
|
+
@socket_path = socket_path
|
83
|
+
@transport_type = :uds
|
84
|
+
return true
|
85
|
+
end
|
86
|
+
|
87
|
+
return false
|
88
|
+
end
|
89
|
+
|
90
|
+
def parse_dogstatsd_url(str:)
|
91
|
+
# udp socket connection
|
92
|
+
|
93
|
+
if str.start_with?(UDP_PREFIX)
|
94
|
+
dogstatsd_url = str[UDP_PREFIX.size..str.size]
|
95
|
+
host = nil
|
96
|
+
port = nil
|
97
|
+
|
98
|
+
if dogstatsd_url.include?(":")
|
99
|
+
parts = dogstatsd_url.split(":")
|
100
|
+
if parts.size > 2
|
101
|
+
raise ArgumentError, "Error: DD_DOGSTATSD_URL wrong format for an UDP connection. E.g. 'udp://localhost:8125'"
|
102
|
+
end
|
103
|
+
|
104
|
+
host = parts[0]
|
105
|
+
port = parts[1].to_i
|
106
|
+
else
|
107
|
+
host = dogstatsd_url
|
108
|
+
end
|
109
|
+
|
110
|
+
return host, port, nil
|
111
|
+
end
|
112
|
+
|
113
|
+
# unix socket connection
|
114
|
+
|
115
|
+
if str.start_with?(UDS_PREFIX)
|
116
|
+
return nil, nil, str[UDS_PREFIX.size..str.size]
|
117
|
+
end
|
118
|
+
|
119
|
+
# malformed value
|
120
|
+
|
121
|
+
raise ArgumentError, "Error: DD_DOGSTATSD_URL has been provided but is not starting with 'udp://' nor 'unix://'"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Datadog
|
4
|
+
class Statsd
|
5
|
+
class Forwarder
|
6
|
+
attr_reader :telemetry
|
7
|
+
attr_reader :transport_type
|
8
|
+
|
9
|
+
def initialize(
|
10
|
+
connection_cfg: nil,
|
11
|
+
|
12
|
+
buffer_max_payload_size: nil,
|
13
|
+
buffer_max_pool_size: nil,
|
14
|
+
buffer_overflowing_stategy: :drop,
|
15
|
+
buffer_flush_interval: nil,
|
16
|
+
|
17
|
+
sender_queue_size: nil,
|
18
|
+
|
19
|
+
telemetry_flush_interval: nil,
|
20
|
+
global_tags: [],
|
21
|
+
|
22
|
+
single_thread: false,
|
23
|
+
|
24
|
+
logger: nil,
|
25
|
+
|
26
|
+
serializer:
|
27
|
+
)
|
28
|
+
@transport_type = connection_cfg.transport_type
|
29
|
+
|
30
|
+
@telemetry = if telemetry_flush_interval
|
31
|
+
Telemetry.new(telemetry_flush_interval,
|
32
|
+
global_tags: global_tags,
|
33
|
+
transport_type: @transport_type
|
34
|
+
)
|
35
|
+
else
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
|
39
|
+
@connection = connection_cfg.make_connection(logger: logger, telemetry: telemetry)
|
40
|
+
|
41
|
+
# Initialize buffer
|
42
|
+
buffer_max_payload_size ||= (@transport_type == :udp ?
|
43
|
+
UDP_DEFAULT_BUFFER_SIZE : UDS_DEFAULT_BUFFER_SIZE)
|
44
|
+
|
45
|
+
if buffer_max_payload_size <= 0
|
46
|
+
raise ArgumentError, 'buffer_max_payload_size cannot be <= 0'
|
47
|
+
end
|
48
|
+
|
49
|
+
unless telemetry.nil? || telemetry.would_fit_in?(buffer_max_payload_size)
|
50
|
+
raise ArgumentError, "buffer_max_payload_size is not high enough to use telemetry (tags=(#{global_tags.inspect}))"
|
51
|
+
end
|
52
|
+
|
53
|
+
buffer = MessageBuffer.new(@connection,
|
54
|
+
max_payload_size: buffer_max_payload_size,
|
55
|
+
max_pool_size: buffer_max_pool_size || DEFAULT_BUFFER_POOL_SIZE,
|
56
|
+
overflowing_stategy: buffer_overflowing_stategy,
|
57
|
+
serializer: serializer
|
58
|
+
)
|
59
|
+
|
60
|
+
sender_queue_size ||= 1 if single_thread
|
61
|
+
sender_queue_size ||= (@transport_type == :udp ?
|
62
|
+
UDP_DEFAULT_SENDER_QUEUE_SIZE : UDS_DEFAULT_SENDER_QUEUE_SIZE)
|
63
|
+
|
64
|
+
@sender = single_thread ?
|
65
|
+
SingleThreadSender.new(
|
66
|
+
buffer,
|
67
|
+
logger: logger,
|
68
|
+
flush_interval: buffer_flush_interval,
|
69
|
+
queue_size: sender_queue_size) :
|
70
|
+
Sender.new(
|
71
|
+
buffer,
|
72
|
+
logger: logger,
|
73
|
+
flush_interval: buffer_flush_interval,
|
74
|
+
telemetry: @telemetry,
|
75
|
+
queue_size: sender_queue_size)
|
76
|
+
@sender.start
|
77
|
+
end
|
78
|
+
|
79
|
+
def send_message(message)
|
80
|
+
sender.add(message)
|
81
|
+
|
82
|
+
tick_telemetry
|
83
|
+
end
|
84
|
+
|
85
|
+
def sync_with_outbound_io
|
86
|
+
sender.rendez_vous
|
87
|
+
end
|
88
|
+
|
89
|
+
def flush(flush_telemetry: false, sync: false)
|
90
|
+
do_flush_telemetry if telemetry && flush_telemetry
|
91
|
+
|
92
|
+
sender.flush(sync: sync)
|
93
|
+
end
|
94
|
+
|
95
|
+
def host
|
96
|
+
return nil unless transport_type == :udp
|
97
|
+
|
98
|
+
connection.host
|
99
|
+
end
|
100
|
+
|
101
|
+
def port
|
102
|
+
return nil unless transport_type == :udp
|
103
|
+
|
104
|
+
connection.port
|
105
|
+
end
|
106
|
+
|
107
|
+
def socket_path
|
108
|
+
return nil unless transport_type == :uds
|
109
|
+
|
110
|
+
connection.socket_path
|
111
|
+
end
|
112
|
+
|
113
|
+
def close
|
114
|
+
sender.stop
|
115
|
+
connection.close
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
attr_reader :sender
|
120
|
+
attr_reader :connection
|
121
|
+
|
122
|
+
def do_flush_telemetry
|
123
|
+
telemetry_snapshot = telemetry.flush
|
124
|
+
telemetry.reset
|
125
|
+
|
126
|
+
telemetry_snapshot.each do |message|
|
127
|
+
sender.add(message)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def tick_telemetry
|
132
|
+
return nil unless telemetry
|
133
|
+
|
134
|
+
do_flush_telemetry if telemetry.should_flush?
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|