Class: Graphite
- Inherits:
-
Object
- Object
- Graphite
- Defined in:
- lib/graphite/socket.rb,
lib/simple-graphite.rb
Defined Under Namespace
Classes: Socket
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#time ⇒ Object
Returns the value of attribute time.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #hostname ⇒ Object
-
#initialize(options = {}) ⇒ Graphite
constructor
A new instance of Graphite.
- #push_to_graphite ⇒ Object
- #send_metrics(metrics_hash) ⇒ Object
- #time_now ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Graphite
Returns a new instance of Graphite.
8 9 10 11 12 13 |
# File 'lib/simple-graphite.rb', line 8 def initialize( = {}) @host = [:host] @type = [:type] ||= :tcp @port = [:port] ||= @type.eql?(:tcp) ? 2003 : 8125 @time = Time.now.to_i end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/simple-graphite.rb', line 6 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
6 7 8 |
# File 'lib/simple-graphite.rb', line 6 def port @port end |
#time ⇒ Object
Returns the value of attribute time.
6 7 8 |
# File 'lib/simple-graphite.rb', line 6 def time @time end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/simple-graphite.rb', line 6 def type @type end |
Class Method Details
.time_now ⇒ Object
37 38 39 |
# File 'lib/simple-graphite.rb', line 37 def self.time_now @time = Time.now.to_i end |
Instance Method Details
#hostname ⇒ Object
33 34 35 |
# File 'lib/simple-graphite.rb', line 33 def hostname ::Socket.gethostname end |
#push_to_graphite ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/simple-graphite.rb', line 15 def push_to_graphite raise "You need to provide a hostname" if @host.nil? begin socket = Graphite::Socket.new @host, @port, @type yield socket ensure socket.close end end |
#send_metrics(metrics_hash) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/simple-graphite.rb', line 25 def send_metrics(metrics_hash) current_time = time_now push_to_graphite do |graphite| graphite.puts((metrics_hash.map { |k,v| [k, v, current_time].join(' ') + "\n" }).join('')) end current_time end |
#time_now ⇒ Object
41 42 43 |
# File 'lib/simple-graphite.rb', line 41 def time_now self.class.time_now end |