Class: Graphite

Inherits:
Object
  • Object
show all
Defined in:
lib/graphite/socket.rb,
lib/simple-graphite.rb

Defined Under Namespace

Classes: Socket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @host = options[:host]
  @type = options[:type] ||= :tcp
  @port = options[:port] ||= @type.eql?(:tcp) ? 2003 : 8125
  @time = Time.now.to_i
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/simple-graphite.rb', line 6

def host
  @host
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/simple-graphite.rb', line 6

def port
  @port
end

#timeObject

Returns the value of attribute time.



6
7
8
# File 'lib/simple-graphite.rb', line 6

def time
  @time
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/simple-graphite.rb', line 6

def type
  @type
end

Class Method Details

.time_nowObject



37
38
39
# File 'lib/simple-graphite.rb', line 37

def self.time_now
  @time = Time.now.to_i
end

Instance Method Details

#hostnameObject



33
34
35
# File 'lib/simple-graphite.rb', line 33

def hostname
  ::Socket.gethostname
end

#push_to_graphiteObject



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_nowObject



41
42
43
# File 'lib/simple-graphite.rb', line 41

def time_now
  self.class.time_now
end