dogstatsd-ruby 4.0.0 → 5.5.0

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: 92868c3d0ccb9a7847eaec3dd2da46413ab8acd2ce2df0008ee66c3e6500f3f6
4
+ data.tar.gz: 26064f00534e7df4a132d722041eea6b85a22849ca5c3c7caed05722320bcd9e
5
5
  SHA512:
6
- metadata.gz: 16c82cb62bfd324d5e1dc1c6293fa121b92bd0c88d00474210c5da67f2cb07ae350cc0b717b15739c7a6f09e9021323ba348cec70ec2d0ac66a362f15f81007b
7
- data.tar.gz: 6e82cef56d746ecf7b10890d397e313c143611b18b4ae65db67eb2f252cb27acacb45de03f6447b7b4ebb1666209806d6b048fb2857c5c8790f1f6729f4ae806
6
+ metadata.gz: e9f076b5aa76f4102f66698d5d19a3fbcd84e2b8b3a5fa944dcac07ddf8ee25f48a65d90e5296cbd7327f04dc90a8aba0234606bc50ab7c335120e9e17c3a6d2
7
+ data.tar.gz: e07f4469b7737fe7d67d32623194c90e09adf3d78ef0c26226bfed103db4067fa01e582f0627d426695dd9a0ff93c4e1e7d79d227bd5ce806d9ca28b2a440382
data/README.md CHANGED
@@ -1,98 +1,227 @@
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
24
66
 
25
- # you could also create a statsd class if you need a drop in replacement
26
- # class Statsd < Datadog::Statsd
27
- # end
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
+ ```
28
73
 
29
- # Increment a counter.
30
- statsd.increment('page.views')
74
+ ### v5.x Common Pitfalls
31
75
 
32
- # Record a gauge 50% of the time.
33
- statsd.gauge('users.online', 123, :sample_rate=>0.5)
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:
34
77
 
35
- # Sample a histogram
36
- statsd.histogram('file.upload.size', 1234)
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.
37
79
 
38
- # Time a block of code
39
- statsd.time('page.render') do
40
- render_page('home.html')
41
- end
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.
42
81
 
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
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).
49
83
 
50
- # Tag a metric.
51
- statsd.histogram('query.time', 10, :tags => ["version:1"])
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:
52
86
 
53
- # Auto-close socket after end of block
54
- Datadog::Statsd.open('localhost', 8125) do |s|
55
- s.increment('page.views')
56
- end
87
+ ```ruby
88
+ statsd = Datadog::Statsd.new('localhost', 8125, single_thread: true)
89
+ # ...
90
+ # release resources used by the client instance and flush last metrics
91
+ statsd.close()
57
92
  ```
58
93
 
59
- You can also post events to your stream. You can tag them, set priority and even aggregate them with other events.
94
+ ### Origin detection over UDP
60
95
 
61
- Aggregation in the stream is made on hostname/event_type/source_type/aggregation_key.
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.
62
97
 
63
- ``` ruby
64
- # Post a simple message
65
- statsd.event("There might be a storm tomorrow", "A friend warned me earlier.")
98
+ To enable origin detection over UDP, add the following lines to your application manifest:
66
99
 
67
- # Cry for help
68
- statsd.event("SO MUCH SNOW", "Started yesterday and it won't stop !!", :alert_type => "error", :tags => ["urgent", "endoftheworld"])
100
+ ```yaml
101
+ env:
102
+ - name: DD_ENTITY_ID
103
+ valueFrom:
104
+ fieldRef:
105
+ fieldPath: metadata.uid
69
106
  ```
