File: metrics_reporter.rb

package info (click to toggle)
ruby-unleash 3.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: ruby: 1,098; makefile: 10; sh: 4
file content (48 lines) | stat: -rwxr-xr-x 1,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'unleash/configuration'
require 'unleash/metrics'
require 'net/http'
require 'json'
require 'time'

module Unleash
  class MetricsReporter
    attr_accessor :last_time

    def initialize
      self.last_time = Time.now
    end

    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

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

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

      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
  end
end