Class: Coverband::Adapters::Service

Inherits:
Base
  • Object
show all
Defined in:
lib/coverband-service-client.rb

Overview

Take Coverband data and store a merged coverage set to the Coverband service

NOTES:

  • uses net/http to avoid any dependencies

  • currently JSON, but likely better to move to something faster

Direct Known Subclasses

PersistentService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coverband_url, opts = {}) ⇒ Service

Returns a new instance of Service.



45
46
47
48
49
50
51
52
53
54
# File 'lib/coverband-service-client.rb', line 45

def initialize(coverband_url, opts = {})
  super()
  @coverband_url = coverband_url
  @process_type = opts.fetch(:process_type) { $PROGRAM_NAME&.split('/')&.last || COVERBAND_PROCESS_TYPE }
  @hostname = opts.fetch(:hostname) { ENV["DYNO"] || Socket.gethostname.force_encoding('utf-8').encode }
  @hostname = @hostname.gsub("'",'').gsub("",'')
  @runtime_env = opts.fetch(:runtime_env) { COVERBAND_ENV }
  @failed_coverage_reports = []
  initialize_stats
end

Instance Attribute Details

#coverband_urlObject (readonly)

Returns the value of attribute coverband_url.



43
44
45
# File 'lib/coverband-service-client.rb', line 43

def coverband_url
  @coverband_url
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



43
44
45
# File 'lib/coverband-service-client.rb', line 43

def hostname
  @hostname
end

#pidObject (readonly)

Returns the value of attribute pid.



43
44
45
# File 'lib/coverband-service-client.rb', line 43

def pid
  @pid
end

#process_typeObject (readonly)

Returns the value of attribute process_type.



43
44
45
# File 'lib/coverband-service-client.rb', line 43

def process_type
  @process_type
end

#runtime_envObject (readonly)

Returns the value of attribute runtime_env.



43
44
45
# File 'lib/coverband-service-client.rb', line 43

def runtime_env
  @runtime_env
end

#statsObject (readonly)

Returns the value of attribute stats.



43
44
45
# File 'lib/coverband-service-client.rb', line 43

def stats
  @stats
end

Instance Method Details

#api_keyObject



92
93
94
# File 'lib/coverband-service-client.rb', line 92

def api_key
  ENV['COVERBAND_API_KEY'] || Coverband.configuration.api_key
end

#clear!Object



79
80
81
# File 'lib/coverband-service-client.rb', line 79

def clear!
  # TBD
end

#clear_file!(filename) ⇒ Object



83
84
85
# File 'lib/coverband-service-client.rb', line 83

def clear_file!(filename)
  # TBD
end

#coverage(local_type = nil, opts = {}) ⇒ Object

Fetch coverband coverage via the API



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/coverband-service-client.rb', line 99

def coverage(local_type = nil, opts = {})
  local_type ||= opts.key?(:override_type) ? opts[:override_type] : type
  env_filter = opts.key?(:env_filter) ? opts[:env_filter] : 'production'
  uri = URI("#{coverband_url}/api/coverage?type=#{local_type}&env_filter=#{env_filter}",)
  req = Net::HTTP::Get.new(uri, 'Content-Type' => 'application/json', 'Coverband-Token' => api_key)
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(req)
  end
  coverage_data = JSON.parse(res.body)
  coverage_data
rescue StandardError => e
  logger&.error "Coverband: Error while retrieving coverage #{e}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
end

#initialize_statsObject



56
57
58
59
60
61
62
# File 'lib/coverband-service-client.rb', line 56

def initialize_stats
  return unless ENV['COVERBAND_STATS_KEY']
  return unless defined?(Dogapi::Client)

  @stats = Dogapi::Client.new(ENV['COVERBAND_STATS_KEY'])
  @app_name = defined?(Rails) ? Rails.application.class.module_parent.to_s : "unknown"
end

#loggerObject



75
76
77
# File 'lib/coverband-service-client.rb', line 75

def logger
  Coverband.configuration.logger
end

#raw_storeObject



145
146
147
# File 'lib/coverband-service-client.rb', line 145

def raw_store
  self
end

#report_timing(timing) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/coverband-service-client.rb', line 64

def report_timing(timing)
  return unless @stats

  @stats.emit_point(
    'coverband.save.time',
    timing,
    host: hostname,
    device: "coverband_#{self.class.name.split("::").last}",
    options: {tags: [runtime_env]})
end

#save_report(report) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/coverband-service-client.rb', line 113

def save_report(report)
  return if report.empty?

  # We set here vs initialize to avoid setting on the primary process vs child processes
  @pid ||= ::Process.pid

  # TODO: do we need dup
  # TODO: remove upstream timestamps, server will track first_seen
  Thread.new do
    data = expand_report(report.dup)
    full_package = {
      collection_type: 'coverage_delta',
      collection_data: {
        tags: {
          process_type: process_type,
          app_loading: type == Coverband::EAGER_TYPE,
          runtime_env: runtime_env,
          pid: pid,
          hostname: hostname,
        },
        file_coverage: data
      }
    }

    starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) if @stats
    save_coverage(full_package)
    ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) if @stats
    report_timing((ending - starting)) if @stats
    retry_failed_reports
  end&.join
end

#sizeObject

TODO: we should support nil to mean not supported



88
89
90
# File 'lib/coverband-service-client.rb', line 88

def size
  0
end