dogstatsd-ruby 4.0.0 → 5.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d2b1b9e7ec2a48c5305f6acda103d0b7a09787801dd899a38fb665f5389e4f3
4
- data.tar.gz: 3d974fffb4ace3bc248e69dcdaabb4c556e6783a25d65af98cb65bdccafc52ee
3
+ metadata.gz: aa6ffb63549958aaa8ec14103abbd51082bd945e31d20f0148571ee2c90350b3
4
+ data.tar.gz: ef1361556ae6de5beb22523187c94ffe7285278918f47d671464b52708ae6973
5
5
  SHA512:
6
- metadata.gz: 16c82cb62bfd324d5e1dc1c6293fa121b92bd0c88d00474210c5da67f2cb07ae350cc0b717b15739c7a6f09e9021323ba348cec70ec2d0ac66a362f15f81007b
7
- data.tar.gz: 6e82cef56d746ecf7b10890d397e313c143611b18b4ae65db67eb2f252cb27acacb45de03f6447b7b4ebb1666209806d6b048fb2857c5c8790f1f6729f4ae806
6
+ metadata.gz: ba586784b291adaf8a2eaa7a5b866f7931f2c887cb372a69a715f51c0c36a34fba1480cb2ecfd8bbf9573052c2839724fb6389cfa81e157a280e0a75ba0b7072
7
+ data.tar.gz: 7c2210e57a96e50825362dcedeaa686af6497f85941a9b7dab2a8ca67bbb7cec999d4f13015c5aab90b1a21cb824f5d8c3902cf5dd1421b3a54763a501f441b8
data/README.md CHANGED
@@ -1,98 +1,223 @@
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
- A client for DogStatsD, an extension of the StatsD metric server for Datadog.
5
+ [![Build Status](https://secure.travis-ci.org/DataDog/dogstatsd-ruby.svg)](http://travis-ci.org/DataDog/dogstatsd-ruby)
6
6
 
7
- [![Build Status](https://secure.travis-ci.org/DataDog/dogstatsd-ruby.png)](http://travis-ci.org/DataDog/dogstatsd-ruby)
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
- Quick Start Guide
10
- -----------------
9
+ ## Installation
11
10
 
12
11
  First install the library:
13
12
 
14
- gem install dogstatsd-ruby
13
+ ```
14
+ gem install dogstatsd-ruby
15
+ ```
15
16
 
16
- Then start instrumenting your code:
17
+ ## Configuration
17
18
 
18
- ``` ruby
19
- # Load the dogstats module.
19
+ To instantiate a DogStatsd client:
20
+
21
+ ```ruby
22
+ # Import the library
20
23
  require 'datadog/statsd'
21
24
 
22
- # Create a stats instance.
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
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
+ 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).
85
+
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
+ ```
24
95
 
25
- # you could also create a statsd class if you need a drop in replacement
26
- # class Statsd < Datadog::Statsd
27
- # end
96
+ ### Origin detection over UDP
28
97
 
29
- # Increment a counter.
30
- statsd.increment('page.views')
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.
31
99
 
32
- # Record a gauge 50% of the time.
33
- statsd.gauge('users.online', 123, :sample_rate=>0.5)
100
+ To enable origin detection over UDP, add the following lines to your application manifest:
34
101
 
35
- # Sample a histogram
36
- statsd.histogram('file.upload.size', 1234)
102
+ ```yaml
103
+ env:
104
+ - name: DD_ENTITY_ID
105
+ valueFrom:
106
+ fieldRef:
107
+ fieldPath: metadata.uid
108
+ ```
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)
37
125
 
38
- # Time a block of code
39
- statsd.time('page.render') do
40
- render_page('home.html')
41
- end
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).
42
127
 
43
- # Send several metrics at the same time
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
128
+ ### Events
49
129
 
50
- # Tag a metric.
51
- statsd.histogram('query.time', 10, :tags => ["version:1"])
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.
52
131
 
53
- # Auto-close socket after end of block
54
- Datadog::Statsd.open('localhost', 8125) do |s|
55
- s.increment('page.views')
56
- end
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()
57
149
  ```
58
150
 
59
- You can also post events to your stream. You can tag them, set priority and even aggregate them with other events.
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.
60
156
 
61
- Aggregation in the stream is made on hostname/event_type/source_type/aggregation_key.
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.
62
158
 
63
- ``` ruby
64
- # Post a simple message
65
- statsd.event("There might be a storm tomorrow", "A friend warned me earlier.")
159
+ The sender thread has the following logic (from `Datadog::Statsd::Sender#send_loop`):
66
160
 
67
- # Cry for help
68
- statsd.event("SO MUCH SNOW", "Started yesterday and it won't stop !!", :alert_type => "error", :tags => ["urgent", "endoftheworld"])
69
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
+
184
+ ```ruby
185
+ statsd = Datadog::Statsd.new('localhost', 8125)
186
+ ```
187
+
188
+ The message queue's maximum size (in messages) is given by the `sender_queue_size` argument, and has appropriate defaults for UDP (2048) and UDS (512).
189
+
190
+ 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`.
191
+
192
+ ### Usual workflow
193
+
194
+ 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.
195
+
196
+ ### Flushing
70
197
 
198
+ 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.
71
199
 
200
+ ### Rendez-vous
72
201
 
73
- Documentation
74
- -------------
202
+ 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)`.
75
203
 
76
- Full API documentation is available
77
- [here](http://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/frames).
204
+ Doing so means the caller thread is blocked and waiting until the data has been flushed by the sender thread.
78
205
 
206
+ This is useful when preparing to exit the application or when checking unit tests.
79
207
 
80
- Feedback
81
- --------
208
+ ### Thread-safety
82
209
 
83
- To suggest a feature, report a bug, or general discussion, head over
84
- [here](http://github.com/DataDog/dogstatsd-ruby/issues/).
210
+ 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)).
85
211
 
212
+ 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.
86
213
 
87
- [Change Log](CHANGELOG.md)
88
- ----------------------------
214
+ ## Versioning
89
215
 
216
+ 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.
90
217
 
91
- Credits
92
- -------
218
+ ## Credits
93
219
 
94
- dogstatsd-ruby is forked from Rien Henrichs [original Statsd
95
- client](https://github.com/reinh/statsd).
220
+ dogstatsd-ruby is forked from Rein Henrichs' [original Statsd client](https://github.com/reinh/statsd).
96
221
 
97
222
  Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
98
223
  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_writer(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,76 @@
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
+ DEFAULT_HOST = '127.0.0.1'
27
+ DEFAULT_PORT = 8125
28
+
29
+ def initialize_with_constructor_args(host: nil, port: nil, socket_path: nil)
30
+ try_initialize_with(host: host, port: port, socket_path: socket_path,
31
+ not_both_error_message:
32
+ "Both UDP: (host/port #{host}:#{port}) and UDS (socket_path #{socket_path}) " +
33
+ "constructor arguments were given. Use only one or the other.",
34
+ )
35
+ end
36
+
37
+ def initialize_with_env_vars()
38
+ try_initialize_with(
39
+ host: ENV['DD_AGENT_HOST'],
40
+ port: ENV['DD_DOGSTATSD_PORT'] && ENV['DD_DOGSTATSD_PORT'].to_i,
41
+ socket_path: ENV['DD_DOGSTATSD_SOCKET'],
42
+ not_both_error_message:
43
+ "Both UDP (DD_AGENT_HOST/DD_DOGSTATSD_PORT #{ENV['DD_AGENT_HOST']}:#{ENV['DD_DOGSTATSD_PORT']}) " +
44
+ "and UDS (DD_DOGSTATSD_SOCKET #{ENV['DD_DOGSTATSD_SOCKET']}) environment variables are set. " +
45
+ "Set only one or the other." %
46
+ [ENV['DD_AGENT_HOST'], ENV['DD_DOGSTATSD_PORT'], ENV['DD_DOGSTATSD_SOCKET']])
47
+ end
48
+
49
+ def initialize_with_defaults()
50
+ try_initialize_with(host: DEFAULT_HOST, port: DEFAULT_PORT)
51
+ end
52
+
53
+ def try_initialize_with(host: nil, port: nil, socket_path: nil, not_both_error_message: "")
54
+ if (host || port) && socket_path
55
+ raise ArgumentError, not_both_error_message
56
+ end
57
+
58
+ if host || port
59
+ @host = host || DEFAULT_HOST
60
+ @port = port || DEFAULT_PORT
61
+ @socket_path = nil
62
+ @transport_type = :udp
63
+ return true
64
+ elsif socket_path
65
+ @host = nil
66
+ @port = nil
67
+ @socket_path = socket_path
68
+ @transport_type = :uds
69
+ return true
70
+ end
71
+
72
+ return false
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,131 @@
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
+ @transport_type = connection_cfg.transport_type
27
+
28
+ if telemetry_flush_interval
29
+ @telemetry = Telemetry.new(telemetry_flush_interval,
30
+ global_tags: global_tags,
31
+ transport_type: @transport_type
32
+ )
33
+ end
34
+
35
+ @connection = connection_cfg.make_connection(logger: logger, telemetry: telemetry)
36
+
37
+ # Initialize buffer
38
+ buffer_max_payload_size ||= (@transport_type == :udp ?
39
+ UDP_DEFAULT_BUFFER_SIZE : UDS_DEFAULT_BUFFER_SIZE)
40
+
41
+ if buffer_max_payload_size <= 0
42
+ raise ArgumentError, 'buffer_max_payload_size cannot be <= 0'
43
+ end
44
+
45
+ unless telemetry.nil? || telemetry.would_fit_in?(buffer_max_payload_size)
46
+ raise ArgumentError, "buffer_max_payload_size is not high enough to use telemetry (tags=(#{global_tags.inspect}))"
47
+ end
48
+
49
+ buffer = MessageBuffer.new(@connection,
50
+ max_payload_size: buffer_max_payload_size,
51
+ max_pool_size: buffer_max_pool_size || DEFAULT_BUFFER_POOL_SIZE,
52
+ overflowing_stategy: buffer_overflowing_stategy,
53
+ )
54
+
55
+ sender_queue_size ||= (@transport_type == :udp ?
56
+ UDP_DEFAULT_SENDER_QUEUE_SIZE : UDS_DEFAULT_SENDER_QUEUE_SIZE)
57
+
58
+ @sender = single_thread ?
59
+ SingleThreadSender.new(
60
+ buffer,
61
+ logger: logger,
62
+ flush_interval: buffer_flush_interval) :
63
+ Sender.new(
64
+ buffer,
65
+ logger: logger,
66
+ flush_interval: buffer_flush_interval,
67
+ telemetry: @telemetry,
68
+ queue_size: sender_queue_size)
69
+ @sender.start
70
+ end
71
+
72
+ def send_message(message)
73
+ sender.add(message)
74
+
75
+ tick_telemetry
76
+ end
77
+
78
+ def sync_with_outbound_io
79
+ sender.rendez_vous
80
+ end
81
+
82
+ def flush(flush_telemetry: false, sync: false)
83
+ do_flush_telemetry if telemetry && flush_telemetry
84
+
85
+ sender.flush(sync: sync)
86
+ end
87
+
88
+ def host
89
+ return nil unless transport_type == :udp
90
+
91
+ connection.host
92
+ end
93
+
94
+ def port
95
+ return nil unless transport_type == :udp
96
+
97
+ connection.port
98
+ end
99
+
100
+ def socket_path
101
+ return nil unless transport_type == :uds
102
+
103
+ connection.socket_path
104
+ end
105
+
106
+ def close
107
+ sender.stop
108
+ connection.close
109
+ end
110
+
111
+ private
112
+ attr_reader :sender
113
+ attr_reader :connection
114
+
115
+ def do_flush_telemetry
116
+ telemetry_snapshot = telemetry.flush
117
+ telemetry.reset
118
+
119
+ telemetry_snapshot.each do |message|
120
+ sender.add(message)
121
+ end
122
+ end
123
+
124
+ def tick_telemetry
125
+ return nil unless telemetry
126
+
127
+ do_flush_telemetry if telemetry.should_flush?
128
+ end
129
+ end
130
+ end
131
+ 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