70
107
 
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.
109
+
110
+ ## Usage
111
+
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).
113
+
114
+ ### Metrics
115
+
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:
117
+
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)
123
+
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).
125
+
126
+ ### Events
127
+
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.
129
+
130
+ ### Service Checks
131
+
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.
133
+
134
+ ### Maximum packet size in high-throughput scenarios
135
+
136
+ In order to have the most efficient use of this library in high-throughput scenarios,
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:
142
+
143
+ ```ruby
144
+ statsd = Datadog::Statsd.new('localhost', 8125, buffer_max_payload_size: 4096)
145
+ # ...
146
+ statsd.close()
147
+ ```
148
+
149
+ ## Threading model
150
+
151
+ Starting with version 5.0, `dogstatsd-ruby` employs a new threading model where one instance of `Datadog::Statsd` can be shared between threads and where data sending is non-blocking (asynchronous).
152
+
153
+ When you instantiate a `Datadog::Statsd`, a sender thread is spawned. This thread will be called the Sender thread, as it is modeled by the [Sender](../lib/datadog/statsd/sender.rb) class. You can make use of `single_thread: true` to disable this behavior.
154
+
155
+ This thread is stopped when you close the statsd client (`Datadog::Statsd#close`). Instantiating a lot of statsd clients without calling `#close` after they are not needed anymore will most likely lead to threads being leaked.
156
+
157
+ The sender thread has the following logic (from `Datadog::Statsd::Sender#send_loop`):
158
+
159
+ ```
160
+ while the sender message queue is not closed do
161
+ read message from sender message queue
162
+
163
+ if message is a Control message to flush
164
+ flush buffer in connection
165
+ else if message is a Control message to synchronize
166
+ synchronize with calling thread
167
+ else
168
+ add message to the buffer
169
+ end
170
+ end while
171
+ ```
172
+
173
+ There are three different kinds of messages:
174
+
175
+ 1. a control message to flush the buffer in the connection
176
+ 2. a control message to synchronize any thread with the sender thread
177
+ 3. a message to append to the buffer
178
+
179
+ There is also an implicit message which closes the queue which will cause the sender thread to finish processing and exit.
180
+
181
+
182
+ ```ruby
183
+ statsd = Datadog::Statsd.new('localhost', 8125)
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) and UDS (512).
187
+
188
+ The `buffer_flush_interval`, if enabled, is implemented with an additional thread which manages the timing of those flushes. This additional thread is used even if `single_thread: true`.
189
+
190
+ ### Usual workflow
191
+
192
+ You push metrics to the statsd client which writes them quickly to the sender message queue. The sender thread receives those message, buffers them and flushes them to the connection when the buffer limit is reached.
193
+
194
+ ### Flushing
195
+
196
+ When calling `Datadog::Statsd#flush`, a specific control message (`:flush`) is sent to the sender thread. When the sender thread receives it, it flushes its internal buffer into the connection.
197
+
198
+ ### Rendez-vous
199
+
200
+ It is possible to ensure a message has been consumed by the sender thread and written to the buffer by simply calling a rendez-vous right after. This is done when you are doing a synchronous flush using `Datadog::Statsd#flush(sync: true)`.
71
201
 
202
+ Doing so means the caller thread is blocked and waiting until the data has been flushed by the sender thread.
72
203
 
73
- Documentation
74
- -------------
204
+ This is useful when preparing to exit the application or when checking unit tests.
75
205
 
76
- Full API documentation is available
77
- [here](http://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/frames).
206
+ ### Thread-safety
78
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)).
79
209
 
80
- Feedback
81
- --------
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.
82
211
 
