Class: Daikon::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/daikon/client.rb

Constant Summary collapse

EXCEPTIONS =
[Timeout::Error,
Errno::EINVAL,
Errno::ECONNRESET,
EOFError,
JSON::ParserError,
Excon::Errors::SocketError]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/daikon/client.rb', line 10

def config
  @config
end

#httpObject

Returns the value of attribute http.



10
11
12
# File 'lib/daikon/client.rb', line 10

def http
  @http
end

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/daikon/client.rb', line 10

def logger
  @logger
end

#monitorObject

Returns the value of attribute monitor.



10
11
12
# File 'lib/daikon/client.rb', line 10

def monitor
  @monitor
end

#redisObject

Returns the value of attribute redis.



10
11
12
# File 'lib/daikon/client.rb', line 10

def redis
  @redis
end

Instance Method Details

#connectObject



22
23
24
# File 'lib/daikon/client.rb', line 22

def connect
  Redis.connect(:url => config.redis_url)
end

#exception(error) ⇒ Object



34
35
36
37
38
39
# File 'lib/daikon/client.rb', line 34

def exception(error)
  log error.to_s
  error.backtrace.each do |line|
    log line
  end
end

#log(message) ⇒ Object



30
31
32
# File 'lib/daikon/client.rb', line 30

def log(message)
  logger.info message if logger
end

#push(method, path, body) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/daikon/client.rb', line 51

def push(method, path, body)
  json = body.to_json
  request(method, path,
               :body    => json,
               :headers => {"Content-Length" => json.size.to_s,
                            "Content-Type"   => "application/json"})
end

#report_infoObject



70
71
72
73
74
75
# File 'lib/daikon/client.rb', line 70

def report_info
   push :post, "/api/v1/infos.json", redis.info
rescue *EXCEPTIONS => ex
  log "Error when reporting info"
  exception(ex)
end

#request(method, path, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/daikon/client.rb', line 41

def request(method, path, options = {})
  options[:method]  = method.to_s.upcase
  options[:path]    = path
  options[:headers] ||= {}
  options[:headers]['Authorization'] = config.api_key

  http.reset
  http.request(options)
end

#rotate_monitor(start, stop) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/daikon/client.rb', line 59

def rotate_monitor(start, stop)
  payload = monitor.rotate.merge({
    "created_at" => stop
  })

  push :post, "/api/v1/summaries.json", payload
rescue *EXCEPTIONS => ex
  log "Error when rotating monitor"
  exception(ex)
end

#setup(config, logger = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/daikon/client.rb', line 12

def setup(config, logger = nil)
  self.config  = config
  self.logger  = logger
  self.redis   = connect
  self.monitor = Monitor.new(connect, logger)
  self.http    = Excon.new(config.server_prefix)

  log "Started Daikon v#{VERSION}"
end

#start_monitorObject



26
27
28
# File 'lib/daikon/client.rb', line 26

def start_monitor
  monitor.start
end