Class: Librato::LogReporter

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/librato/logreporter.rb,
lib/librato/logreporter/group.rb,
lib/librato/logreporter/version.rb,
lib/librato/logreporter/configuration.rb

Overview

Provides a common interface to reporting metrics with methods like #increment, #measure, #timing, #group - all written to your preferred IO stream.

Defined Under Namespace

Modules: Configuration Classes: Group

Constant Summary collapse

VERSION =
'0.1.0'

Instance Method Summary collapse

Methods included from Configuration

#log, #log=, #prefix, #prefix=, #source, #source=

Instance Method Details

#group(prefix) {|Group.new(collector: self, prefix: prefix)| ... } ⇒ Object

Group a set of metrics by common prefix

Examples:

# write a 'performance.hits' increment and
# a 'performance.response.time' measure
group(:performance) do |perf|
  perf.increment :hits
  perf.measure 'response.time', response_time
end

Yields:



30
31
32
# File 'lib/librato/logreporter.rb', line 30

def group(prefix)
  yield Group.new(collector: self, prefix: prefix)
end

#increment(counter, options = {}) ⇒ Object

Increment a given metric

Examples:

Increment metric ‘foo’ by 1

increment :foo

Increment metric ‘bar’ by 2

increment :bar, :by => 2

Increment metric ‘foo’ by 1 with a custom source

increment :foo, :source => user.id


45
46
47
48
# File 'lib/librato/logreporter.rb', line 45

def increment(counter, options={})
  by = options[:by] || 1
  log_write(counter => by, :source => options[:source])
end

#measure(*args, &block) ⇒ Object Also known as: timing

Examples:

Simple measurement

measure 'sources_returned', sources.length

Simple timing in milliseconds

timing 'myservice.lookup', 2.31

Block-based timing

timing 'db.query' do
  do_my_query
end

Custom source

measure 'user.all_orders', user.order_count, :source => user.id


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/librato/logreporter.rb', line 64

def measure(*args, &block)
  options = {}
  event = args[0].to_s
  returned = nil

  # handle block or specified argument
  if block_given?
    start = Time.now
    returned = yield
    value = ((Time.now - start) * 1000.0).to_i
  elsif args[1]
    value = args[1]
  else
    raise "no value provided"
  end

  # detect options hash if present
  if args.length > 1 and args[-1].respond_to?(:each)
    options = args[-1]
  end

  log_write(event => value, :source => options[:source])
  returned
end