Class: Metricsd::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/metricsd/client.rb

Overview

Client class implements MetricsD protocol, allowing to send various metrics to the MetricsD server for collecting, analyzing, and graphing them.

Class allows to record count and timing stats to the database:

# Record success hit
Metricsd::Client.record_success("api.docs.upload")
# Record failure hit
Metricsd::Client.record_failure("api.docs.upload")
# Record timing info
Metricsd::Client.record_time("api.docs.upload", 0.14)
# Record complete success hit info (count + timing)
Metricsd::Client.record_hit("api.docs.upload", true, 0.14)
# Record an integer value
Metricsd::Client.record_value("user.password.size", 15)
Metricsd::Client.record_value("user.age", 26)

To send several metrics in a single network packet, you can use record_values:

# Send all database pool stats
Metricsd::Client.record_values({
  'db.pool.reserved'  => db_stats[:reserved],
  'db.pool.available' => db_stats[:available],
  'db.pool.pending'   => db_stats[:pending],
}, :group => 'doc_timestamp')

You can specify message source using :source => 'src' option. In this case you will be able to see summary graphs and graphs per source:

# Generate graphs for all tables, and each single table.
Metricsd::Client.record_success("hbase.reads", :source => @hbase_table)

By default only summary statistics is calculated. You can enable per-host graphs by specifying the appropriate source:

# Generate summary graph for all hosts, and graphs for each single host.
Metricsd::Client.record_success("hbase.reads", :source => Metricsd::Client.source)
# ... or you can pass an empty string with the same effect.
Metricsd::Client.record_success("hbase.reads", :source => '')

You can group your metrics using :group option. In this case metrics will be displayed together on the summary page.

# Group metrics using :group option.
Metricsd::Client.record_success("reads", :source => @hbase_table, :group => 'hbase')
# Group metrics using special syntax "group.metric".
Metricsd::Client.record_success("hbase.reads", :source => @hbase_table)

Constant Summary collapse

@@lock =
Monitor.new

Class Method Summary collapse

Class Method Details

.record_failure(metric, opts = {}) ⇒ Object Also known as: failure

Record failed boolean event.

It creates a single metric:

  • your.metric.status with numbers of failed and succeded events

Parameters:

  • metric (String)

    is the metric name (like app.docs.upload)

  • opts (Hash) (defaults to: {})

    options.

Options Hash (opts):

  • :group (String)

    metrics group.

  • :source (String)

    metric source.



100
101
102
# File 'lib/metricsd/client.rb', line 100

def record_failure(metric, opts = {})
  record_status(metric, false, opts)
end

.record_hit(metric, is_success, time, opts = {}) ⇒ Object Also known as: hit

Record complete hit info. Time should be a floating point number of seconds.

It creates two metrics:

  • your.metric.status with counts of failed and succeded events

  • your.metric.time with time statistics

Parameters:

  • metric (String)

    is the metric name (like app.docs.upload)

  • is_success (Boolean)

    indicating whether request was successful.

  • time (Float)

    floating point number of seconds.

  • opts (Hash) (defaults to: {})

    options.

Options Hash (opts):

  • :group (String)

    metrics group.

  • :source (String)

    metric source.



66
67
68
69
70
71
72
# File 'lib/metricsd/client.rb', line 66

def record_hit(metric, is_success, time, opts = {})
  record_internal({
      "#{metric}.status" => is_success ? 1 : -1,
      "#{metric}.time"   => (time * 1000).round
    }, opts
  )
end

.record_status(metric, is_success, opts = {}) ⇒ Object Also known as: status

Record status event (success or failure).

It creates a single metric:

  • your.metric.status with numbers of failed and succeded events

Parameters:

  • metric (String)

    is the metric name (like app.docs.upload)

  • is_success (Boolean)

    indicating whether event is successful.

  • opts (Hash) (defaults to: {})

    options.

Options Hash (opts):

  • :group (String)

    metrics group.

  • :source (String)

    metric source.



116
117
118
# File 'lib/metricsd/client.rb', line 116

def record_status(metric, is_success, opts = {})
  record_internal({"#{metric}.status" => is_success ? 1 : -1}, opts)
end

.record_success(metric, opts = {}) ⇒ Object Also known as: success

Record succeded boolean event.

It creates a single metric:

  • your.metric.status with numbers of failed and succeded events

Parameters:

  • metric (String)

    is the metric name (like app.docs.upload)

  • opts (Hash) (defaults to: {})

    options.

Options Hash (opts):

  • :group (String)

    metrics group.

  • :source (String)

    metric source.



85
86
87
# File 'lib/metricsd/client.rb', line 85

def record_success(metric, opts = {})
  record_status(metric, true, opts)
end

.record_time(metric, time = nil, opts = {}, &block) ⇒ Object Also known as: time

Record timing info. Time should be a floating point number of seconds.

It creates a single metric:

  • your.metric.time with time statistics

Parameters:

  • metric (String)

    is the metric name (like app.docs.upload)

  • time (Float) (defaults to: nil)

    floating point number of seconds.

  • opts (Hash) (defaults to: {})

    options.

Options Hash (opts):

  • :group (String)

    metrics group.

  • :source (String)

    metric source.



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/metricsd/client.rb', line 133

def record_time(metric, time = nil, opts = {}, &block)
  opts, time = time, nil if Hash === time
  result = nil
  if time.nil?
    raise ArgumentError, "You should pass a block if time is not given" unless block_given?
    time = Benchmark.measure do
      result = block.call
    end.real
  end
  record_internal({"#{metric}.time" => (time * 1000).round}, opts)
  result
end

.record_value(metric, value, opts = {}) ⇒ Object Also known as: record, value

Record an integer value.

It creates a single metric:

  • your.metric with values statistics

Parameters:

  • metric (String)

    is the metric name (like app.docs.upload)

  • value (Integer)

    metric value.

  • opts (Hash) (defaults to: {})

    options.

Options Hash (opts):

  • :group (String)

    metrics group.

  • :source (String)

    metric source.



158
159
160
# File 'lib/metricsd/client.rb', line 158

def record_value(metric, value, opts = {})
  record_internal({metric => value.round}, opts)
end

.record_values(metrics, opts = {}) ⇒ Object Also known as: values

Record multiple integer values.

It creates a metric for each entry in metrics Hash:

  • your.metric with values statistics

Examples:

Metricsd::Client.record_values(
  'db.pool.reserved'  => db_stats[:reserved],
  'db.pool.available' => db_stats[:available],
  'db.pool.pending'   => db_stats[:pending],
)

Parameters:

  • metrics (Hash)

    a Hash that maps metrics names to their values.

  • opts (Hash) (defaults to: {})

    options.

Options Hash (opts):

  • :group (String)

    metrics group.

  • :source (String)

    metric source.



181
182
183
# File 'lib/metricsd/client.rb', line 181

def record_values(metrics, opts = {})
  record_internal(metrics, opts)
end

.reset_connection!Object

Reset and re-establish connection.



187
188
189
# File 'lib/metricsd/client.rb', line 187

def reset_connection!
  @@socket = nil
end