83
- To suggest a feature, report a bug, or general discussion, head over
84
- [here](http://github.com/DataDog/dogstatsd-ruby/issues/).
212
+ ## Versioning
85
213
 
214
+ 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.
215
+ As much as possible, we will add a "future deprecation" message in the minor release preceding the one dropping the support.
86
216
 
87
- [Change Log](CHANGELOG.md)
88
- ----------------------------
217
+ ## Ruby Versions
89
218
 
219
+ This gem supports and is tested on Ruby minor versions 2.1 through 3.1.
220
+ Support for Ruby 2.0 was dropped in version 5.4.0.
90
221
 
91
- Credits
92
- -------
222
+ ## Credits
93
223
 
94
- dogstatsd-ruby is forked from Rien Henrichs [original Statsd
95
- client](https://github.com/reinh/statsd).
224
+ dogstatsd-ruby is forked from Rein Henrichs' [original Statsd client](https://github.com/reinh/statsd).
96
225
 
97
226
  Copyright (c) 2011 Rein Henrichs. See LICENSE.txt for
98
227
  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
+ )
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,133 @@
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
+ @telemetry = if telemetry_flush_interval
29
+ Telemetry.new(telemetry_flush_interval,
30
+ global_tags: global_tags,
31
+ transport_type: @transport_type
32
+ )
33
+ else
34
+ nil
35
+ end
36
+
37
+ @connection = connection_cfg.make_connection(logger: logger, telemetry: telemetry)
38
+
39
+ # Initialize buffer
40
+ buffer_max_payload_size ||= (@transport_type == :udp ?
41
+ UDP_DEFAULT_BUFFER_SIZE : UDS_DEFAULT_BUFFER_SIZE)
42
+
43
+ if buffer_max_payload_size <= 0
44
+ raise ArgumentError, 'buffer_max_payload_size cannot be <= 0'
45
+ end
46
+
47
+ unless telemetry.nil? || telemetry.would_fit_in?(buffer_max_payload_size)
48
+ raise ArgumentError, "buffer_max_payload_size is not high enough to use telemetry (tags=(#{global_tags.inspect}))"
49
+ end
50
+
51
+ buffer = MessageBuffer.new(@connection,
52
+ max_payload_size: buffer_max_payload_size,
53
+ max_pool_size: buffer_max_pool_size || DEFAULT_BUFFER_POOL_SIZE,
54
+ overflowing_stategy: buffer_overflowing_stategy,
55
+ )
56
+
57
+ sender_queue_size ||= (@transport_type == :udp ?
58
+ UDP_DEFAULT_SENDER_QUEUE_SIZE : UDS_DEFAULT_SENDER_QUEUE_SIZE)
59
+
60
+ @sender = single_thread ?
61
+ SingleThreadSender.new(
62
+ buffer,
63
+ logger: logger,
64
+ flush_interval: buffer_flush_interval) :
65
+ Sender.new(
66
+ buffer,
67
+ logger: logger,
68
+ flush_interval: buffer_flush_interval,
69
+ telemetry: @telemetry,
70
+ queue_size: sender_queue_size)
71
+ @sender.start
72
+ end
73
+
74
+ def send_message(message)
75
+ sender.add(message)
76
+
77
+ tick_telemetry
78
+ end
79
+
80
+ def sync_with_outbound_io
81
+ sender.rendez_vous
82
+ end
83
+
84
+ def flush(flush_telemetry: false, sync: false)
85
+ do_flush_telemetry if telemetry && flush_telemetry
86
+
87
+ sender.flush(sync: sync)
88
+ end
89
+
90
+ def host
91
+ return nil unless transport_type == :udp
92
+
93
+ connection.host
94
+ end
95
+
96
+ def port
97
+ return nil unless transport_type == :udp
98
+
99
+ connection.port
100
+ end
101
+
102
+ def socket_path
103
+ return nil unless transport_type == :uds
104
+
105
+ connection.socket_path
106
+ end
107
+
108
+ def close
109
+ sender.stop
110
+ connection.close
111
+ end
112
+
113
+ private
114
+ attr_reader :sender
115
+ attr_reader :connection
116
+
117
+ def do_flush_telemetry
118
+ telemetry_snapshot = telemetry.flush
119
+ telemetry.reset
120
+
121
+ telemetry_snapshot.each do |message|
122
+ sender.add(message)
123
+ end
124
+ end
125
+
126
+ def tick_telemetry
127
+ return nil unless telemetry
128
+
129
+ do_flush_telemetry if telemetry.should_flush?
130
+ end
131
+ end
132
+ end
133
+ 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