Class: Instrumentation::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/instrumentation/report.rb

Overview

Reads data from system and process information and writes it to websocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ Report

Returns a new instance of Report.



8
9
10
11
12
13
14
# File 'lib/instrumentation/report.rb', line 8

def initialize(pid)
  @pid = pid
  @sleep = 1
  @memory = BoundedArray.new(300)
  @loadavg = BoundedArray.new(300)
  @socket = nil
end

Instance Attribute Details

#loadavgObject (readonly)

Returns the value of attribute loadavg.



5
6
7
# File 'lib/instrumentation/report.rb', line 5

def loadavg
  @loadavg
end

#memoryObject (readonly)

Returns the value of attribute memory.



5
6
7
# File 'lib/instrumentation/report.rb', line 5

def memory
  @memory
end

#socketObject

Returns the value of attribute socket.



6
7
8
# File 'lib/instrumentation/report.rb', line 6

def socket
  @socket
end

Instance Method Details

#event(name, data) ⇒ Object



41
42
43
# File 'lib/instrumentation/report.rb', line 41

def event(name, data)
  { data_type: name, data: data }.to_json
end

#joinObject



53
54
55
# File 'lib/instrumentation/report.rb', line 53

def join
  @thread.join
end

#read_dataObject



26
27
28
29
30
31
32
# File 'lib/instrumentation/report.rb', line 26

def read_data
  now = Time.now.strftime('%FT%T')
  @memory = @memory << [now, read_memory]
  @loadavg = @loadavg << [now, read_loadavg]
rescue => error
  puts "Error when reading data: #{error.inspect}"
end

#read_loadavgObject



49
50
51
# File 'lib/instrumentation/report.rb', line 49

def read_loadavg
  LoadAverage.new.read[:one]
end

#read_memoryObject



45
46
47
# File 'lib/instrumentation/report.rb', line 45

def read_memory
  Memory.new(@pid).read
end

#send_dataObject



34
35
36
37
38
39
# File 'lib/instrumentation/report.rb', line 34

def send_data
  socket.send_data(event(:memory, @memory.items))
  socket.send_data(event(:loadavg, @loadavg.items))
rescue => error
  puts "Error when sending data: #{error.inspect}"
end

#shutdownObject



57
58
59
# File 'lib/instrumentation/report.rb', line 57

def shutdown
  @thread.kill
end

#startObject



16
17
18
19
20
21
22
23
24
# File 'lib/instrumentation/report.rb', line 16

def start
  @thread = Thread.new do
    loop do
      read_data
      send_data if socket
      sleep @sleep
    end
  end
end