Class: Datadog::Statsd
- Inherits:
-
Object
- Object
- Datadog::Statsd
- Defined in:
- lib/datadog/statsd.rb
Defined Under Namespace
Classes: Batch, Connection
Constant Summary collapse
- OPTS_KEYS =
Create a dictionary to assign a key to every parameter’s name, except for tags (treated differently) Goal: Simple and fast to add some other parameters
{ :date_happened => :d, :hostname => :h, :aggregation_key => :k, :priority => :p, :source_type_name => :s, :alert_type => :t, }
- SC_OPT_KEYS =
Service check options
{ :timestamp => 'd:'.freeze, :hostname => 'h:'.freeze, :tags => '#'.freeze, :message => 'm:'.freeze, }
- OK =
0- WARNING =
1- CRITICAL =
2- UNKNOWN =
3- MAX_EVENT_SIZE =
8 * 1024
- COUNTER_TYPE =
'c'.freeze
- GAUGE_TYPE =
'g'.freeze
- HISTOGRAM_TYPE =
'h'.freeze
- DISTRIBUTION_TYPE =
'd'.freeze
- TIMING_TYPE =
'ms'.freeze
- SET_TYPE =
's'.freeze
- VERSION =
"4.2.0".freeze
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Buffer containing the statsd message before they are sent in batch.
-
#connection ⇒ Object
readonly
Connection.
-
#max_buffer_bytes ⇒ Object
readonly
Maximum buffer size in bytes before it is flushed.
-
#namespace ⇒ Object
readonly
A namespace to prepend to all statsd calls.
-
#tags ⇒ Object
readonly
Global tags to be added to every statsd call.
Class Method Summary collapse
-
.open(*args) ⇒ 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.
Instance Method Summary collapse
-
#batch ⇒ Object
Send several metrics in the same UDP Packet They will be buffered and flushed when the block finishes.
-
#close ⇒ 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.
-
#event(title, text, opts = EMPTY_OPTIONS) ⇒ Object
This end point allows you to post events to the stream.
-
#gauge(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitary 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.
-
#increment(stat, opts = EMPTY_OPTIONS) ⇒ Object
Sends an increment (count = 1) for the given stat to the statsd server.
-
#initialize(host = nil, port = nil, namespace: nil, tags: nil, max_buffer_bytes: 8192, socket_path: nil, logger: nil) ⇒ Statsd
constructor
A new instance of Statsd.
- #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.
-
#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.
Constructor Details
#initialize(host = nil, port = nil, namespace: nil, tags: nil, max_buffer_bytes: 8192, socket_path: nil, logger: nil) ⇒ Statsd
Returns a new instance of Statsd.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/datadog/statsd.rb', line 208 def initialize( host = nil, port = nil, namespace: nil, tags: nil, max_buffer_bytes: 8192, socket_path: nil, logger: nil ) @connection = Connection.new(host, port, socket_path, logger) @logger = logger @namespace = namespace @prefix = @namespace ? "#{@namespace}.".freeze : nil raise ArgumentError, 'tags must be a Array<String>' unless .nil? or .is_a? Array @tags = ( || []).compact.map! {|tag| escape_tag_content(tag)} # append the entity id to tags if DD_ENTITY_ID env var is not nil @tags << 'dd.internal.entity_id:' + escape_tag_content(ENV.fetch('DD_ENTITY_ID', nil)) unless ENV.fetch('DD_ENTITY_ID', nil).nil? @batch = Batch.new @connection, max_buffer_bytes end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Buffer containing the statsd message before they are sent in batch
193 194 195 |
# File 'lib/datadog/statsd.rb', line 193 def buffer @buffer end |
#connection ⇒ Object (readonly)
Connection
199 200 201 |
# File 'lib/datadog/statsd.rb', line 199 def connection @connection end |
#max_buffer_bytes ⇒ Object (readonly)
Maximum buffer size in bytes before it is flushed
196 197 198 |
# File 'lib/datadog/statsd.rb', line 196 def max_buffer_bytes @max_buffer_bytes end |
#namespace ⇒ Object (readonly)
A namespace to prepend to all statsd calls. Defaults to no namespace.
187 188 189 |
# File 'lib/datadog/statsd.rb', line 187 def namespace @namespace end |
#tags ⇒ Object (readonly)
Global tags to be added to every statsd call. Defaults to no tags.
190 191 192 |
# File 'lib/datadog/statsd.rb', line 190 def @tags end |
Class Method Details
.open(*args) ⇒ 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
234 235 236 237 238 239 |
# File 'lib/datadog/statsd.rb', line 234 def self.open(*args) instance = new(*args) yield instance ensure instance.close end |
Instance Method Details
#batch ⇒ Object
Send several metrics in the same UDP Packet They will be buffered and flushed when the block finishes
424 425 426 |
# File 'lib/datadog/statsd.rb', line 424 def batch @batch.open { yield self } end |
#close ⇒ Object
Close the underlying socket
429 430 431 |
# File 'lib/datadog/statsd.rb', line 429 def close @connection.close end |
#count(stat, count, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitrary count for the given stat to the statsd server.
276 277 278 279 |
# File 'lib/datadog/statsd.rb', line 276 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.
263 264 265 266 267 |
# File 'lib/datadog/statsd.rb', line 263 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. Note: Distributions are a beta feature of Datadog and not generally available. Distributions must be specifically enabled for your organization.
324 325 326 |
# File 'lib/datadog/statsd.rb', line 324 def distribution(stat, value, opts=EMPTY_OPTIONS) send_stats stat, value, DISTRIBUTION_TYPE, 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.
412 413 414 |
# File 'lib/datadog/statsd.rb', line 412 def event(title, text, opts=EMPTY_OPTIONS) send_stat format_event(title, text, opts) end |
#gauge(stat, value, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitary 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.
294 295 296 297 |
# File 'lib/datadog/statsd.rb', line 294 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.
308 309 310 |
# File 'lib/datadog/statsd.rb', line 308 def histogram(stat, value, opts=EMPTY_OPTIONS) send_stats stat, value, HISTOGRAM_TYPE, opts end |
#increment(stat, opts = EMPTY_OPTIONS) ⇒ Object
Sends an increment (count = 1) for the given stat to the statsd server.
249 250 251 252 253 |
# File 'lib/datadog/statsd.rb', line 249 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 |
#service_check(name, status, opts = EMPTY_OPTIONS) ⇒ Object
390 391 392 |
# File 'lib/datadog/statsd.rb', line 390 def service_check(name, status, opts=EMPTY_OPTIONS) send_stat format_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.
374 375 376 377 |
# File 'lib/datadog/statsd.rb', line 374 def set(stat, value, opts=EMPTY_OPTIONS) opts = {:sample_rate => opts} if opts.is_a? Numeric send_stats stat, value, SET_TYPE, opts 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
356 357 358 359 360 361 362 363 |
# File 'lib/datadog/statsd.rb', line 356 def time(stat, opts=EMPTY_OPTIONS) opts = {:sample_rate => opts} if opts.is_a? Numeric start = (PROCESS_TIME_SUPPORTED ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : Time.now.to_f) return yield ensure finished = (PROCESS_TIME_SUPPORTED ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : Time.now.to_f) timing(stat, ((finished - 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.
338 339 340 341 |
# File 'lib/datadog/statsd.rb', line 338 def timing(stat, ms, opts=EMPTY_OPTIONS) opts = {:sample_rate => opts} if opts.is_a? Numeric send_stats stat, ms, TIMING_TYPE, opts end |