dogstatsd-ruby 5.2.0 → 5.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,13 @@ require_relative 'statsd/version'
5
5
  require_relative 'statsd/telemetry'
6
6
  require_relative 'statsd/udp_connection'
7
7
  require_relative 'statsd/uds_connection'
8
+ require_relative 'statsd/connection_cfg'
8
9
  require_relative 'statsd/message_buffer'
9
10
  require_relative 'statsd/serialization'
10
11
  require_relative 'statsd/sender'
11
12
  require_relative 'statsd/single_thread_sender'
12
13
  require_relative 'statsd/forwarder'
14
+ require_relative 'statsd/timer'
13
15
 
14
16
  # = Datadog::Statsd: A DogStatsd client (https://www.datadoghq.com)
15
17
  #
@@ -40,7 +42,12 @@ module Datadog
40
42
  UDP_DEFAULT_BUFFER_SIZE = 1_432
41
43
  UDS_DEFAULT_BUFFER_SIZE = 8_192
42
44
  DEFAULT_BUFFER_POOL_SIZE = Float::INFINITY
45
+
46
+ UDP_DEFAULT_SENDER_QUEUE_SIZE = 2048
47
+ UDS_DEFAULT_SENDER_QUEUE_SIZE = 512
48
+
43
49
  MAX_EVENT_SIZE = 8 * 1_024
50
+
44
51
  # minimum flush interval for the telemetry in seconds
45
52
  DEFAULT_TELEMETRY_FLUSH_INTERVAL = 10
46
53
 
@@ -69,9 +76,12 @@ module Datadog
69
76
  # @option [Logger] logger for debugging
70
77
  # @option [Integer] buffer_max_payload_size max bytes to buffer
71
78
  # @option [Integer] buffer_max_pool_size max messages to buffer
79
+ # @option [Integer] sender_queue_size size of the sender queue in number of buffers
80
+ # @option [Numeric] buffer_flush_interval interval in second to flush buffer
72
81
  # @option [String] socket_path unix socket path
73
82
  # @option [Float] default sample rate if not overridden
74
83
  # @option [Boolean] single_thread flushes the metrics on the main thread instead of in a companion thread
