Class: Datadog::Statsd

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/statsd.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/message_buffer.rb,
lib/datadog/statsd/udp_connection.rb,
lib/datadog/statsd/uds_connection.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/service_check_serializer.rb

Defined Under Namespace

Modules: Serialization Classes: Connection, Error, Forwarder, MessageBuffer, Sender, Telemetry, 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
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.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, logger: nil, auto_close: false, telemetry_enable: true, telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL) ⇒ Statsd

Returns a new instance of Statsd.

Parameters:

  • host (String) (defaults to: nil)

    your statsd host

  • port (Integer) (defaults to: nil)

    your statsd port

  • [String] (Hash)

    a customizable set of options

  • [Array<String>|Hash] (Hash)

    a customizable set of options

  • [Logger] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options

  • [Float] (Hash)

    a customizable set of options


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
# File 'lib/datadog/statsd.rb', line 73

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,

  logger: nil,
  auto_close: false,

  telemetry_enable: true,
  telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL
)
  unless tags.nil? || tags.is_a?(Array) || tags.is_a?(Hash)
    raise ArgumentError, 'tags must be an array of string tags or a Hash'
  end

  @namespace = namespace
  @prefix = @namespace ? "#{@namespace}.".freeze : nil
  @serializer = Serialization::Serializer.new(prefix: @prefix, global_tags: tags)
  @sample_rate = sample_rate

  @forwarder = Forwarder.new(
    host: host,
    port: port,
    socket_path: socket_path,

    global_tags: tags,
    logger: logger,

    buffer_max_payload_size: buffer_max_payload_size,
    buffer_max_pool_size: buffer_max_pool_size,
    buffer_overflowing_stategy: buffer_overflowing_stategy,

    telemetry_flush_interval: telemetry_enable ? telemetry_flush_interval : nil,
  )

  self.class.subscribe_for_close_on_exit(self) if auto_close
end

Instance Attribute Details

#namespaceObject (readonly)

A namespace to prepend to all statsd calls. Defaults to no namespace.


54
55
56
# File 'lib/datadog/statsd.rb', line 54

def namespace
  @namespace
end

#sample_rateObject (readonly)

Default sample rate


62
63
64
# File 'lib/datadog/statsd.rb', line 62

def sample_rate
  @sample_rate
end

Class Method Details

.close_instancesObject


396
397
398
# File 'lib/datadog/statsd.rb', line 396

def close_instances
  instances.each(&:close)
end

.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


121
122
123
124
125
126
127
# File 'lib/datadog/statsd.rb', line 121

def self.open(*args)
  instance = new(*args)

  yield instance
ensure
  instance.close
end

.subscribe_for_close_on_exit(instance) ⇒ Object


392
393
394
# File 'lib/datadog/statsd.rb', line 392

def subscribe_for_close_on_exit(instance)
  instances << 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.

Examples:

Send several metrics in one packet:

$statsd.batch do |s|
   s.gauge('users.online',156)
   s.increment('page.views')
 end

Yields:

  • (_self)

Yield Parameters:


320
321
322
323
# File 'lib/datadog/statsd.rb', line 320

def batch
  yield self
  flush(sync: true)
end

#close(flush: true) ⇒ Object

Close the underlying socket

Parameters:

  • flush (Boolean, true) (defaults to: true)

    Should we flush the metrics before closing


328
329
330
331
# File 'lib/datadog/statsd.rb', line 328

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.

Parameters:

  • stat (String)

    stat name

  • count (Integer)

    count

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags


164
165
166
167
# File 'lib/datadog/statsd.rb', line 164

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.

Parameters:

  • stat (String)

    stat name

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags

  • :by (Numeric)

    decrement value, default 1

See Also:


151
152
153
154
155
# File 'lib/datadog/statsd.rb', line 151

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.

Examples:

Report the current user count:

$statsd.distribution('user.count', User.count)

Parameters:

  • stat (String)

    stat name.

  • value (Numeric)

    distribution value.

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags


209
210
211
# File 'lib/datadog/statsd.rb', line 209

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.

Examples:

Report an awful event:

$statsd.event('Something terrible happened', 'The end is near if we do nothing', :alert_type=>'warning', :tags=>['end_of_times','urgent'])

Parameters:

  • title (String)

    Event title

  • text (String)

    Event text. Supports newlines (\n)

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the additional data about the event

