Class: Sq::Dbsync::Loggers::Graphite

Inherits:
Abstract
  • Object
show all
Defined in:
lib/sq/dbsync/loggers.rb

Overview

Logs metric run time to graphite.

Instance Method Summary collapse

Methods inherited from Abstract

#log

Constructor Details

#initialize(opts) ⇒ Graphite

Returns a new instance of Graphite.



92
93
94
# File 'lib/sq/dbsync/loggers.rb', line 92

def initialize(opts)
  @opts   = opts
end

Instance Method Details

#measure(label, &block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/sq/dbsync/loggers.rb', line 96

def measure(label, &block)
  start_time = Time.now.utc
  block.call
ensure
  end_time = Time.now.utc
  record_metric(end_time.to_i, label, end_time - start_time)
end

#record_metric(timestamp, name, value) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/sq/dbsync/loggers.rb', line 104

def record_metric(timestamp, name, value)
  msg = "#{@opts.fetch(:prefix, 'dbsync')}.#{name} #{value} #{timestamp}\n"

  s = TCPSocket.new(@opts[:host], @opts.fetch(:port, 2003))
  s.send msg, 0
  s.close
end