Class: Datadog::Statsd
- Inherits:
-
Object
- Object
- Datadog::Statsd
- Defined in:
- lib/datadog/statsd.rb,
lib/datadog/statsd/timer.rb,
lib/datadog/statsd/sender.rb,
lib/datadog/statsd/version.rb,
lib/datadog/statsd/forwarder.rb,
lib/datadog/statsd/telemetry.rb,
lib/datadog/statsd/connection.rb,
lib/datadog/statsd/serialization.rb,
lib/datadog/statsd/connection_cfg.rb,
lib/datadog/statsd/message_buffer.rb,
lib/datadog/statsd/udp_connection.rb,
lib/datadog/statsd/uds_connection.rb,
lib/datadog/statsd/origin_detection.rb,
lib/datadog/statsd/single_thread_sender.rb,
lib/datadog/statsd/serialization/serializer.rb,
lib/datadog/statsd/serialization/tag_serializer.rb,
lib/datadog/statsd/serialization/stat_serializer.rb,
lib/datadog/statsd/serialization/event_serializer.rb,
lib/datadog/statsd/serialization/field_serializer.rb,
lib/datadog/statsd/serialization/service_check_serializer.rb
Defined Under Namespace
Modules: Serialization Classes: Connection, ConnectionCfg, Error, Forwarder, MessageBuffer, Sender, SingleThreadSender, Telemetry, Timer, UDPConnection, UDSConnection
Constant Summary collapse
- OK =
0
- WARNING =
1
- CRITICAL =
2
- UNKNOWN =
3
- UDP_DEFAULT_BUFFER_SIZE =
1_432
- UDS_DEFAULT_BUFFER_SIZE =
8_192
- DEFAULT_BUFFER_POOL_SIZE =
Float::INFINITY
- UDP_DEFAULT_SENDER_QUEUE_SIZE =
2048
- UDS_DEFAULT_SENDER_QUEUE_SIZE =
512
- MAX_EVENT_SIZE =
8 * 1_024
- DEFAULT_TELEMETRY_FLUSH_INTERVAL =
minimum flush interval for the telemetry in seconds
10
- COUNTER_TYPE =
'c'
- GAUGE_TYPE =
'g'
- HISTOGRAM_TYPE =
'h'
- DISTRIBUTION_TYPE =
'd'
- TIMING_TYPE =
'ms'
- SET_TYPE =
's'
- VERSION =
'5.7.1'
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
A namespace to prepend to all statsd calls.
-
#sample_rate ⇒ Object
readonly
Default sample rate.
Class Method Summary collapse
-
.open(*args, **kwargs) ⇒ Object
yield a new instance to a block and close it when done for short-term use-cases that don’t want to close the socket manually TODO: replace with …
Instance Method Summary collapse
-
#batch {|_self| ... } ⇒ Object
Send several metrics in the same packet.
-
#close(flush: true) ⇒ Object
Close the underlying socket.
-
#count(stat, count, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitrary count for the given stat to the statsd server.
-
#decrement(stat, opts = EMPTY_OPTIONS) ⇒ Object
Sends a decrement (count = -1) for the given stat to the statsd server.
-
#distribution(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends a value to be tracked as a distribution to the statsd server.
-
#distribution_time(stat, opts = EMPTY_OPTIONS) ⇒ Object
Reports execution time of the provided block as a distribution.
-
#event(title, text, opts = EMPTY_OPTIONS) ⇒ Object
This end point allows you to post events to the stream.
-
#flush(flush_telemetry: false, sync: false) ⇒ Object
Flush the buffer into the connection.
-
#gauge(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitrary gauge value for the given stat to the statsd server.
-
#histogram(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends a value to be tracked as a histogram to the statsd server.
- #host ⇒ Object
-
#increment(stat, opts = EMPTY_OPTIONS) ⇒ Object
Sends an increment (count = 1) for the given stat to the statsd server.
-
#initialize(host = nil, port = nil, socket_path: nil, namespace: nil, tags: nil, sample_rate: nil, buffer_max_payload_size: nil, buffer_max_pool_size: nil, buffer_overflowing_stategy: :drop, buffer_flush_interval: nil, sender_queue_size: nil, logger: nil, single_thread: false, delay_serialization: false, telemetry_enable: true, telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL, origin_detection: true, container_id: nil, cardinality: nil) ⇒ Statsd
constructor
A new instance of Statsd.
- #port ⇒ Object
- #service_check(name, status, opts = EMPTY_OPTIONS) ⇒ Object
-
#set(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends a value to be tracked as a set to the statsd server.
- #socket_path ⇒ Object
- #sync_with_outbound_io ⇒ Object
-
#tags ⇒ Object
Global tags to be added to every statsd call.
- #telemetry ⇒ Object
-
#time(stat, opts = EMPTY_OPTIONS) { ... } ⇒ Object
Reports execution time of the provided block using #timing.
-
#timing(stat, ms, opts = EMPTY_OPTIONS) ⇒ Object
Sends a timing (in ms) for the given stat to the statsd server.
- #transport_type ⇒ Object
Constructor Details
#initialize(host = nil, port = nil, socket_path: nil, namespace: nil, tags: nil, sample_rate: nil, buffer_max_payload_size: nil, buffer_max_pool_size: nil, buffer_overflowing_stategy: :drop, buffer_flush_interval: nil, sender_queue_size: nil, logger: nil, single_thread: false, delay_serialization: false, telemetry_enable: true, telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL, origin_detection: true, container_id: nil, cardinality: nil) ⇒ Statsd
Returns a new instance of Statsd.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/datadog/statsd.rb', line 89 def initialize( host = nil, port = nil, socket_path: nil, namespace: nil, tags: nil, sample_rate: nil, buffer_max_payload_size: nil, buffer_max_pool_size: nil, buffer_overflowing_stategy: :drop, buffer_flush_interval: nil, sender_queue_size: nil, logger: nil, single_thread: false, delay_serialization: false, telemetry_enable: true, telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL, origin_detection: true, container_id: nil, cardinality: nil ) unless .nil? || .is_a?(Array) || .is_a?(Hash) raise ArgumentError, 'tags must be an array of string tags or a Hash' end @namespace = namespace @prefix = @namespace ? "#{@namespace}.".freeze : nil origin_detection_enabled = origin_detection_enabled?(origin_detection) container_id = get_container_id(container_id, origin_detection_enabled) external_data = sanitize(ENV['DD_EXTERNAL_ENV']) if origin_detection_enabled @serializer = Serialization::Serializer.new(prefix: @prefix, container_id: container_id, external_data: external_data, global_tags: , ) @cardinality = cardinality || ENV['DD_CARDINALITY'] || ENV['DATADOG_CARDINALITY'] @sample_rate = sample_rate @delay_serialization = delay_serialization @forwarder = Forwarder.new( connection_cfg: ConnectionCfg.new( host: host, port: port, socket_path: socket_path, ), global_tags: , logger: logger, single_thread: single_thread, buffer_max_payload_size: buffer_max_payload_size, buffer_max_pool_size: buffer_max_pool_size, buffer_overflowing_stategy: buffer_overflowing_stategy, buffer_flush_interval: buffer_flush_interval, sender_queue_size: sender_queue_size, telemetry_flush_interval: telemetry_enable ? telemetry_flush_interval : nil, container_id: container_id, external_data: external_data, cardinality: @cardinality, serializer: serializer ) end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
A namespace to prepend to all statsd calls. Defaults to no namespace.
63 64 65 |
# File 'lib/datadog/statsd.rb', line 63 def namespace @namespace end |
#sample_rate ⇒ Object (readonly)
Default sample rate
71 72 73 |
# File 'lib/datadog/statsd.rb', line 71 def sample_rate @sample_rate end |
Class Method Details
.open(*args, **kwargs) ⇒ Object
yield a new instance to a block and close it when done for short-term use-cases that don’t want to close the socket manually TODO: replace with … once we are on ruby 2.7
171 172 173 174 175 176 177 |
# File 'lib/datadog/statsd.rb', line 171 def self.open(*args, **kwargs) instance = new(*args, **kwargs) yield instance ensure instance.close if instance end |
Instance Method Details
#batch {|_self| ... } ⇒ Object
Send several metrics in the same packet. They will be buffered and flushed when the block finishes.
This method exists for compatibility with v4.x versions, it is not needed anymore since the batching is now automatically done internally. It also means that an automatic flush could occur if the buffer is filled during the execution of the batch block.
This method is DEPRECATED and will be removed in future v6.x API.
410 411 412 413 |
# File 'lib/datadog/statsd.rb', line 410 def batch yield self flush(sync: true) end |
#close(flush: true) ⇒ Object
Close the underlying socket
418 419 420 421 |
# File 'lib/datadog/statsd.rb', line 418 def close(flush: true) flush(sync: true) if flush forwarder.close end |
#count(stat, count, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitrary count for the given stat to the statsd server.
220 221 222 223 |
# File 'lib/datadog/statsd.rb', line 220 def count(stat, count, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) send_stats(stat, count, COUNTER_TYPE, opts) end |
#decrement(stat, opts = EMPTY_OPTIONS) ⇒ Object
Sends a decrement (count = -1) for the given stat to the statsd server.
205 206 207 208 209 |
# File 'lib/datadog/statsd.rb', line 205 def decrement(stat, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) decr_value = - opts.fetch(:by, 1) count(stat, decr_value, opts) end |
#distribution(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends a value to be tracked as a distribution to the statsd server.
271 272 273 |
# File 'lib/datadog/statsd.rb', line 271 def distribution(stat, value, opts = EMPTY_OPTIONS) send_stats(stat, value, DISTRIBUTION_TYPE, opts) end |
#distribution_time(stat, opts = EMPTY_OPTIONS) ⇒ Object
Reports execution time of the provided block as a distribution.
If the block fails, the stat is still reported, then the error is reraised
288 289 290 291 292 293 294 |
# File 'lib/datadog/statsd.rb', line 288 def distribution_time(stat, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) start = now yield ensure distribution(stat, ((now - start) * 1000).round, opts) end |
#event(title, text, opts = EMPTY_OPTIONS) ⇒ Object
This end point allows you to post events to the stream. You can tag them, set priority and even aggregate them with other events.
Aggregation in the stream is made on hostname/event_type/source_type/aggregation_key. If there’s no event type, for example, then that won’t matter; it will be grouped with other events that don’t have an event type.
389 390 391 392 393 |
# File 'lib/datadog/statsd.rb', line 389 def event(title, text, opts = EMPTY_OPTIONS) telemetry.sent(events: 1) if telemetry forwarder.(serializer.to_event(title, text, opts)) end |
#flush(flush_telemetry: false, sync: false) ⇒ Object
Flush the buffer into the connection
428 429 430 |
# File 'lib/datadog/statsd.rb', line 428 def flush(flush_telemetry: false, sync: false) forwarder.flush(flush_telemetry: flush_telemetry, sync: sync) end |
#gauge(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitrary gauge value for the given stat to the statsd server.
This is useful for recording things like available disk space, memory usage, and the like, which have different semantics than counters.
240 241 242 243 |
# File 'lib/datadog/statsd.rb', line 240 def gauge(stat, value, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) send_stats(stat, value, GAUGE_TYPE, opts) end |
#histogram(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends a value to be tracked as a histogram to the statsd server.
256 257 258 |
# File 'lib/datadog/statsd.rb', line 256 def histogram(stat, value, opts = EMPTY_OPTIONS) send_stats(stat, value, HISTOGRAM_TYPE, opts) end |
#host ⇒ Object
436 437 438 |
# File 'lib/datadog/statsd.rb', line 436 def host forwarder.host end |
#increment(stat, opts = EMPTY_OPTIONS) ⇒ Object
Sends an increment (count = 1) for the given stat to the statsd server.
189 190 191 192 193 |
# File 'lib/datadog/statsd.rb', line 189 def increment(stat, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) incr_value = opts.fetch(:by, 1) count(stat, incr_value, opts) end |
#port ⇒ Object
440 441 442 |
# File 'lib/datadog/statsd.rb', line 440 def port forwarder.port end |
#service_check(name, status, opts = EMPTY_OPTIONS) ⇒ Object
363 364 365 366 367 |
# File 'lib/datadog/statsd.rb', line 363 def service_check(name, status, opts = EMPTY_OPTIONS) telemetry.sent(service_checks: 1) if telemetry forwarder.(serializer.to_service_check(name, status, opts)) end |
#set(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends a value to be tracked as a set to the statsd server.
347 348 349 350 |
# File 'lib/datadog/statsd.rb', line 347 def set(stat, value, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) send_stats(stat, value, SET_TYPE, opts) end |
#socket_path ⇒ Object
444 445 446 |
# File 'lib/datadog/statsd.rb', line 444 def socket_path forwarder.socket_path end |
#sync_with_outbound_io ⇒ Object
423 424 425 |
# File 'lib/datadog/statsd.rb', line 423 def sync_with_outbound_io forwarder.sync_with_outbound_io end |
#tags ⇒ Object
Global tags to be added to every statsd call. Defaults to no tags.
66 67 68 |
# File 'lib/datadog/statsd.rb', line 66 def serializer. end |
#telemetry ⇒ Object
432 433 434 |
# File 'lib/datadog/statsd.rb', line 432 def telemetry forwarder.telemetry end |
#time(stat, opts = EMPTY_OPTIONS) { ... } ⇒ Object
Reports execution time of the provided block using #timing.
If the block fails, the stat is still reported, then the error is reraised
328 329 330 331 332 333 334 |
# File 'lib/datadog/statsd.rb', line 328 def time(stat, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) start = now yield ensure timing(stat, ((now - start) * 1000).round, opts) end |
#timing(stat, ms, opts = EMPTY_OPTIONS) ⇒ Object
Sends a timing (in ms) for the given stat to the statsd server. The sample_rate determines what percentage of the time this report is sent. The statsd server then uses the sample_rate to correctly track the average timing for the stat.
308 309 310 311 |
# File 'lib/datadog/statsd.rb', line 308 def timing(stat, ms, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) send_stats(stat, ms, TIMING_TYPE, opts) end |
#transport_type ⇒ Object
448 449 450 |
# File 'lib/datadog/statsd.rb', line 448 def transport_type forwarder.transport_type end |