dogstatsd-ruby 5.2.0 → 5.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -68
- data/lib/datadog/statsd/connection.rb +10 -10
- data/lib/datadog/statsd/forwarder.rb +2 -4
- data/lib/datadog/statsd/message_buffer.rb +13 -4
- data/lib/datadog/statsd/sender.rb +51 -7
- data/lib/datadog/statsd/single_thread_sender.rb +34 -3
- data/lib/datadog/statsd/threaded_sender.rb +132 -0
- data/lib/datadog/statsd/udp_connection.rb +12 -4
- data/lib/datadog/statsd/uds_connection.rb +12 -5
- data/lib/datadog/statsd/version.rb +1 -1
- data/lib/datadog/statsd.rb +16 -0
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 828679b3ff6ef3a2acce023e8b3cc5d362fe8c41ac17acee8bd2d5662ae3b2f3
|
4
|
+
data.tar.gz: 0c836007b9b82eb7e0cf056f86d9426e8f43b1e1543876c53819b8e0f46c8235
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc31df8c212fa4ab0854075528a03729d8600fabbc2e387ec4bf4c3c712a5106600c68065d5204d082f3d9fbaeafa74c8dc6cee15c8dea03fe7de192bcfb654e
|
7
|
+
data.tar.gz: 552b383c99f0071662d83991dc5e3472b0cc73125ef9fa3c558de1a59b8bf48fed3dc0d7b96e62808cf0bb044e65c4e332e569e7c9add6dcaf72a0300bfb4ca3
|
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,73 @@ 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 companion 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
56
|
If you have issues with the companion thread or the buffering mode, you can instantiate a client that behaves exactly as in v4.x (i.e. no companion 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 companion thread for
|
76
|
+
Version v5.x of `dogstatsd-ruby` is using a companion 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 companion thread to flush metrics.
|
88
79
|
|
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.
|
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.
|
91
81
|
|
92
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).
|
93
83
|
|
94
84
|
If you are using [Puma](https://github.com/puma/puma) or [Unicorn](https://yhbt.net/unicorn.git), please make sure to create the instance of DogStatsD in the workers, not in the main process before it forks to create its workers. See [this comment for more details](https://github.com/DataDog/dogstatsd-ruby/issues/179#issuecomment-845570345).
|
95
85
|
|
96
|
-
Applications that
|
86
|
+
Applications that run into issues but can't apply these recommendations should use the `single_thread` mode which disables the use of the companion thread.
|
87
|
+
Here is how to instantiate a client in this mode:
|
97
88
|
|
98
89
|
```ruby
|
99
|
-
# Import the library
|
100
|
-
require 'datadog/statsd'
|
101
|
-
|
102
|
-
# Create a DogStatsD client instance.
|
103
90
|
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true)
|
104
|
-
...
|
91
|
+
# ...
|
105
92
|
# release resources used by the client instance and flush last metrics
|
106
93
|
statsd.close()
|
107
94
|
```
|
108
95
|
|
109
96
|
### Origin detection over UDP
|
110
97
|
|
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.
|
98
|
+
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.
|
99
|
+
|
100
|
+
To enable origin detection over UDP, add the following lines to your application manifest:
|
112
101
|
|
113
|
-
To enable origin detection over UDP, add the following lines to your application manifest
|
114
102
|
```yaml
|
115
103
|
env:
|
116
104
|
- name: DD_ENTITY_ID
|
@@ -118,56 +106,57 @@ env:
|
|
118
106
|
fieldRef:
|
119
107
|
fieldPath: metadata.uid
|
120
108
|
```
|
109
|
+
|
121
110
|
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
111
|
|
123
112
|
## Usage
|
124
113
|
|
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).
|
114
|
+
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
115
|
|
127
116
|
### Metrics
|
128
117
|
|
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/
|
118
|
+
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
119
|
|
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/
|
120
|
+
* [Submit a COUNT metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#count).
|
121
|
+
* [Submit a GAUGE metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#gauge).
|
122
|
+
* [Submit a SET metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#set)
|
123
|
+
* [Submit a HISTOGRAM metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#histogram)
|
124
|
+
* [Submit a DISTRIBUTION metric](https://docs.datadoghq.com/metrics/dogstatsd_metrics_submission/?code-lang=ruby#distribution)
|
136
125
|
|
137
|
-
Some options are suppported when submitting metrics, like [applying a Sample Rate to your metrics](https://docs.datadoghq.com/
|
126
|
+
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
127
|
|
139
128
|
### Events
|
140
129
|
|
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/
|
130
|
+
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
131
|
|
143
132
|
### Service Checks
|
144
133
|
|
145
134
|
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
135
|
|
147
|
-
### Maximum
|
136
|
+
### Maximum packet size in high-throughput scenarios
|
148
137
|
|
149
138
|
In order to have the most efficient use of this library in high-throughput scenarios,
|
150
|
-
|
151
|
-
and UDP (1432 bytes)
|
152
|
-
|
153
|
-
|
139
|
+
recommended values for the maximum packet size have already been set for both UDS (8192 bytes)
|
140
|
+
and UDP (1432 bytes).
|
141
|
+
|
142
|
+
However, if are in control of your network and want to use a different value for the maximum packet
|
143
|
+
size, you can do it by setting the `buffer_max_payload_size` parameter:
|
154
144
|
|
155
145
|
```ruby
|
156
|
-
# Create a DogStatsD client instance.
|
157
146
|
statsd = Datadog::Statsd.new('localhost', 8125, buffer_max_payload_size: 4096)
|
147
|
+
# ...
|
148
|
+
statsd.close()
|
158
149
|
```
|
159
150
|
|
160
151
|
## Threading model
|
161
152
|
|
162
|
-
|
153
|
+
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
154
|
|
164
|
-
When you instantiate a `Datadog::Statsd`, a companion thread is spawned. This thread will be called the Sender thread, as it is modeled by the [Sender](../lib/datadog/statsd/sender.rb) class.
|
155
|
+
When you instantiate a `Datadog::Statsd`, a companion 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
156
|
|
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.
|
157
|
+
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
158
|
|
170
|
-
The sender thread has the following logic (
|
159
|
+
The sender thread has the following logic (from `Datadog::Statsd::Sender#send_loop`):
|
171
160
|
|
172
161
|
```
|
173
162
|
while the sender message queue is not closed do
|
@@ -183,15 +172,13 @@ while the sender message queue is not closed do
|
|
183
172
|
end while
|
184
173
|
```
|
185
174
|
|
186
|
-
|
187
|
-
|
188
|
-
We can see that there is 3 different kind of messages:
|
175
|
+
There are three different kinds of messages:
|
189
176
|
|
190
|
-
|
191
|
-
|
192
|
-
|
177
|
+
1. a control message to flush the buffer in the connection
|
178
|
+
2. a control message to synchronize any thread with the sender thread
|
179
|
+
3. a message to append to the buffer
|
193
180
|
|
194
|
-
There is also an implicit message which
|
181
|
+
There is also an implicit message which closes the queue which will cause the sender thread to finish processing and exit.
|
195
182
|
|
196
183
|
### Usual workflow
|
197
184
|
|
@@ -199,20 +186,23 @@ You push metrics to the statsd client which writes them quickly to the sender me
|
|
199
186
|
|
200
187
|
### Flushing
|
201
188
|
|
202
|
-
When calling
|
189
|
+
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
190
|
|
204
191
|
### Rendez-vous
|
205
192
|
|
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
|
193
|
+
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)`.
|
194
|
+
|
195
|
+
Doing so means the caller thread is blocked and waiting until the data has been flushed by the sender thread.
|
196
|
+
|
197
|
+
This is useful when preparing to exit the application or when checking unit tests.
|
207
198
|
|
208
|
-
|
199
|
+
## Versioning
|
209
200
|
|
210
|
-
This is
|
201
|
+
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. As much as possible, we will add a "future deprecation" message in the minor release preceding the one dropping the support.
|
211
202
|
|
212
203
|
## Credits
|
213
204
|
|
214
|
-
dogstatsd-ruby is forked from Rein Henrichs [original Statsd
|
215
|
-
client](https://github.com/reinh/statsd).
|
205
|
+
dogstatsd-ruby is forked from Rein Henrichs' [original Statsd client](https://github.com/reinh/statsd).
|
216
206
|
|
217
207
|
Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
|
218
208
|
further details.
|
@@ -8,14 +8,8 @@ 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
|
19
13
|
end
|
20
14
|
|
21
15
|
def write(payload)
|
@@ -36,6 +30,7 @@ module Datadog
|
|
36
30
|
retries += 1
|
37
31
|
begin
|
38
32
|
close
|
33
|
+
connect
|
39
34
|
retry
|
40
35
|
rescue StandardError => e
|
41
36
|
boom = e
|
@@ -48,11 +43,16 @@ module Datadog
|
|
48
43
|
end
|
49
44
|
|
50
45
|
private
|
46
|
+
|
51
47
|
attr_reader :telemetry
|
52
48
|
attr_reader :logger
|
53
49
|
|
54
|
-
def
|
55
|
-
|
50
|
+
def connect
|
51
|
+
raise 'Should be implemented by subclass'
|
52
|
+
end
|
53
|
+
|
54
|
+
def close
|
55
|
+
raise 'Should be implemented by subclass'
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
@@ -49,13 +49,12 @@ module Datadog
|
|
49
49
|
raise ArgumentError, "buffer_max_payload_size is not high enough to use telemetry (tags=(#{global_tags.inspect}))"
|
50
50
|
end
|
51
51
|
|
52
|
-
|
52
|
+
buffer = MessageBuffer.new(@connection,
|
53
53
|
max_payload_size: buffer_max_payload_size,
|
54
54
|
max_pool_size: buffer_max_pool_size || DEFAULT_BUFFER_POOL_SIZE,
|
55
55
|
overflowing_stategy: buffer_overflowing_stategy,
|
56
56
|
)
|
57
|
-
|
58
|
-
@sender = single_thread ? SingleThreadSender.new(buffer) : Sender.new(buffer)
|
57
|
+
@sender = (single_thread ? SingleThreadSender : Sender).new(buffer, logger: logger)
|
59
58
|
@sender.start
|
60
59
|
end
|
61
60
|
|
@@ -99,7 +98,6 @@ module Datadog
|
|
99
98
|
end
|
100
99
|
|
101
100
|
private
|
102
|
-
attr_reader :buffer
|
103
101
|
attr_reader :sender
|
104
102
|
attr_reader :connection
|
105
103
|
|
@@ -19,7 +19,7 @@ module Datadog
|
|
19
19
|
@overflowing_stategy = overflowing_stategy
|
20
20
|
|
21
21
|
@buffer = String.new
|
22
|
-
|
22
|
+
clear_buffer
|
23
23
|
end
|
24
24
|
|
25
25
|
def add(message)
|
@@ -42,16 +42,20 @@ module Datadog
|
|
42
42
|
true
|
43
43
|
end
|
44
44
|
|
45
|
+
def reset
|
46
|
+
clear_buffer
|
47
|
+
connection.reset_telemetry
|
48
|
+
end
|
49
|
+
|
45
50
|
def flush
|
46
51
|
return if buffer.empty?
|
47
52
|
|
48
53
|
connection.write(buffer)
|
49
|
-
|
50
|
-
buffer.clear
|
51
|
-
@message_count = 0
|
54
|
+
clear_buffer
|
52
55
|
end
|
53
56
|
|
54
57
|
private
|
58
|
+
|
55
59
|
attr :max_payload_size
|
56
60
|
attr :max_pool_size
|
57
61
|
|
@@ -66,6 +70,11 @@ module Datadog
|
|
66
70
|
false
|
67
71
|
end
|
68
72
|
|
73
|
+
def clear_buffer
|
74
|
+
buffer.clear
|
75
|
+
@message_count = 0
|
76
|
+
end
|
77
|
+
|
69
78
|
def preemptive_flush?
|
70
79
|
@message_count == max_pool_size || buffer.bytesize > bytesize_threshold
|
71
80
|
end
|
@@ -2,23 +2,45 @@
|
|
2
2
|
|
3
3
|
module Datadog
|
4
4
|
class Statsd
|
5
|
+
# Sender is using a companion thread to flush and pack messages
|
6
|
+
# in a `MessageBuffer`.
|
7
|
+
# The communication with this thread is done using a `Queue`.
|
8
|
+
# If the thread is dead, it is starting a new one to avoid having a blocked
|
9
|
+
# Sender with no companion thread to communicate with (most of the time, having
|
10
|
+
# a dead companion thread means that a fork just happened and that we are
|
11
|
+
# running in the child process).
|
5
12
|
class Sender
|
6
13
|
CLOSEABLE_QUEUES = Queue.instance_methods.include?(:close)
|
7
14
|
|
8
|
-
def initialize(message_buffer)
|
15
|
+
def initialize(message_buffer, logger: nil)
|
9
16
|
@message_buffer = message_buffer
|
17
|
+
@logger = logger
|
18
|
+
@mx = Mutex.new
|
10
19
|
end
|
11
20
|
|
12
21
|
def flush(sync: false)
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
message_queue
|
22
|
+
# keep a copy around in case another thread is calling #stop while this method is running
|
23
|
+
current_message_queue = message_queue
|
24
|
+
|
25
|
+
# don't try to flush if there is no message_queue instantiated or
|
26
|
+
# no companion thread running
|
27
|
+
if !current_message_queue
|
28
|
+
@logger.debug { "Statsd: can't flush: no message queue ready" } if @logger
|
29
|
+
return
|
30
|
+
end
|
31
|
+
if !sender_thread.alive?
|
32
|
+
@logger.debug { "Statsd: can't flush: no sender_thread alive" } if @logger
|
33
|
+
return
|
34
|
+
end
|
17
35
|
|
36
|
+
current_message_queue.push(:flush)
|
18
37
|
rendez_vous if sync
|
19
38
|
end
|
20
39
|
|
21
40
|
def rendez_vous
|
41
|
+
# could happen if #start hasn't be called
|
42
|
+
return unless message_queue
|
43
|
+
|
22
44
|
# Initialize and get the thread's sync queue
|
23
45
|
queue = (Thread.current[:statsd_sync_queue] ||= Queue.new)
|
24
46
|
# tell sender-thread to notify us in the current
|
@@ -32,19 +54,39 @@ module Datadog
|
|
32
54
|
def add(message)
|
33
55
|
raise ArgumentError, 'Start sender first' unless message_queue
|
34
56
|
|
57
|
+
# if the thread does not exist, we assume we are running in a forked process,
|
58
|
+
# empty the message queue and message buffers (these messages belong to
|
59
|
+
# the parent process) and spawn a new companion thread.
|
60
|
+
if !sender_thread.alive?
|
61
|
+
@mx.synchronize {
|
62
|
+
# a call from another thread has already re-created
|
63
|
+
# the companion thread before this one acquired the lock
|
64
|
+
break if sender_thread.alive?
|
65
|
+
@logger.debug { "Statsd: companion thread is dead, re-creating one" } if @logger
|
66
|
+
|
67
|
+
message_queue.close if CLOSEABLE_QUEUES
|
68
|
+
@message_queue = nil
|
69
|
+
message_buffer.reset
|
70
|
+
start
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
35
74
|
message_queue << message
|
36
75
|
end
|
37
76
|
|
38
77
|
def start
|
39
78
|
raise ArgumentError, 'Sender already started' if message_queue
|
40
79
|
|
41
|
-
# initialize message queue for background thread
|
80
|
+
# initialize a new message queue for the background thread
|
42
81
|
@message_queue = Queue.new
|
43
82
|
# start background thread
|
44
83
|
@sender_thread = Thread.new(&method(:send_loop))
|
45
84
|
end
|
46
85
|
|
47
86
|
if CLOSEABLE_QUEUES
|
87
|
+
# when calling stop, make sure that no other threads is trying
|
88
|
+
# to close the sender nor trying to continue to `#add` more message
|
89
|
+
# into the sender.
|
48
90
|
def stop(join_worker: true)
|
49
91
|
message_queue = @message_queue
|
50
92
|
message_queue.close if message_queue
|
@@ -53,6 +95,9 @@ module Datadog
|
|
53
95
|
sender_thread.join if sender_thread && join_worker
|
54
96
|
end
|
55
97
|
else
|
98
|
+
# when calling stop, make sure that no other threads is trying
|
99
|
+
# to close the sender nor trying to continue to `#add` more message
|
100
|
+
# into the sender.
|
56
101
|
def stop(join_worker: true)
|
57
102
|
message_queue = @message_queue
|
58
103
|
message_queue << :close if message_queue
|
@@ -65,7 +110,6 @@ module Datadog
|
|
65
110
|
private
|
66
111
|
|
67
112
|
attr_reader :message_buffer
|
68
|
-
|
69
113
|
attr_reader :message_queue
|
70
114
|
attr_reader :sender_thread
|
71
115
|
|
@@ -2,17 +2,35 @@
|
|
2
2
|
|
3
3
|
module Datadog
|
4
4
|
class Statsd
|
5
|
+
# The SingleThreadSender is a sender synchronously buffering messages
|
6
|
+
# in a `MessageBuffer`.
|
7
|
+
# It is using current Process.PID to check it is the result of a recent fork
|
8
|
+
# and it is reseting the MessageBuffer if that's the case.
|
5
9
|
class SingleThreadSender
|
6
|
-
def initialize(message_buffer)
|
10
|
+
def initialize(message_buffer, logger: nil)
|
7
11
|
@message_buffer = message_buffer
|
12
|
+
@logger = logger
|
13
|
+
@mx = Mutex.new
|
14
|
+
# store the pid for which this sender has been created
|
15
|
+
update_fork_pid
|
8
16
|
end
|
9
17
|
|
10
18
|
def add(message)
|
11
|
-
@
|
19
|
+
@mx.synchronize {
|
20
|
+
# we have just forked, meaning we have messages in the buffer that we should
|
21
|
+
# not send, they belong to the parent process, let's clear the buffer.
|
22
|
+
if forked?
|
23
|
+
@message_buffer.reset
|
24
|
+
update_fork_pid
|
25
|
+
end
|
26
|
+
@message_buffer.add(message)
|
27
|
+
}
|
12
28
|
end
|
13
29
|
|
14
30
|
def flush(*)
|
15
|
-
@
|
31
|
+
@mx.synchronize {
|
32
|
+
@message_buffer.flush()
|
33
|
+
}
|
16
34
|
end
|
17
35
|
|
18
36
|
# Compatibility with `Sender`
|
@@ -26,6 +44,19 @@ module Datadog
|
|
26
44
|
# Compatibility with `Sender`
|
27
45
|
def rendez_vous()
|
28
46
|
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# below are "fork management" methods to be able to clean the MessageBuffer
|
51
|
+
# if it detects that it is running in a unknown PID.
|
52
|
+
|
53
|
+
def forked?
|
54
|
+
Process.pid != @fork_pid
|
55
|
+
end
|
56
|
+
|
57
|
+
def update_fork_pid
|
58
|
+
@fork_pid = Process.pid
|
59
|
+
end
|
29
60
|
end
|
30
61
|
end
|
31
62
|
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Datadog
|
4
|
+
class FlushQueue < Queue
|
5
|
+
end
|
6
|
+
class CloseQueue < Queue
|
7
|
+
end
|
8
|
+
class Statsd
|
9
|
+
# Sender is using a background thread to flush and pack messages
|
10
|
+
# in a `MessageBuffer`.
|
11
|
+
# The communication with this thread is done using a `Queue`.
|
12
|
+
# If the thread is dead, it is starting a new one to avoid having a blocked
|
13
|
+
# Sender with no background thread to communicate with (most of the time,
|
14
|
+
# having a dead background thread means that a fork just happened and that we
|
15
|
+
# are running in the child process).
|
16
|
+
class Sender
|
17
|
+
CLOSEABLE_QUEUES = Queue.instance_methods.include?(:close)
|
18
|
+
|
19
|
+
def initialize(message_buffer, logger: nil)
|
20
|
+
@message_buffer = message_buffer
|
21
|
+
@logger = logger
|
22
|
+
|
23
|
+
# communication and synchronization with the background thread
|
24
|
+
# @mux is also used to not having multiple threads fighting for
|
25
|
+
# closing the Sender or creating a new background thread
|
26
|
+
@channel = Queue.new
|
27
|
+
@mux = Mutex.new
|
28
|
+
|
29
|
+
@is_closed = false
|
30
|
+
|
31
|
+
# start background thread immediately
|
32
|
+
@sender_thread = Thread.new(&method(:send_loop))
|
33
|
+
end
|
34
|
+
|
35
|
+
def flush(sync: false)
|
36
|
+
@mux.synchronize {
|
37
|
+
# we don't want to send a flush action to the bg thread if:
|
38
|
+
# - there is no bg thread running
|
39
|
+
# - the sender has been closed
|
40
|
+
return if !sender_thread.alive? || @is_closed
|
41
|
+
|
42
|
+
if sync
|
43
|
+
# blocking flush
|
44
|
+
blocking_queue = FlushQueue.new
|
45
|
+
channel << blocking_queue
|
46
|
+
blocking_queue.pop # wait for the bg thread to finish its work
|
47
|
+
blocking_queue.close if CLOSEABLE_QUEUES
|
48
|
+
else
|
49
|
+
# asynchronous flush
|
50
|
+
channel << :flush
|
51
|
+
end
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
def add(message)
|
56
|
+
return if @is_closed # don't send a message to the bg thread if the sender has been closed
|
57
|
+
|
58
|
+
# the bg thread is not running anymore, this is happening if the main process has forked and
|
59
|
+
# we are running in the child, we will spawn a bg thread and reset buffers (containing parents' messages)
|
60
|
+
if !sender_thread.alive?
|
61
|
+
@mux.synchronize {
|
62
|
+
return if @is_closed
|
63
|
+
# test if a call from another thread has already re-created
|
64
|
+
# the background thread before this one acquired the lock
|
65
|
+
break if sender_thread.alive?
|
66
|
+
|
67
|
+
# re-create the channel of communication since we will spawn a new bg thread
|
68
|
+
channel.close if CLOSEABLE_QUEUES
|
69
|
+
@channel = Queue.new
|
70
|
+
message_buffer.reset # don't use messages appended by another fork
|
71
|
+
@sender_thread = Thread.new(&method(:send_loop))
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
channel << message
|
76
|
+
end
|
77
|
+
|
78
|
+
# Compatibility with `Sender`
|
79
|
+
def start()
|
80
|
+
end
|
81
|
+
|
82
|
+
def stop()
|
83
|
+
return if @is_closed
|
84
|
+
# use this lock to both: not having another thread stopping this instance nor
|
85
|
+
# having a #add call creating a new thread
|
86
|
+
@mux.synchronize {
|
87
|
+
@is_closed = true
|
88
|
+
if sender_thread.alive? # no reasons to stop the bg thread is none is running already
|
89
|
+
blocking_queue = CloseQueue.new
|
90
|
+
channel << blocking_queue
|
91
|
+
blocking_queue.pop # wait for the bg thread to finish its work
|
92
|
+
blocking_queue.close if CLOSEABLE_QUEUES
|
93
|
+
sender_thread.join(3) # wait for completion, timeout after 3 seconds
|
94
|
+
# TODO(remy): should I close `channel` here?
|
95
|
+
end
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
attr_reader :message_buffer
|
102
|
+
attr_reader :channel
|
103
|
+
attr_reader :mux
|
104
|
+
attr_reader :sender_thread
|
105
|
+
|
106
|
+
def send_loop
|
107
|
+
until (message = channel.pop).nil? && (CLOSEABLE_QUEUES && channel.closed?)
|
108
|
+
# skip if message is nil, e.g. when the channel is empty and closed
|
109
|
+
next unless message
|
110
|
+
|
111
|
+
case message
|
112
|
+
# if a FlushQueue is received, the background thread has to flush the message
|
113
|
+
# buffer and to send an :unblock to let the caller know that it has finished
|
114
|
+
when FlushQueue
|
115
|
+
message_buffer.flush
|
116
|
+
message << :unblock
|
117
|
+
# if a :flush is received, the background thread has to flush asynchronously
|
118
|
+
when :flush
|
119
|
+
message_buffer.flush
|
120
|
+
# if a CloseQueue is received, the background thread has to do a last flush
|
121
|
+
# and to send an :unblock to let the caller know that it has finished
|
122
|
+
when CloseQueue
|
123
|
+
message << :unblock
|
124
|
+
return
|
125
|
+
else
|
126
|
+
message_buffer.add(message)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -19,18 +19,26 @@ module Datadog
|
|
19
19
|
|
20
20
|
@host = host || ENV.fetch('DD_AGENT_HOST', DEFAULT_HOST)
|
21
21
|
@port = port || ENV.fetch('DD_DOGSTATSD_PORT', DEFAULT_PORT).to_i
|
22
|
+
@socket = nil
|
23
|
+
connect
|
24
|
+
end
|
25
|
+
|
26
|
+
def close
|
27
|
+
@socket.close if @socket
|
28
|
+
@socket = nil
|
22
29
|
end
|
23
30
|
|
24
31
|
private
|
25
32
|
|
26
33
|
def connect
|
27
|
-
|
28
|
-
|
29
|
-
|
34
|
+
close if @socket
|
35
|
+
|
36
|
+
@socket = UDPSocket.new
|
37
|
+
@socket.connect(host, port)
|
30
38
|
end
|
31
39
|
|
32
40
|
def send_message(message)
|
33
|
-
socket.send(message, 0)
|
41
|
+
@socket.send(message, 0)
|
34
42
|
end
|
35
43
|
end
|
36
44
|
end
|
@@ -14,20 +14,27 @@ module Datadog
|
|
14
14
|
super(**kwargs)
|
15
15
|
|
16
16
|
@socket_path = socket_path
|
17
|
+
@socket = nil
|
18
|
+
connect
|
19
|
+
end
|
20
|
+
|
21
|
+
def close
|
22
|
+
@socket.close if @socket
|
23
|
+
@socket = nil
|
17
24
|
end
|
18
25
|
|
19
26
|
private
|
20
27
|
|
21
28
|
def connect
|
22
|
-
|
23
|
-
|
24
|
-
socket
|
29
|
+
close if @socket
|
30
|
+
|
31
|
+
@socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
|
32
|
+
@socket.connect(Socket.pack_sockaddr_un(@socket_path))
|
25
33
|
end
|
26
34
|
|
27
35
|
def send_message(message)
|
28
|
-
socket.sendmsg_nonblock(message)
|
36
|
+
@socket.sendmsg_nonblock(message)
|
29
37
|
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ENOENT => e
|
30
|
-
@socket = nil
|
31
38
|
# TODO: FIXME: This error should be considered as a retryable error in the
|
32
39
|
# Connection class. An even better solution would be to make BadSocketError inherit
|
33
40
|
# from a specific retryable error class in the Connection class.
|
data/lib/datadog/statsd.rb
CHANGED
@@ -11,6 +11,9 @@ require_relative 'statsd/sender'
|
|
11
11
|
require_relative 'statsd/single_thread_sender'
|
12
12
|
require_relative 'statsd/forwarder'
|
13
13
|
|
14
|
+
$deprecation_message_mutex = Mutex.new
|
15
|
+
$deprecation_message_done = false
|
16
|
+
|
14
17
|
# = Datadog::Statsd: A DogStatsd client (https://www.datadoghq.com)
|
15
18
|
#
|
16
19
|
# @example Set up a global Statsd client for a server on localhost:8125
|
@@ -101,6 +104,19 @@ module Datadog
|
|
101
104
|
@serializer = Serialization::Serializer.new(prefix: @prefix, global_tags: tags)
|
102
105
|
@sample_rate = sample_rate
|
103
106
|
|
107
|
+
# deprecation message for ruby < 2.1.0 users as we will drop support for ruby 2.0
|
108
|
+
# in dogstatsd-ruby 5.4.0
|
109
|
+
# TODO(remy): remove this message and the two global vars used in dogstatd-ruby 5.4.0
|
110
|
+
if RUBY_VERSION < '2.1.0' && $deprecation_message_mutex.try_lock && !$deprecation_message_done
|
111
|
+
if logger != nil
|
112
|
+
logger.warn { "deprecation: dogstatsd-ruby will drop support of Ruby < 2.1.0 in a next minor release" }
|
113
|
+
else
|
114
|
+
puts("warning: deprecation: dogstatsd-ruby will drop support of Ruby < 2.1.0 in a next minor release")
|
115
|
+
end
|
116
|
+
$deprecation_message_done = true
|
117
|
+
$deprecation_message_mutex.unlock
|
118
|
+
end
|
119
|
+
|
104
120
|
@forwarder = Forwarder.new(
|
105
121
|
host: host,
|
106
122
|
port: port,
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dogstatsd-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rein Henrichs
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-
|
12
|
+
date: 2021-10-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A Ruby DogStatsd client
|
15
15
|
email: code@datadoghq.com
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- lib/datadog/statsd/serialization/tag_serializer.rb
|
35
35
|
- lib/datadog/statsd/single_thread_sender.rb
|
36
36
|
- lib/datadog/statsd/telemetry.rb
|
37
|
+
- lib/datadog/statsd/threaded_sender.rb
|
37
38
|
- lib/datadog/statsd/udp_connection.rb
|
38
39
|
- lib/datadog/statsd/uds_connection.rb
|
39
40
|
- lib/datadog/statsd/version.rb
|
@@ -42,9 +43,9 @@ licenses:
|
|
42
43
|
- MIT
|
43
44
|
metadata:
|
44
45
|
bug_tracker_uri: https://github.com/DataDog/dogstatsd-ruby/issues
|
45
|
-
changelog_uri: https://github.com/DataDog/dogstatsd-ruby/blob/v5.
|
46
|
-
documentation_uri: https://www.rubydoc.info/gems/dogstatsd-ruby/5.
|
47
|
-
source_code_uri: https://github.com/DataDog/dogstatsd-ruby/tree/v5.
|
46
|
+
changelog_uri: https://github.com/DataDog/dogstatsd-ruby/blob/v5.3.0/CHANGELOG.md
|
47
|
+
documentation_uri: https://www.rubydoc.info/gems/dogstatsd-ruby/5.3.0
|
48
|
+
source_code_uri: https://github.com/DataDog/dogstatsd-ruby/tree/v5.3.0
|
48
49
|
post_install_message:
|
49
50
|
rdoc_options: []
|
50
51
|
require_paths:
|