Class: Datadog::Statsd
- Inherits:
-
Object
- Object
- Datadog::Statsd
- Defined in:
- lib/datadog/statsd.rb,
lib/datadog/statsd/batch.rb,
lib/datadog/statsd/version.rb,
lib/datadog/statsd/telemetry.rb,
lib/datadog/statsd/connection.rb,
lib/datadog/statsd/serialization.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: Batch, Connection, Telemetry, UDPConnection, UDSConnection
Constant Summary collapse
- OK =
0
- WARNING =
1
- CRITICAL =
2
- UNKNOWN =
3
- DEFAULT_BUFFER_SIZE =
8 * 1_024
- 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 =
'4.8.3'
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.
-
#sample_rate ⇒ Object
readonly
Default sample rate.
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: DEFAULT_BUFFER_SIZE, socket_path: nil, logger: nil, sample_rate: nil, disable_telemetry: false, telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL) ⇒ 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.
-
#tags ⇒ Object
Global tags to be added to every statsd call.
-
#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: DEFAULT_BUFFER_SIZE, socket_path: nil, logger: nil, sample_rate: nil, disable_telemetry: false, telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL) ⇒ Statsd
Returns a new instance of Statsd.
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 |
# File 'lib/datadog/statsd.rb', line 74 def initialize( host = nil, port = nil, namespace: nil, tags: nil, max_buffer_bytes: DEFAULT_BUFFER_SIZE, socket_path: nil, logger: nil, sample_rate: nil, disable_telemetry: false, telemetry_flush_interval: DEFAULT_TELEMETRY_FLUSH_INTERVAL ) unless .nil? || .is_a?(Array) || .is_a?(Hash) raise ArgumentError, 'tags must be a Array<String> or a Hash' end @namespace = namespace @prefix = @namespace ? "#{@namespace}.".freeze : nil @serializer = Serialization::Serializer.new(prefix: @prefix, global_tags: ) transport_type = socket_path.nil? ? :udp : :uds @telemetry = Telemetry.new(disable_telemetry, telemetry_flush_interval, global_tags: , transport_type: transport_type ) @connection = case transport_type when :udp UDPConnection.new(host, port, logger, telemetry) when :uds UDSConnection.new(socket_path, logger, telemetry) end @logger = logger @sample_rate = sample_rate # we reduce max_buffer_bytes by a the rough estimate of the telemetry payload @batch = Batch.new(connection, (max_buffer_bytes - telemetry.estimate_max_size)) end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Buffer containing the statsd message before they are sent in batch
55 56 57 |
# File 'lib/datadog/statsd.rb', line 55 def buffer @buffer end |
#connection ⇒ Object (readonly)
Connection
64 65 66 |
# File 'lib/datadog/statsd.rb', line 64 def connection @connection end |
#max_buffer_bytes ⇒ Object (readonly)
Maximum buffer size in bytes before it is flushed
58 59 60 |
# File 'lib/datadog/statsd.rb', line 58 def max_buffer_bytes @max_buffer_bytes end |
#namespace ⇒ Object (readonly)
A namespace to prepend to all statsd calls. Defaults to no namespace.
47 48 49 |
# File 'lib/datadog/statsd.rb', line 47 def namespace @namespace end |
#sample_rate ⇒ Object (readonly)
Default sample rate
61 62 63 |
# File 'lib/datadog/statsd.rb', line 61 def sample_rate @sample_rate 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
119 120 121 122 123 124 125 |
# File 'lib/datadog/statsd.rb', line 119 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
311 312 313 314 315 |
# File 'lib/datadog/statsd.rb', line 311 def batch @batch.open do yield self end end |
#close ⇒ Object
Close the underlying socket
318 319 320 |
# File 'lib/datadog/statsd.rb', line 318 def close connection.close end |
#count(stat, count, opts = EMPTY_OPTIONS) ⇒ Object
Sends an arbitrary count for the given stat to the statsd server.
162 163 164 165 |
# File 'lib/datadog/statsd.rb', line 162 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.
149 150 151 152 153 |
# File 'lib/datadog/statsd.rb', line 149 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.
207 208 209 |
# File 'lib/datadog/statsd.rb', line 207 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.
297 298 299 300 301 |
# File 'lib/datadog/statsd.rb', line 297 def event(title, text, opts = EMPTY_OPTIONS) telemetry.sent(events: 1) send_stat(serializer.to_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.
180 181 182 183 |
# File 'lib/datadog/statsd.rb', line 180 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.
194 195 196 |
# File 'lib/datadog/statsd.rb', line 194 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.
135 136 137 138 139 |
# File 'lib/datadog/statsd.rb', line 135 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
272 273 274 275 276 |
# File 'lib/datadog/statsd.rb', line 272 def service_check(name, status, opts = EMPTY_OPTIONS) telemetry.sent(service_checks: 1) send_stat(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.
256 257 258 259 |
# File 'lib/datadog/statsd.rb', line 256 def set(stat, value, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) send_stats(stat, value, SET_TYPE, opts) end |
#tags ⇒ Object
Global tags to be added to every statsd call. Defaults to no tags.
50 51 52 |
# File 'lib/datadog/statsd.rb', line 50 def serializer. 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
239 240 241 242 243 244 245 |
# File 'lib/datadog/statsd.rb', line 239 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.
221 222 223 224 |
# File 'lib/datadog/statsd.rb', line 221 def timing(stat, ms, opts = EMPTY_OPTIONS) opts = { sample_rate: opts } if opts.is_a?(Numeric) send_stats(stat, ms, TIMING_TYPE, opts) end |