Class: Unleash::MetricsReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/metrics_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetricsReporter

Returns a new instance of MetricsReporter.



12
13
14
# File 'lib/unleash/metrics_reporter.rb', line 12

def initialize
  self.last_time = Time.now
end

Instance Attribute Details

#last_timeObject

Returns the value of attribute last_time.



10
11
12
# File 'lib/unleash/metrics_reporter.rb', line 10

def last_time
  @last_time
end

Instance Method Details

#generate_reportObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/unleash/metrics_reporter.rb', line 16

def generate_report
  now = Time.now
  start, stop, self.last_time = self.last_time, now, now
  report = {
    'appName': Unleash.configuration.app_name,
    'instanceId': Unleash.configuration.instance_id,
    'bucket': {
      'start': start.iso8601(Unleash::TIME_RESOLUTION),
      'stop': stop.iso8601(Unleash::TIME_RESOLUTION),
      'toggles': Unleash.toggle_metrics.features
    }
  }

  Unleash.toggle_metrics.reset
  return report
end

#sendObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/unleash/metrics_reporter.rb', line 33

def send
  Unleash.logger.debug "send() Report"

  generated_report = self.generate_report()

  uri = URI(Unleash.configuration.client_metrics_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  http.open_timeout = Unleash.configuration.timeout # in seconds
  http.read_timeout = Unleash.configuration.timeout # in seconds

  headers = (Unleash.configuration.get_http_headers || {}).dup
  headers['Content-Type'] = 'application/json'
  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = generated_report.to_json

  Unleash.logger.debug "Report to send: #{request.body}"

  response = http.request(request)

  if ['200','202'].include? response.code
    Unleash.logger.debug "Report sent to unleash server sucessfully. Server responded with http code #{response.code}"
  else
    Unleash.logger.error "Error when sending report to unleash server. Server responded with http code #{response.code}."
  end

end