dogstatsd-ruby 5.2.0 → 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 +89 -70
- data/lib/datadog/statsd/connection.rb +12 -11
- data/lib/datadog/statsd/connection_cfg.rb +125 -0
- data/lib/datadog/statsd/forwarder.rb +34 -18
- data/lib/datadog/statsd/message_buffer.rb +22 -5
- data/lib/datadog/statsd/sender.rb +80 -13
- data/lib/datadog/statsd/single_thread_sender.rb +56 -5
- data/lib/datadog/statsd/telemetry.rb +22 -1
- data/lib/datadog/statsd/timer.rb +61 -0
- data/lib/datadog/statsd/udp_connection.rb +22 -11
- data/lib/datadog/statsd/uds_connection.rb +16 -5
- data/lib/datadog/statsd/version.rb +1 -1
- data/lib/datadog/statsd.rb +67 -18
- metadata +15 -9
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,9 +22,9 @@ 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
|
-
...
|
27
|
+
# ...
|
28
28
|
# release resources used by the client instance
|
29
29
|
statsd.close()
|
30
30
|
```
|
@@ -32,85 +32,71 @@ Or if you want to connect over Unix Domain Socket:
|
|
32
32
|
```ruby
|
33
33
|
# Connection over Unix Domain Socket
|
34
34
|
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file')
|
35
|
-
...
|
35
|
+
# ...
|
36
36
|
# release resources used by the client instance
|
37
37
|
statsd.close()
|
38
38
|
```
|
39
39
|
|
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/?
|
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
41
|
|
42
42
|
### Migrating from v4.x to v5.x
|
43
43
|
|
44
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
|
45
|
+
change concerning you is the new [threading model](#threading-model):
|
46
46
|
|
47
47
|
In practice, it means two things:
|
48
48
|
|
49
|
-
1. Now that the client is buffering metrics before sending them, you have to
|
50
|
-
call the method `Datadog::Statsd#flush` if you want to force the sending of metrics. Note that the companion thread will automatically flush the buffered metrics if the buffer gets full or when you are closing the instance.
|
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.
|
51
50
|
|
52
51
|
2. You have to make sure you are either:
|
53
52
|
|
54
|
-
*
|
55
|
-
* properly
|
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`
|
56
55
|
|
57
|
-
If you have issues with the
|
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):
|
58
57
|
|
59
58
|
```ruby
|
60
|
-
# Import the library
|
61
|
-
require 'datadog/statsd'
|
62
|
-
|
63
59
|
# Create a DogStatsD client instance using UDP
|
64
|
-
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true,
|
65
|
-
...
|
66
|
-
# to close the instance is not necessary in this case since metrics are flushed on submission
|
67
|
-
# but it is still a good practice and it explicitely closes the socket
|
60
|
+
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true, buffer_max_pool_size: 1)
|
61
|
+
# ...
|
68
62
|
statsd.close()
|
69
63
|
```
|
70
64
|
|
71
65
|
or
|
72
66
|
|
73
67
|
```ruby
|
74
|
-
# Import the library
|
75
|
-
require 'datadog/statsd'
|
76
|
-
|
77
68
|
# Create a DogStatsD client instance using UDS
|
78
|
-
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file', single_thread: true,
|
79
|
-
...
|
80
|
-
# to close the instance is not necessary in this case since metrics are flushed on submission
|
81
|
-
# but it is still a good practice and it explicitely closes the socket
|
69
|
+
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file', single_thread: true, buffer_max_pool_size: 1)
|
70
|
+
# ...
|
82
71
|
statsd.close()
|
83
72
|
```
|
84
73
|
|
85
74
|
### v5.x Common Pitfalls
|
86
75
|
|
87
|
-
Version v5.x of `dogstatsd-ruby` is using a
|
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:
|
88
77
|
|
89
|
-
|
90
|
-
* Applications creating a lot of different instances of the client without closing them: it is important to close the instance to free the thread and the socket it is using or it will lead to thread leaks.
|
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.
|
91
79
|
|
92
|
-
|
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.
|
93
81
|
|
94
|
-
If you are using [
|
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).
|
95
83
|
|
96
|
-
Applications that
|
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:
|
97
86
|
|
98
87
|
```ruby
|
99
|
-
# Import the library
|
100
|
-
require 'datadog/statsd'
|
101
|
-
|
102
|
-
# Create a DogStatsD client instance.
|
103
88
|
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true)
|
104
|
-
...
|
89
|
+
# ...
|
105
90
|
# release resources used by the client instance and flush last metrics
|
106
91
|
statsd.close()
|
107
92
|
```
|
108
93
|
|
109
94
|
### Origin detection over UDP
|
110
95
|
|
111
|
-
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:
|
112
99
|
|
113
|
-
To enable origin detection over UDP, add the following lines to your application manifest
|
114
100
|
```yaml
|
115
101
|
env:
|
116
102
|
- name: DD_ENTITY_ID
|
@@ -118,56 +104,57 @@ env:
|
|
118
104
|
fieldRef:
|
119
105
|
fieldPath: metadata.uid
|
120
106
|
```
|
107
|
+
|
121
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.
|
122
109
|
|
123
110
|
## Usage
|
124
111
|
|
125
|
-
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).
|
126
113
|
|
127
114
|
### Metrics
|
128
115
|
|
129
|
-
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:
|
130
117
|
|
131
|
-
* [Submit a COUNT metric](https://docs.datadoghq.com/
|
132
|
-
* [Submit a GAUGE metric](https://docs.datadoghq.com/
|
133
|
-
* [Submit a SET metric](https://docs.datadoghq.com/
|
134
|
-
* [Submit a HISTOGRAM metric](https://docs.datadoghq.com/
|
135
|
-
* [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)
|
136
123
|
|
137
|
-
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).
|
138
125
|
|
139
126
|
### Events
|
140
127
|
|
141
|
-
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.
|
142
129
|
|
143
130
|
### Service Checks
|
144
131
|
|
145
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.
|
146
133
|
|
147
|
-
### Maximum
|
134
|
+
### Maximum packet size in high-throughput scenarios
|
148
135
|
|
149
136
|
In order to have the most efficient use of this library in high-throughput scenarios,
|
150
|
-
|
151
|
-
and UDP (1432 bytes)
|
152
|
-
|
153
|
-
|
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:
|
154
142
|
|
155
143
|
```ruby
|
156
|
-
# Create a DogStatsD client instance.
|
157
144
|
statsd = Datadog::Statsd.new('localhost', 8125, buffer_max_payload_size: 4096)
|
145
|
+
# ...
|
146
|
+
statsd.close()
|
158
147
|
```
|
159
148
|
|
160
149
|
## Threading model
|
161
150
|
|
162
|
-
|
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).
|
163
152
|
|
164
|
-
When you instantiate a `Datadog::Statsd`, a
|
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.
|
165
154
|
|
166
|
-
This thread is stopped when you close the statsd client (`Datadog::Statsd#close`).
|
167
|
-
could lead to a thread leak (even though they will be sleeping, blocked on IO).
|
168
|
-
The communication between the current thread is managed through a standard Ruby Queue.
|
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.
|
169
156
|
|
170
|
-
The sender thread has the following logic (
|
157
|
+
The sender thread has the following logic (from `Datadog::Statsd::Sender#send_loop`):
|
171
158
|
|
172
159
|
```
|
173
160
|
while the sender message queue is not closed do
|
@@ -183,15 +170,22 @@ while the sender message queue is not closed do
|
|
183
170
|
end while
|
184
171
|
```
|
185
172
|
|
186
|
-
|
173
|
+
There are three different kinds of messages:
|
187
174
|
|
188
|
-
|
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
|
189
178
|
|
190
|
-
|
191
|
-
* a control message to synchronize any thread with the sender thread
|
192
|
-
* a message to append to the buffer
|
179
|
+
There is also an implicit message which closes the queue which will cause the sender thread to finish processing and exit.
|
193
180
|
|
194
|
-
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
statsd = Datadog::Statsd.new('localhost', 8125)
|
184
|
+
```
|
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`.
|
195
189
|
|
196
190
|
### Usual workflow
|
197
191
|
|
@@ -199,20 +193,45 @@ You push metrics to the statsd client which writes them quickly to the sender me
|
|
199
193
|
|
200
194
|
### Flushing
|
201
195
|
|
202
|
-
When calling
|
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.
|
203
197
|
|
204
198
|
### Rendez-vous
|
205
199
|
|
206
|
-
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
|
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.
|
207
226
|
|
208
|
-
|
227
|
+
## Ruby Versions
|
209
228
|
|
210
|
-
This
|
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.
|
211
231
|
|
212
232
|
## Credits
|
213
233
|
|
214
|
-
dogstatsd-ruby is forked from Rein Henrichs [original Statsd
|
215
|
-
client](https://github.com/reinh/statsd).
|
234
|
+
dogstatsd-ruby is forked from Rein Henrichs' [original Statsd client](https://github.com/reinh/statsd).
|
216
235
|
|
217
236
|
Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
|
218
237
|
further details.
|
@@ -8,16 +8,11 @@ module Datadog
|
|
8
8
|
@logger = logger
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
begin
|
14
|
-
@socket && @socket.close if instance_variable_defined?(:@socket)
|
15
|
-
rescue StandardError => boom
|
16
|
-
logger.error { "Statsd: #{boom.class} #{boom}" } if logger
|
17
|
-
end
|
18
|
-
@socket = nil
|
11
|
+
def reset_telemetry
|
12
|
+
telemetry.reset if telemetry
|
19
13
|
end
|
20
14
|
|
15
|
+
# not thread safe: `Sender` instances that use this are required to properly synchronize or sequence calls to this method
|
21
16
|
def write(payload)
|
22
17
|
logger.debug { "Statsd: #{payload}" } if logger
|
23
18
|
|
@@ -36,23 +31,29 @@ module Datadog
|
|
36
31
|
retries += 1
|
37
32
|
begin
|
38
33
|
close
|
34
|
+
connect
|
39
35
|
retry
|
40
36
|
rescue StandardError => e
|
41
37
|
boom = e
|
42
38
|
end
|
43
39
|
end
|
44
40
|
|
45
|
-
telemetry.
|
41
|
+
telemetry.dropped_writer(packets: 1, bytes: payload.length) if telemetry
|
46
42
|
logger.error { "Statsd: #{boom.class} #{boom}" } if logger
|
47
43
|
nil
|
48
44
|
end
|
49
45
|
|
50
46
|
private
|
47
|
+
|
51
48
|
attr_reader :telemetry
|
52
49
|
attr_reader :logger
|
53
50
|
|
54
|
-
def
|
55
|
-
|
51
|
+
def connect
|
52
|
+
raise 'Should be implemented by subclass'
|
53
|
+
end
|
54
|
+
|
55
|
+
def close
|
56
|
+
raise 'Should be implemented by subclass'
|
56
57
|
end
|
57
58
|
end
|
58
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
|
@@ -7,39 +7,40 @@ module Datadog
|
|
7
7
|
attr_reader :transport_type
|
8
8
|
|
9
9
|
def initialize(
|
10
|
-
|
11
|
-
port: nil,
|
12
|
-
socket_path: nil,
|
10
|
+
connection_cfg: nil,
|
13
11
|
|
14
12
|
buffer_max_payload_size: nil,
|
15
13
|
buffer_max_pool_size: nil,
|
16
14
|
buffer_overflowing_stategy: :drop,
|
15
|
+
buffer_flush_interval: nil,
|
16
|
+
|
17
|
+
sender_queue_size: nil,
|
17
18
|
|
18
19
|
telemetry_flush_interval: nil,
|
19
20
|
global_tags: [],
|
20
21
|
|
21
22
|
single_thread: false,
|
22
23
|
|
23
|
-
logger: nil
|
24
|
+
logger: nil,
|
25
|
+
|
26
|
+
serializer:
|
24
27
|
)
|
25
|
-
@transport_type =
|
28
|
+
@transport_type = connection_cfg.transport_type
|
26
29
|
|
27
|
-
if telemetry_flush_interval
|
28
|
-
|
30
|
+
@telemetry = if telemetry_flush_interval
|
31
|
+
Telemetry.new(telemetry_flush_interval,
|
29
32
|
global_tags: global_tags,
|
30
|
-
transport_type: transport_type
|
33
|
+
transport_type: @transport_type
|
31
34
|
)
|
35
|
+
else
|
36
|
+
nil
|
32
37
|
end
|
33
38
|
|
34
|
-
@connection =
|
35
|
-
when :udp
|
36
|
-
UDPConnection.new(host, port, logger: logger, telemetry: telemetry)
|
37
|
-
when :uds
|
38
|
-
UDSConnection.new(socket_path, logger: logger, telemetry: telemetry)
|
39
|
-
end
|
39
|
+
@connection = connection_cfg.make_connection(logger: logger, telemetry: telemetry)
|
40
40
|
|
41
41
|
# Initialize buffer
|
42
|
-
buffer_max_payload_size ||= (transport_type == :udp ?
|
42
|
+
buffer_max_payload_size ||= (@transport_type == :udp ?
|
43
|
+
UDP_DEFAULT_BUFFER_SIZE : UDS_DEFAULT_BUFFER_SIZE)
|
43
44
|
|
44
45
|
if buffer_max_payload_size <= 0
|
45
46
|
raise ArgumentError, 'buffer_max_payload_size cannot be <= 0'
|
@@ -49,13 +50,29 @@ module Datadog
|
|
49
50
|
raise ArgumentError, "buffer_max_payload_size is not high enough to use telemetry (tags=(#{global_tags.inspect}))"
|
50
51
|
end
|
51
52
|
|
52
|
-
|
53
|
+
buffer = MessageBuffer.new(@connection,
|
53
54
|
max_payload_size: buffer_max_payload_size,
|
54
55
|
max_pool_size: buffer_max_pool_size || DEFAULT_BUFFER_POOL_SIZE,
|
55
56
|
overflowing_stategy: buffer_overflowing_stategy,
|
57
|
+
serializer: serializer
|
56
58
|
)
|
57
59
|
|
58
|
-
|
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)
|
59
76
|
@sender.start
|
60
77
|
end
|
61
78
|
|
@@ -99,7 +116,6 @@ module Datadog
|
|
99
116
|
end
|
100
117
|
|
101
118
|
private
|
102
|
-
attr_reader :buffer
|
103
119
|
attr_reader :sender
|
104
120
|
attr_reader :connection
|
105
121
|
|
@@ -8,7 +8,8 @@ module Datadog
|
|
8
8
|
def initialize(connection,
|
9
9
|
max_payload_size: nil,
|
10
10
|
max_pool_size: DEFAULT_BUFFER_POOL_SIZE,
|
11
|
-
overflowing_stategy: :drop
|
11
|
+
overflowing_stategy: :drop,
|
12
|
+
serializer:
|
12
13
|
)
|
13
14
|
raise ArgumentError, 'max_payload_size keyword argument must be provided' unless max_payload_size
|
14
15
|
raise ArgumentError, 'max_pool_size keyword argument must be provided' unless max_pool_size
|
@@ -17,12 +18,19 @@ module Datadog
|
|
17
18
|
@max_payload_size = max_payload_size
|
18
19
|
@max_pool_size = max_pool_size
|
19
20
|
@overflowing_stategy = overflowing_stategy
|
21
|
+
@serializer = serializer
|
20
22
|
|
21
23
|
@buffer = String.new
|
22
|
-
|
24
|
+
clear_buffer
|
23
25
|
end
|
24
26
|
|
25
27
|
def add(message)
|
28
|
+
# Serializes the message if it hasn't been already. Part of the
|
29
|
+
# delay_serialization feature.
|
30
|
+
if message.is_a?(Array)
|
31
|
+
message = @serializer.to_stat(*message[0], **message[1])
|
32
|
+
end
|
33
|
+
|
26
34
|
message_size = message.bytesize
|
27
35
|
|
28
36
|
return nil unless message_size > 0 # to avoid adding empty messages to the buffer
|
@@ -42,16 +50,20 @@ module Datadog
|
|
42
50
|
true
|
43
51
|
end
|
44
52
|
|
53
|
+
def reset
|
54
|
+
clear_buffer
|
55
|
+
connection.reset_telemetry
|
56
|
+
end
|
57
|
+
|
45
58
|
def flush
|
46
59
|
return if buffer.empty?
|
47
60
|
|
48
61
|
connection.write(buffer)
|
49
|
-
|
50
|
-
buffer.clear
|
51
|
-
@message_count = 0
|
62
|
+
clear_buffer
|
52
63
|
end
|
53
64
|
|
54
65
|
private
|
66
|
+
|
55
67
|
attr :max_payload_size
|
56
68
|
attr :max_pool_size
|
57
69
|
|
@@ -66,6 +78,11 @@ module Datadog
|
|
66
78
|
false
|
67
79
|
end
|
68
80
|
|
81
|
+
def clear_buffer
|
82
|
+
buffer.clear
|
83
|
+
@message_count = 0
|
84
|
+
end
|
85
|
+
|
69
86
|
def preemptive_flush?
|
70
87
|
@message_count == max_pool_size || buffer.bytesize > bytesize_threshold
|
71
88
|
end
|