Class: Unleash::MetricsReporter

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

Constant Summary collapse

LONGEST_WITHOUT_A_REPORT =
600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetricsReporter

Returns a new instance of MetricsReporter.



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

def initialize
  self.last_time = Time.now
end

Instance Attribute Details

#last_timeObject

Returns the value of attribute last_time.



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

def last_time
  @last_time
end

Instance Method Details

#generate_reportObject



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

def generate_report
  now = Time.now

  start = self.last_time
  stop  = now
  self.last_time = 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

  report
end

#postObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unleash/metrics_reporter.rb', line 38

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

  if bucket_empty? && (Time.now - self.last_time < LONGEST_WITHOUT_A_REPORT) # and last time is less then 10 minutes...
    Unleash.logger.debug "Report not posted to server, as it would have been empty. (and has been empty for up to 10 min)"

    return
  end

  response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_uri, self.generate_report.to_json)

  if ['200', '202'].include? response.code
    Unleash.logger.debug "Report sent to unleash server successfully. 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