dogstatsd-ruby 3.3.0 → 5.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +174 -53
- data/lib/datadog/statsd/connection.rb +60 -0
- data/lib/datadog/statsd/forwarder.rb +120 -0
- data/lib/datadog/statsd/message_buffer.rb +97 -0
- data/lib/datadog/statsd/sender.rb +161 -0
- data/lib/datadog/statsd/serialization/event_serializer.rb +71 -0
- data/lib/datadog/statsd/serialization/serializer.rb +41 -0
- data/lib/datadog/statsd/serialization/service_check_serializer.rb +60 -0
- data/lib/datadog/statsd/serialization/stat_serializer.rb +55 -0
- data/lib/datadog/statsd/serialization/tag_serializer.rb +96 -0
- data/lib/datadog/statsd/serialization.rb +15 -0
- data/lib/datadog/statsd/single_thread_sender.rb +62 -0
- data/lib/datadog/statsd/telemetry.rb +96 -0
- data/lib/datadog/statsd/udp_connection.rb +49 -0
- data/lib/datadog/statsd/uds_connection.rb +49 -0
- data/lib/datadog/statsd/version.rb +9 -0
- data/lib/datadog/statsd.rb +198 -286
- metadata +32 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfde2027b6fb73eee85ed0c612db51df707c2b01bec2266a3f2049acc34990f1
|
4
|
+
data.tar.gz: 5b1bb3263af1cbdde2bb84cf4d92f2e84ad7e0a0b3eaa5ad67f048750c062c46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b563e322f2eaff18eeb07f786d6277290c032e94859d89d856a7bc2512a278424ab5ec9f94f8bf477a222e9ef00075ad2e0293b29dbb0021a5b6551f836b3c9
|
7
|
+
data.tar.gz: 787b04cc62a139289784a9b1992a152ca74fafa3d97afa68944dea95121ff6d3af80164b551b5430d50b263f0d0c8f702d22c1450faaaebdbc4bc69f09c331fe
|
data/README.md
CHANGED
@@ -1,93 +1,214 @@
|
|
1
|
+
# dogstatsd-ruby
|
1
2
|
|
2
|
-
dogstatsd-ruby
|
3
|
-
==============
|
3
|
+
A client for DogStatsD, an extension of the StatsD metric server for Datadog. Full API documentation is available in [DogStatsD-ruby rubydoc](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/Datadog/Statsd).
|
4
4
|
|
5
|
-
|
5
|
+
[![Build Status](https://secure.travis-ci.org/DataDog/dogstatsd-ruby.svg)](http://travis-ci.org/DataDog/dogstatsd-ruby)
|
6
6
|
|
7
|
-
[
|
7
|
+
See [CHANGELOG.md](CHANGELOG.md) for changes. To suggest a feature, report a bug, or general discussion, [open an issue](http://github.com/DataDog/dogstatsd-ruby/issues/).
|
8
8
|
|
9
|
-
|
10
|
-
-----------------
|
9
|
+
## Installation
|
11
10
|
|
12
11
|
First install the library:
|
13
12
|
|
14
|
-
|
13
|
+
```
|
14
|
+
gem install dogstatsd-ruby
|
15
|
+
```
|
15
16
|
|
16
|
-
|
17
|
+
## Configuration
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
To instantiate a DogStatsd client:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# Import the library
|
20
23
|
require 'datadog/statsd'
|
21
24
|
|
22
|
-
# Create a
|
25
|
+
# Create a DogStatsD client instance
|
23
26
|
statsd = Datadog::Statsd.new('localhost', 8125)
|
27
|
+
# ...
|
28
|
+
# release resources used by the client instance
|
29
|
+
statsd.close()
|
30
|
+
```
|
31
|
+
Or if you want to connect over Unix Domain Socket:
|
32
|
+
```ruby
|
33
|
+
# Connection over Unix Domain Socket
|
34
|
+
statsd = Datadog::Statsd.new(socket_path: '/path/to/socket/file')
|
35
|
+
# ...
|
36
|
+
# release resources used by the client instance
|
37
|
+
statsd.close()
|
38
|
+
```
|
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/?code-lang=ruby#client-instantiation-parameters).
|
41
|
+
|
42
|
+
### Migrating from v4.x to v5.x
|
24
43
|
|
25
|
-
|
26
|
-
|
27
|
-
# end
|
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):
|
28
46
|
|
29
|
-
|
30
|
-
statsd.increment('page.views')
|
47
|
+
In practice, it means two things:
|
31
48
|
|
32
|
-
#
|
33
|
-
statsd.gauge('users.online', 123, :sample_rate=>0.5)
|
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.
|
34
50
|
|
35
|
-
|
36
|
-
statsd.histogram('file.upload.size', 1234)
|
51
|
+
2. You have to make sure you are either:
|
37
52
|
|
38
|
-
|
39
|
-
|
40
|
-
render_page('home.html')
|
41
|
-
end
|
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`
|
42
55
|
|
43
|
-
|
44
|
-
# All metrics will be buffered and sent in one packet when the block completes
|
45
|
-
statsd.batch do |s|
|
46
|
-
s.increment('page.views')
|
47
|
-
s.gauge('users.online', 123)
|
48
|
-
end
|
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):
|
49
57
|
|
50
|
-
|
51
|
-
|
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()
|
52
63
|
```
|
53
64
|
|
54
|
-
|
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.
|
55
81
|
|
56
|
-
|
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).
|
57
83
|
|
58
|
-
|
59
|
-
# Post a simple message
|
60
|
-
statsd.event("There might be a storm tomorrow", "A friend warned me earlier.")
|
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).
|
61
85
|
|
62
|
-
|
63
|
-
|
86
|
+
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.
|
87
|
+
Here is how to instantiate a client in this mode:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true)
|
91
|
+
# ...
|
92
|
+
# release resources used by the client instance and flush last metrics
|
93
|
+
statsd.close()
|
94
|
+
```
|
95
|
+
|
96
|
+
### Origin detection over UDP
|
97
|
+
|
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:
|
101
|
+
|
102
|
+
```yaml
|
103
|
+
env:
|
104
|
+
- name: DD_ENTITY_ID
|
105
|
+
valueFrom:
|
106
|
+
fieldRef:
|
107
|
+
fieldPath: metadata.uid
|
64
108
|
```
|
65
109
|
|
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.
|
111
|
+
|
112
|
+
## Usage
|
113
|
+
|
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).
|
115
|
+
|
116
|
+
### Metrics
|
117
|
+
|
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:
|
119
|
+
|
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)
|
125
|
+
|
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).
|
127
|
+
|
128
|
+
### Events
|
129
|
+
|
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.
|
131
|
+
|
132
|
+
### Service Checks
|
133
|
+
|
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.
|
135
|
+
|
136
|
+
### Maximum packet size in high-throughput scenarios
|
137
|
+
|
138
|
+
In order to have the most efficient use of this library in high-throughput scenarios,
|
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:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
statsd = Datadog::Statsd.new('localhost', 8125, buffer_max_payload_size: 4096)
|
147
|
+
# ...
|
148
|
+
statsd.close()
|
149
|
+
```
|
150
|
+
|
151
|
+
## Threading model
|
152
|
+
|
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).
|
154
|
+
|
155
|
+
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.
|
156
|
+
|
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.
|
158
|
+
|
159
|
+
The sender thread has the following logic (from `Datadog::Statsd::Sender#send_loop`):
|
160
|
+
|
161
|
+
```
|
162
|
+
while the sender message queue is not closed do
|
163
|
+
read message from sender message queue
|
164
|
+
|
165
|
+
if message is a Control message to flush
|
166
|
+
flush buffer in connection
|
167
|
+
else if message is a Control message to synchronize
|
168
|
+
synchronize with calling thread
|
169
|
+
else
|
170
|
+
add message to the buffer
|
171
|
+
end
|
172
|
+
end while
|
173
|
+
```
|
174
|
+
|
175
|
+
There are three different kinds of messages:
|
176
|
+
|
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
|
180
|
+
|
181
|
+
There is also an implicit message which closes the queue which will cause the sender thread to finish processing and exit.
|
182
|
+
|
183
|
+
### Usual workflow
|
184
|
+
|
185
|
+
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.
|
186
|
+
|
187
|
+
### Flushing
|
188
|
+
|
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.
|
66
190
|
|
191
|
+
### Rendez-vous
|
67
192
|
|
68
|
-
|
69
|
-
-------------
|
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)`.
|
70
194
|
|
71
|
-
|
72
|
-
[here](http://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/frames).
|
195
|
+
Doing so means the caller thread is blocked and waiting until the data has been flushed by the sender thread.
|
73
196
|
|
197
|
+
This is useful when preparing to exit the application or when checking unit tests.
|
74
198
|
|
75
|
-
|
76
|
-
--------
|
199
|
+
### Thread-safety
|
77
200
|
|
78
|
-
|
79
|
-
[here](http://github.com/DataDog/dogstatsd-ruby/issues/).
|
201
|
+
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)).
|
80
202
|
|
203
|
+
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.
|
81
204
|
|
82
|
-
|
83
|
-
----------------------------
|
205
|
+
## Versioning
|
84
206
|
|
207
|
+
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.
|
85
208
|
|
86
|
-
Credits
|
87
|
-
-------
|
209
|
+
## Credits
|
88
210
|
|
89
|
-
dogstatsd-ruby is forked from
|
90
|
-
client](https://github.com/reinh/statsd).
|
211
|
+
dogstatsd-ruby is forked from Rein Henrichs' [original Statsd client](https://github.com/reinh/statsd).
|
91
212
|
|
92
213
|
Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
|
93
214
|
further details.
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Datadog
|
4
|
+
class Statsd
|
5
|
+
class Connection
|
6
|
+
def initialize(telemetry: nil, logger: nil)
|
7
|
+
@telemetry = telemetry
|
8
|
+
@logger = logger
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset_telemetry
|
12
|
+
telemetry.reset
|
13
|
+
end
|
14
|
+
|
15
|
+
# not thread safe: `Sender` instances that use this are required to properly synchronize or sequence calls to this method
|
16
|
+
def write(payload)
|
17
|
+
logger.debug { "Statsd: #{payload}" } if logger
|
18
|
+
|
19
|
+
send_message(payload)
|
20
|
+
|
21
|
+
telemetry.sent(packets: 1, bytes: payload.length) if telemetry
|
22
|
+
|
23
|
+
true
|
24
|
+
rescue StandardError => boom
|
25
|
+
# Try once to reconnect if the socket has been closed
|
26
|
+
retries ||= 1
|
27
|
+
if retries <= 1 &&
|
28
|
+
(boom.is_a?(Errno::ENOTCONN) or
|
29
|
+
boom.is_a?(Errno::ECONNREFUSED) or
|
30
|
+
boom.is_a?(IOError) && boom.message =~ /closed stream/i)
|
31
|
+
retries += 1
|
32
|
+
begin
|
33
|
+
close
|
34
|
+
connect
|
35
|
+
retry
|
36
|
+
rescue StandardError => e
|
37
|
+
boom = e
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
telemetry.dropped(packets: 1, bytes: payload.length) if telemetry
|
42
|
+
logger.error { "Statsd: #{boom.class} #{boom}" } if logger
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
attr_reader :telemetry
|
49
|
+
attr_reader :logger
|
50
|
+
|
51
|
+
def connect
|
52
|
+
raise 'Should be implemented by subclass'
|
53
|
+
end
|
54
|
+
|
55
|
+
def close
|
56
|
+
raise 'Should be implemented by subclass'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,120 @@
|
|
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
|
+
host: nil,
|
11
|
+
port: nil,
|
12
|
+
socket_path: nil,
|
13
|
+
|
14
|
+
buffer_max_payload_size: nil,
|
15
|
+
buffer_max_pool_size: nil,
|
16
|
+
buffer_overflowing_stategy: :drop,
|
17
|
+
|
18
|
+
telemetry_flush_interval: nil,
|
19
|
+
global_tags: [],
|
20
|
+
|
21
|
+
single_thread: false,
|
22
|
+
|
23
|
+
logger: nil
|
24
|
+
)
|
25
|
+
@transport_type = socket_path.nil? ? :udp : :uds
|
26
|
+
|
27
|
+
if telemetry_flush_interval
|
28
|
+
@telemetry = Telemetry.new(telemetry_flush_interval,
|
29
|
+
global_tags: global_tags,
|
30
|
+
transport_type: transport_type
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
@connection = case transport_type
|
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
|
40
|
+
|
41
|
+
# Initialize buffer
|
42
|
+
buffer_max_payload_size ||= (transport_type == :udp ? UDP_DEFAULT_BUFFER_SIZE : UDS_DEFAULT_BUFFER_SIZE)
|
43
|
+
|
44
|
+
if buffer_max_payload_size <= 0
|
45
|
+
raise ArgumentError, 'buffer_max_payload_size cannot be <= 0'
|
46
|
+
end
|
47
|
+
|
48
|
+
unless telemetry.nil? || telemetry.would_fit_in?(buffer_max_payload_size)
|
49
|
+
raise ArgumentError, "buffer_max_payload_size is not high enough to use telemetry (tags=(#{global_tags.inspect}))"
|
50
|
+
end
|
51
|
+
|
52
|
+
buffer = MessageBuffer.new(@connection,
|
53
|
+
max_payload_size: buffer_max_payload_size,
|
54
|
+
max_pool_size: buffer_max_pool_size || DEFAULT_BUFFER_POOL_SIZE,
|
55
|
+
overflowing_stategy: buffer_overflowing_stategy,
|
56
|
+
)
|
57
|
+
@sender = (single_thread ? SingleThreadSender : Sender).new(buffer, logger: logger)
|
58
|
+
@sender.start
|
59
|
+
end
|
60
|
+
|
61
|
+
def send_message(message)
|
62
|
+
sender.add(message)
|
63
|
+
|
64
|
+
tick_telemetry
|
65
|
+
end
|
66
|
+
|
67
|
+
def sync_with_outbound_io
|
68
|
+
sender.rendez_vous
|
69
|
+
end
|
70
|
+
|
71
|
+
def flush(flush_telemetry: false, sync: false)
|
72
|
+
do_flush_telemetry if telemetry && flush_telemetry
|
73
|
+
|
74
|
+
sender.flush(sync: sync)
|
75
|
+
end
|
76
|
+
|
77
|
+
def host
|
78
|
+
return nil unless transport_type == :udp
|
79
|
+
|
80
|
+
connection.host
|
81
|
+
end
|
82
|
+
|
83
|
+
def port
|
84
|
+
return nil unless transport_type == :udp
|
85
|
+
|
86
|
+
connection.port
|
87
|
+
end
|
88
|
+
|
89
|
+
def socket_path
|
90
|
+
return nil unless transport_type == :uds
|
91
|
+
|
92
|
+
connection.socket_path
|
93
|
+
end
|
94
|
+
|
95
|
+
def close
|
96
|
+
sender.stop
|
97
|
+
connection.close
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
attr_reader :sender
|
102
|
+
attr_reader :connection
|
103
|
+
|
104
|
+
def do_flush_telemetry
|
105
|
+
telemetry_snapshot = telemetry.flush
|
106
|
+
telemetry.reset
|
107
|
+
|
108
|
+
telemetry_snapshot.each do |message|
|
109
|
+
sender.add(message)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def tick_telemetry
|
114
|
+
return nil unless telemetry
|
115
|
+
|
116
|
+
do_flush_telemetry if telemetry.should_flush?
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Datadog
|
4
|
+
class Statsd
|
5
|
+
class MessageBuffer
|
6
|
+
PAYLOAD_SIZE_TOLERANCE = 0.05
|
7
|
+
|
8
|
+
def initialize(connection,
|
9
|
+
max_payload_size: nil,
|
10
|
+
max_pool_size: DEFAULT_BUFFER_POOL_SIZE,
|
11
|
+
overflowing_stategy: :drop
|
12
|
+
)
|
13
|
+
raise ArgumentError, 'max_payload_size keyword argument must be provided' unless max_payload_size
|
14
|
+
raise ArgumentError, 'max_pool_size keyword argument must be provided' unless max_pool_size
|
15
|
+
|
16
|
+
@connection = connection
|
17
|
+
@max_payload_size = max_payload_size
|
18
|
+
@max_pool_size = max_pool_size
|
19
|
+
@overflowing_stategy = overflowing_stategy
|
20
|
+
|
21
|
+
@buffer = String.new
|
22
|
+
clear_buffer
|
23
|
+
end
|
24
|
+
|
25
|
+
def add(message)
|
26
|
+
message_size = message.bytesize
|
27
|
+
|
28
|
+
return nil unless message_size > 0 # to avoid adding empty messages to the buffer
|
29
|
+
return nil unless ensure_sendable!(message_size)
|
30
|
+
|
31
|
+
flush if should_flush?(message_size)
|
32
|
+
|
33
|
+
buffer << "\n" unless buffer.empty?
|
34
|
+
buffer << message
|
35
|
+
|
36
|
+
@message_count += 1
|
37
|
+
|
38
|
+
# flush when we're pretty sure that we won't be able
|
39
|
+
# to add another message to the buffer
|
40
|
+
flush if preemptive_flush?
|
41
|
+
|
42
|
+
true
|
43
|
+
end
|
44
|
+
|
45
|
+
def reset
|
46
|
+
clear_buffer
|
47
|
+
connection.reset_telemetry
|
48
|
+
end
|
49
|
+
|
50
|
+
def flush
|
51
|
+
return if buffer.empty?
|
52
|
+
|
53
|
+
connection.write(buffer)
|
54
|
+
clear_buffer
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
attr :max_payload_size
|
60
|
+
attr :max_pool_size
|
61
|
+
|
62
|
+
attr :overflowing_stategy
|
63
|
+
|
64
|
+
attr :connection
|
65
|
+
attr :buffer
|
66
|
+
|
67
|
+
def should_flush?(message_size)
|
68
|
+
return true if buffer.bytesize + 1 + message_size >= max_payload_size
|
69
|
+
|
70
|
+
false
|
71
|
+
end
|
72
|
+
|
73
|
+
def clear_buffer
|
74
|
+
buffer.clear
|
75
|
+
@message_count = 0
|
76
|
+
end
|
77
|
+
|
78
|
+
def preemptive_flush?
|
79
|
+
@message_count == max_pool_size || buffer.bytesize > bytesize_threshold
|
80
|
+
end
|
81
|
+
|
82
|
+
def ensure_sendable!(message_size)
|
83
|
+
return true if message_size <= max_payload_size
|
84
|
+
|
85
|
+
if overflowing_stategy == :raise
|
86
|
+
raise Error, 'Message too big for payload limit'
|
87
|
+
end
|
88
|
+
|
89
|
+
false
|
90
|
+
end
|
91
|
+
|
92
|
+
def bytesize_threshold
|
93
|
+
@bytesize_threshold ||= (max_payload_size - PAYLOAD_SIZE_TOLERANCE * max_payload_size).to_i
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|