84
+ # @option [Boolean] delay_serialization delays stat serialization
75
85
  def initialize(
76
86
  host = nil,
77
87
  port = nil,
@@ -84,10 +94,14 @@ module Datadog
84
94
  buffer_max_payload_size: nil,
85
95
  buffer_max_pool_size: nil,
86
96
  buffer_overflowing_stategy: :drop,
97
+ buffer_flush_interval: nil,
98
+
99
+ sender_queue_size: nil,
87
100
 
88
101
  logger: nil,
89
102
 
90
103
  single_thread: false,
104
+ delay_serialization: false,
91
105
 
92
106
  telemetry_enable: true,
93
107
  telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL
@@ -100,11 +114,14 @@ module Datadog
100
114
  @prefix = @namespace ? "#{@namespace}.".freeze : nil
101
115
  @serializer = Serialization::Serializer.new(prefix: @prefix, global_tags: tags)
102
116
  @sample_rate = sample_rate
117
+ @delay_serialization = delay_serialization
103
118
 
104
119
  @forwarder = Forwarder.new(
105
- host: host,
106
- port: port,
107
- socket_path: socket_path,
120
+ connection_cfg: ConnectionCfg.new(
121
+ host: host,
122
+ port: port,
123
+ socket_path: socket_path,
124
+ ),
108
125
 
109
126
  global_tags: tags,
110
127
  logger: logger,
@@ -114,19 +131,24 @@ module Datadog
114
131
  buffer_max_payload_size: buffer_max_payload_size,
115
132
  buffer_max_pool_size: buffer_max_pool_size,
116
133
  buffer_overflowing_stategy: buffer_overflowing_stategy,
134
+ buffer_flush_interval: buffer_flush_interval,
135
+
136
+ sender_queue_size: sender_queue_size,
117
137
 
118
138
  telemetry_flush_interval: telemetry_enable ? telemetry_flush_interval : nil,
139
+ serializer: serializer
119
140
  )
120
141
  end
121
142
 
122
143
  # yield a new instance to a block and close it when done
123
144
  # for short-term use-cases that don't want to close the socket manually
124
- def self.open(*args)
125
- instance = new(*args)
145
+ # TODO: replace with ... once we are on ruby 2.7
146
+ def self.open(*args, **kwargs)
147
+ instance = new(*args, **kwargs)
126
148
 
127
149
  yield instance
128
150
  ensure
129
- instance.close
151
+ instance.close if instance
130
152
  end
131
153
 
132
154
  # Sends an increment (count = 1) for the given stat to the statsd server.
@@ -134,6 +156,7 @@ module Datadog
134
156
  # @param [String] stat stat name
135
157
  # @param [Hash] opts the options to create the metric with
136
158
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
159
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
137
160
  # @option opts [Array<String>] :tags An array of tags
138
161
  # @option opts [Numeric] :by increment value, default 1
139
162
  # @see #count
@@ -148,6 +171,7 @@ module Datadog
148
171
  # @param [String] stat stat name
149
172
  # @param [Hash] opts the options to create the metric with
150
173
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
174
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
151
175
  # @option opts [Array<String>] :tags An array of tags
152
176
  # @option opts [Numeric] :by decrement value, default 1
153
177
  # @see #count
@@ -163,13 +187,14 @@ module Datadog
163
187
  # @param [Integer] count count
164
188
  # @param [Hash] opts the options to create the metric with
165
189
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
190
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
166
191
  # @option opts [Array<String>] :tags An array of tags
167
192
  def count(stat, count, opts = EMPTY_OPTIONS)
168
193
  opts = { sample_rate: opts } if opts.is_a?(Numeric)
169
194
  send_stats(stat, count, COUNTER_TYPE, opts)
170
195
  end
171
196
 
172
- # Sends an arbitary gauge value for the given stat to the statsd server.
197
+ # Sends an arbitrary gauge value for the given stat to the statsd server.
173
198
  #
174
199
  # This is useful for recording things like available disk space,
175
200
  # memory usage, and the like, which have different semantics than
@@ -179,6 +204,7 @@ module Datadog
179
204
  # @param [Numeric] value gauge value.
180
205
  # @param [Hash] opts the options to create the metric with
181
206
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
207
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
182
208
  # @option opts [Array<String>] :tags An array of tags
183
209
  # @example Report the current user count:
184
210
  # $statsd.gauge('user.count', User.count)
@@ -193,6 +219,7 @@ module Datadog
193
219
  # @param [Numeric] value histogram value.
194
220
  # @param [Hash] opts the options to create the metric with
195
221
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
222
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
196
223
  # @option opts [Array<String>] :tags An array of tags
197
224
  # @example Report the current user count:
198
225
  # $statsd.histogram('user.count', User.count)
@@ -206,6 +233,7 @@ module Datadog
206
233
  # @param [Numeric] value distribution value.
207
234
  # @param [Hash] opts the options to create the metric with
208
235
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
236
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
209
237
  # @option opts [Array<String>] :tags An array of tags
210
238
  # @example Report the current user count:
211
239
  # $statsd.distribution('user.count', User.count)
@@ -213,6 +241,26 @@ module Datadog
213
241
  send_stats(stat, value, DISTRIBUTION_TYPE, opts)
214
242
  end
215
243
 
244
+ # Reports execution time of the provided block as a distribution.
245
+ #
246
+ # If the block fails, the stat is still reported, then the error
247
+ # is reraised
248
+ #
249
+ # @param [String] stat stat name.
250
+ # @param [Numeric] value distribution value.
251
+ # @param [Hash] opts the options to create the metric with
252
+ # @option opts [Numeric] :sample_rate sample rate, 1 for always
253
+ # @option opts [Array<String>] :tags An array of tags
254
+ # @example Report the time (in ms) taken to activate an account
255
+ # $statsd.distribution_time('account.activate') { @account.activate! }
256
+ def distribution_time(stat, opts = EMPTY_OPTIONS)
257
+ opts = { sample_rate: opts } if opts.is_a?(Numeric)
258
+ start = now
259
+ yield
260
+ ensure
261
+ distribution(stat, ((now - start) * 1000).round, opts)
262
+ end
263
+
216
264
  # Sends a timing (in ms) for the given stat to the statsd server. The
217
265
  # sample_rate determines what percentage of the time this report is sent. The
218
266
  # statsd server then uses the sample_rate to correctly track the average
@@ -222,6 +270,7 @@ module Datadog
222
270
  # @param [Integer] ms timing in milliseconds
223
271
  # @param [Hash] opts the options to create the metric with
224
272
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
273
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
225
274
  # @option opts [Array<String>] :tags An array of tags
226
275
  def timing(stat, ms, opts = EMPTY_OPTIONS)
227
276
  opts = { sample_rate: opts } if opts.is_a?(Numeric)
@@ -236,6 +285,7 @@ module Datadog
236
285
  # @param [String] stat stat name
237
286
  # @param [Hash] opts the options to create the metric with
238
287
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
288
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
239
289
  # @option opts [Array<String>] :tags An array of tags
240
290
  # @yield The operation to be timed
241
291
  # @see #timing
@@ -255,6 +305,7 @@ module Datadog
255
305
  # @param [Numeric] value set value.
256
306
  # @param [Hash] opts the options to create the metric with
257
307
  # @option opts [Numeric] :sample_rate sample rate, 1 for always
308
+ # @option opts [Boolean] :pre_sampled If true, the client assumes the caller has already sampled metrics at :sample_rate, and doesn't perform sampling.
258
309
  # @option opts [Array<String>] :tags An array of tags
259
310
  # @example Record a unique visitory by id:
260
311
  # $statsd.set('visitors.uniques', User.id)
@@ -366,17 +417,10 @@ module Datadog
366
417
  attr_reader :serializer
367
418
  attr_reader :forwarder
368
419
 
369
- PROCESS_TIME_SUPPORTED = (RUBY_VERSION >= '2.1.0')
370
420
  EMPTY_OPTIONS = {}.freeze
371
421
 
372
- if PROCESS_TIME_SUPPORTED
373
- def now
374
- Process.clock_gettime(Process::CLOCK_MONOTONIC)
375
- end
376
- else
377
- def now
378
- Time.now.to_f
379
- end
422
+ def now
423
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
380
424
  end
381
425
 
382
426
  def send_stats(stat, delta, type, opts = EMPTY_OPTIONS)
@@ -384,8 +428,13 @@ module Datadog
384
428
 
385
429
  sample_rate = opts[:sample_rate] || @sample_rate || 1
386
430
 
387
- if sample_rate == 1 || rand <= sample_rate
388
- full_stat = serializer.to_stat(stat, delta, type, tags: opts[:tags], sample_rate: sample_rate)
431
+ if sample_rate == 1 || opts[:pre_sampled] || rand <= sample_rate
432
+ full_stat =
433
+ if @delay_serialization
434
+ [[stat, delta, type], {tags: opts[:tags], sample_rate: sample_rate}]
435
+ else
436
+ serializer.to_stat(stat, delta, type, tags: opts[:tags], sample_rate: sample_rate)
437
+ end
389
438
 
390
439
  forwarder.send_message(full_stat)
391
440
  end
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.2.0
4
+ version: 5.6.1
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-07-01 00:00:00.000000000 Z
12
+ date: 2023-09-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Ruby DogStatsd client
15
15
  email: code@datadoghq.com
@@ -23,6 +23,7 @@ files:
23
23
  - README.md
24
24
  - lib/datadog/statsd.rb
25
25
  - lib/datadog/statsd/connection.rb
26
+ - lib/datadog/statsd/connection_cfg.rb
26
27
  - lib/datadog/statsd/forwarder.rb
27
28
  - lib/datadog/statsd/message_buffer.rb
28
29
  - lib/datadog/statsd/sender.rb
@@ -34,6 +35,7 @@ files:
34
35
  - lib/datadog/statsd/serialization/tag_serializer.rb
35
36
  - lib/datadog/statsd/single_thread_sender.rb
36
37
  - lib/datadog/statsd/telemetry.rb
38
+ - lib/datadog/statsd/timer.rb
37
39
  - lib/datadog/statsd/udp_connection.rb
38
40
  - lib/datadog/statsd/uds_connection.rb
39
41
  - lib/datadog/statsd/version.rb
@@ -42,10 +44,14 @@ licenses:
42
44
  - MIT
43
45
  metadata:
44
46
  bug_tracker_uri: https://github.com/DataDog/dogstatsd-ruby/issues
45
- changelog_uri: https://github.com/DataDog/dogstatsd-ruby/blob/v5.2.0/CHANGELOG.md
46
- documentation_uri: https://www.rubydoc.info/gems/dogstatsd-ruby/5.2.0
47
- source_code_uri: https://github.com/DataDog/dogstatsd-ruby/tree/v5.2.0
48
- post_install_message:
47
+ changelog_uri: https://github.com/DataDog/dogstatsd-ruby/blob/v5.6.1/CHANGELOG.md
48
+ documentation_uri: https://www.rubydoc.info/gems/dogstatsd-ruby/5.6.1
49
+ source_code_uri: https://github.com/DataDog/dogstatsd-ruby/tree/v5.6.1
50
+ post_install_message: |2+
51
+
52
+ If you are upgrading from v4.x of the dogstatsd-ruby library, note the major change to the threading model:
53
+ https://github.com/DataDog/dogstatsd-ruby#migrating-from-v4x-to-v5x
54
+
49
55
  rdoc_options: []
50
56
  require_paths:
51
57
  - lib
@@ -53,16 +59,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
59
  requirements:
54
60
  - - ">="
55
61
  - !ruby/object:Gem::Version
56
- version: 2.0.0
62
+ version: 2.1.0
57
63
  required_rubygems_version: !ruby/object:Gem::Requirement
58
64
  requirements:
59
65
  - - ">="
60
66
  - !ruby/object:Gem::Version
61
67
  version: '0'
62
68
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 2.7.10
69
+ rubygems_version: 3.2.3
65
70
  signing_key:
66
71
  specification_version: 4
67
72
  summary: A Ruby DogStatsd client
68
73
  test_files: []
74
+ ...