Options Hash (opts):

  • :date_happened (Integer, String, nil) — default: nil

    Assign a timestamp to the event. Default is now when none

  • :hostname (String, nil) — default: nil

    Assign a hostname to the event.

  • :aggregation_key (String, nil) — default: nil

    Assign an aggregation key to the event, to group it with some others

  • :priority (String, nil) — default: 'normal'

    Can be “normal” or “low”

  • :source_type_name (String, nil) — default: nil

    Assign a source type to the event

  • :alert_type (String, nil) — default: 'info'

    Can be “error”, “warning”, “info” or “success”.

  • :truncate_if_too_long (Boolean, false) — default: false

    Truncate the event if it is too long

  • :tags (Array<String>)

    tags to be added to every metric


299
300
301
302
303
# File 'lib/datadog/statsd.rb', line 299

def event(title, text, opts = EMPTY_OPTIONS)
  telemetry.sent(events: 1) if telemetry

  forwarder.send_message(serializer.to_event(title, text, opts))
end

#flush(flush_telemetry: false, sync: false) ⇒ Object

Flush the buffer into the connection


338
339
340
# File 'lib/datadog/statsd.rb', line 338

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 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.

Examples:

Report the current user count:

$statsd.gauge('user.count', User.count)

Parameters:

  • stat (String)

    stat name.

  • value (Numeric)

    gauge value.

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags


182
183
184
185
# File 'lib/datadog/statsd.rb', line 182

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.

Examples:

Report the current user count:

$statsd.histogram('user.count', User.count)

Parameters:

  • stat (String)

    stat name.

  • value (Numeric)

    histogram value.

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags


196
197
198
# File 'lib/datadog/statsd.rb', line 196

def histogram(stat, value, opts = EMPTY_OPTIONS)
  send_stats(stat, value, HISTOGRAM_TYPE, opts)
end

#hostObject


346
347
348
# File 'lib/datadog/statsd.rb', line 346

def host
  forwarder.host
end

#increment(stat, opts = EMPTY_OPTIONS) ⇒ Object

Sends an increment (count = 1) for the given stat to the statsd server.

Parameters:

  • stat (String)

    stat name

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags

  • :by (Numeric)

    increment value, default 1

See Also:


137
138
139
140
141
# File 'lib/datadog/statsd.rb', line 137

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

#portObject


350
351
352
# File 'lib/datadog/statsd.rb', line 350

def port
  forwarder.port
end

#service_check(name, status, opts = EMPTY_OPTIONS) ⇒ Object

Examples:

Report a critical service check status

$statsd.service_check('my.service.check', Statsd::CRITICAL, :tags=>['urgent'])

274
275
276
277
278
# File 'lib/datadog/statsd.rb', line 274

def service_check(name, status, opts = EMPTY_OPTIONS)
  telemetry.sent(service_checks: 1) if telemetry

  forwarder.send_message(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.

Examples:

Record a unique visitory by id:

$statsd.set('visitors.uniques', User.id)

Parameters:

  • stat (String)

    stat name.

  • value (Numeric)

    set value.

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags


258
259
260
261
# File 'lib/datadog/statsd.rb', line 258

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_pathObject


354
355
356
# File 'lib/datadog/statsd.rb', line 354

def socket_path
  forwarder.socket_path
end

#sync_with_outbound_ioObject


333
334
335
# File 'lib/datadog/statsd.rb', line 333

def sync_with_outbound_io
  forwarder.sync_with_outbound_io
end

#tagsObject

Global tags to be added to every statsd call. Defaults to no tags.


57
58
59
# File 'lib/datadog/statsd.rb', line 57

def tags
  serializer.global_tags
end

#telemetryObject


342
343
344
# File 'lib/datadog/statsd.rb', line 342

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

Examples:

Report the time (in ms) taken to activate an account

$statsd.time('account.activate') { @account.activate! }

Parameters:

  • stat (String)

    stat name

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags

Yields:

  • The operation to be timed

See Also:


241
242
243
244
245
246
247
# File 'lib/datadog/statsd.rb', line 241

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.

Parameters:

  • stat (String)

    stat name

  • ms (Integer)

    timing in milliseconds

  • opts (Hash) (defaults to: EMPTY_OPTIONS)

    the options to create the metric with

Options Hash (opts):

  • :sample_rate (Numeric)

    sample rate, 1 for always

  • :tags (Array<String>)

    An array of tags


223
224
225
226
# File 'lib/datadog/statsd.rb', line 223

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_typeObject


358
359
360
# File 'lib/datadog/statsd.rb', line 358

def transport_type
  forwarder.transport_type
end