coverband-service-client 0.0.7 → 0.0.12.rc

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c91e07a6f8536e144c0cde767a20b91ae1aefcc2d5e1bcda5c9962c8d667de8f
4
- data.tar.gz: ffc9bc012bb68be47b474d4c5b436ffbcc1e2e675e9616545ffb357e274b513b
3
+ metadata.gz: b21c24a8ca01191ebb807640f09555b360ba6f0333b86d6e0c109387832bb35a
4
+ data.tar.gz: 7657f2a6942d2474d196f470c1fbaca8b069ae49619c483e8e0e2c1787b39410
5
5
  SHA512:
6
- metadata.gz: 9879add06ec81ced176e688e5bd5c40d9ae3848deb165437acc099cc3454fa33cf65a49cacc5df1433b496b86c12c169db975b8776969e981ff12388b346b102
7
- data.tar.gz: 441d88a85a8e9d9afa7538c47a14fe028f58d817d65d642b22fdde963dd6f38ce123edc80518913369bf3b81091e34c81f5da2e3477c8323d3654f608052ebc8
6
+ metadata.gz: ef97e09b88610ae38f45295e650e8b79b5cdaf77e46d616753ffcc1aa98cb34f5f0267b30a1ca7dc3a859df55f9b4305b150e7412dc05ad1062504777be819d2
7
+ data.tar.gz: c4cae237dd5fbf03da3ddb8061ee76cc597e5fcc44d45b6351d2a2fad646bf6f4e3ff035dd9dba803e4cb70c421bfeeea1b09544dcb4d837b9ad9135d7bec1cc
@@ -1,17 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- coverband-service-client (0.0.7)
4
+ coverband-service-client (0.0.12.rc)
5
5
  coverband (~> 4.2.4)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- coverband (4.2.4)
10
+ coverband (4.2.5)
11
11
  redis
12
12
  minitest (5.14.0)
13
13
  rake (13.0.1)
14
- redis (4.1.3)
14
+ redis (4.1.4)
15
15
 
16
16
  PLATFORMS
17
17
  ruby
@@ -1,19 +1,32 @@
1
1
  # frozen_string_literal: true
2
+ require 'socket'
2
3
 
4
+ COVERBAND_ORIGINAL_START = ENV['COVERBAND_DISABLE_AUTO_START']
5
+ ENV['COVERBAND_DISABLE_AUTO_START'] = 'true'
3
6
  require 'coverband'
4
7
  require 'coverband/service/client/version'
5
8
  require 'securerandom'
6
9
 
7
- COVERBAND_ENV = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || (defined?(Rails) ? Rails.env : 'unknown')
8
- COVERBAND_SERVICE_URL = ENV['COVERBAND_URL'] ||
9
- ((COVERBAND_ENV == 'development') ? 'http://127.0.0.1:3456' : 'https://coverband-service.herokuapp.com')
10
- COVERBAND_TIMEOUT = (COVERBAND_ENV == 'development') ? 5 : 1
11
-
12
10
  module Coverband
11
+ COVERBAND_ENV = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || (defined?(Rails) ? Rails.env : 'unknown')
12
+ COVERBAND_SERVICE_URL = ENV['COVERBAND_URL'] || 'https://coverband.io'
13
+ COVERBAND_TIMEOUT = (COVERBAND_ENV == 'development') ? 5 : 1
14
+ COVERBAND_ENABLE_DEV_MODE = ENV['COVERBAND_ENABLE_DEV_MODE'] || false
15
+ COVERBAND_ENABLE_TEST_MODE = ENV['COVERBAND_ENABLE_TEST_MODE'] || false
16
+ COVERBAND_PROCESS_TYPE = ENV['PROCESS_TYPE'] || 'unknown'
17
+ COVERBAND_REPORT_PERIOD = (ENV['COVERBAND_REPORT_PERIOD'] || 600).to_i
18
+
19
+ def self.service_disabled_dev_test_env?
20
+ (COVERBAND_ENV == 'test' && !COVERBAND_ENABLE_TEST_MODE) ||
21
+ (COVERBAND_ENV == 'development' && !COVERBAND_ENABLE_DEV_MODE)
22
+ end
13
23
 
14
- if COVERBAND_ENV == 'test' && !ENV['COVERBAND_ENABLE_TEST_MODE']
24
+ if service_disabled_dev_test_env?
15
25
  def self.report_coverage
16
- # for now disable coverband reporting in test env by default
26
+ # for now disable coverband reporting in test & dev env by default
27
+ if Coverband.configuration.verbose
28
+ puts "Coverband: disabled for #{COVERBAND_ENV}, set COVERBAND_ENABLE_DEV_MODE or COVERBAND_ENABLE_TEST_MODE to enable" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
29
+ end
17
30
  end
18
31
  end
19
32
 
@@ -23,18 +36,23 @@ module Coverband
23
36
  #
24
37
  # NOTES:
25
38
  # * uses net/http to avoid any dependencies
26
- # * currently JSON, but likely better to move to something simpler / faster
39
+ # * currently JSON, but likely better to move to something faster
27
40
  ###
28
41
  class Service < Base
29
- attr_reader :coverband_url, :process_type, :runtime_env
42
+ attr_reader :coverband_url, :process_type, :runtime_env, :hostname, :pid
30
43
 
31
44
  def initialize(coverband_url, opts = {})
32
45
  super()
33
46
  @coverband_url = coverband_url
34
- @process_type = opts.fetch(:process_type) { 'unknown' }
47
+ @process_type = opts.fetch(:process_type) { $PROGRAM_NAME&.split('/')&.last || COVERBAND_PROCESS_TYPE }
48
+ @hostname = opts.fetch(:hostname) { ENV["DYNO"] || Socket.gethostname.force_encoding('utf-8').encode }
35
49
  @runtime_env = opts.fetch(:runtime_env) { COVERBAND_ENV }
36
50
  end
37
51
 
52
+ def logger
53
+ Coverband.configuration.logger
54
+ end
55
+
38
56
  def clear!
39
57
  # TBD
40
58
  end
@@ -48,28 +66,35 @@ module Coverband
48
66
  0
49
67
  end
50
68
 
51
- # TODO: no longer get by type just get both reports in a single request
69
+ def api_key
70
+ ENV['COVERBAND_API_KEY'] || Coverband.configuration.api_key
71
+ end
72
+
73
+ ###
74
+ # Fetch coverband coverage via the API
75
+ ###
52
76
  def coverage(local_type = nil, opts = {})
53
77
  local_type ||= opts.key?(:override_type) ? opts[:override_type] : type
54
- uri = URI("#{coverband_url}/api/coverage/#{ENV['COVERBAND_ID']}?type=#{local_type}")
55
- req = Net::HTTP::Get.new(uri, 'Content-Type' => 'application/json', 'Coverband-Token' => ENV['COVERBAND_API_KEY'])
78
+ env_filter = opts.key?(:env_filter) ? opts[:env_filter] : 'production'
79
+ uri = URI("#{coverband_url}/api/coverage?type=#{local_type}&env_filter=#{env_filter}",)
80
+ req = Net::HTTP::Get.new(uri, 'Content-Type' => 'application/json', 'Coverband-Token' => api_key)
56
81
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
57
82
  http.request(req)
58
83
  end
59
84
  coverage_data = JSON.parse(res.body)
60
- # puts "coverage data: "
61
- # puts coverage_data
62
85
  coverage_data
63
86
  rescue StandardError => e
64
- puts "Coverband: Error while retrieving coverage #{e}"
87
+ logger&.error "Coverband: Error while retrieving coverage #{e}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
65
88
  end
66
89
 
67
90
  def save_report(report)
68
- #puts caller.join(',')
69
91
  return if report.empty?
70
92
 
93
+ # We set here vs initialize to avoid setting on the primary process vs child processes
94
+ @pid ||= ::Process.pid
95
+
71
96
  # TODO: do we need dup
72
- # TODO: remove timestamps, server will track first_seen
97
+ # TODO: remove upstream timestamps, server will track first_seen
73
98
  Thread.new do
74
99
  data = expand_report(report.dup)
75
100
  full_package = {
@@ -78,7 +103,9 @@ module Coverband
78
103
  tags: {
79
104
  process_type: process_type,
80
105
  app_loading: type == Coverband::EAGER_TYPE,
81
- runtime_env: runtime_env
106
+ runtime_env: runtime_env,
107
+ pid: pid,
108
+ hostname: hostname,
82
109
  },
83
110
  file_coverage: data
84
111
  }
@@ -94,12 +121,18 @@ module Coverband
94
121
  private
95
122
 
96
123
  def save_coverage(data)
124
+ if api_key.nil?
125
+ puts "Coverband: Error: no Coverband API key was found!"
126
+ return
127
+ end
128
+
97
129
  uri = URI("#{coverband_url}/api/collector")
98
130
  req = Net::HTTP::Post.new(uri,
99
131
  'Content-Type' => 'application/json',
100
- 'Coverband-Token' => ENV['COVERBAND_API_KEY'])
101
- # puts "sending #{data}"
132
+ 'Coverband-Token' => api_key)
102
133
  req.body = { remote_uuid: SecureRandom.uuid, data: data }.to_json
134
+
135
+ logger&.info "Coverband: saving (#{uri}) #{req.body}" if Coverband.configuration.verbose
103
136
  res = Net::HTTP.start(
104
137
  uri.hostname,
105
138
  uri.port,
@@ -111,7 +144,51 @@ module Coverband
111
144
  http.request(req)
112
145
  end
113
146
  rescue StandardError => e
114
- puts "Coverband: Error while saving coverage #{e}"
147
+ logger&.info "Coverband: Error while saving coverage #{e}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
148
+ end
149
+ end
150
+
151
+ class PersistentService < Service
152
+ attr_reader :http
153
+
154
+ def initialize(coverband_url, opts = {})
155
+ super
156
+ initiate_http
157
+ end
158
+
159
+ private
160
+
161
+ def initiate_http
162
+ @http = Net::HTTP::Persistent.new name: 'coverband_persistent'
163
+ @http.headers['Content-Type'] = 'application/json'
164
+ @http.headers['Coverband-Token'] = api_key
165
+ @http.open_timeout = COVERBAND_TIMEOUT
166
+ @http.read_timeout = COVERBAND_TIMEOUT
167
+ @http.ssl_timeout = COVERBAND_TIMEOUT
168
+ end
169
+
170
+ def save_coverage(data)
171
+ persistent_attempts = 0
172
+ begin
173
+ if api_key.nil?
174
+ puts "Coverband: Error: no Coverband API key was found!"
175
+ return
176
+ end
177
+
178
+ post_uri = URI("#{coverband_url}/api/collector")
179
+ post = Net::HTTP::Post.new post_uri.path
180
+ body = { remote_uuid: SecureRandom.uuid, data: data }.to_json
181
+ post.body = body
182
+ logger&.info "Coverband: saving (#{post_uri}) #{body}" if Coverband.configuration.verbose
183
+ res = http.request post_uri, post
184
+ rescue Net::HTTP::Persistent::Error => e
185
+ persistent_attempts += 1
186
+ http.shutdown
187
+ initiate_http
188
+ retry if persistent_attempts < 2
189
+ end
190
+ rescue StandardError => e
191
+ logger&.info "Coverband: Error while saving coverage #{e}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
115
192
  end
116
193
  end
117
194
  end
@@ -145,14 +222,22 @@ module Coverband
145
222
  rescue StandardError => e
146
223
  # we don't want to raise errors if Coverband can't reach redis.
147
224
  # This is a nice to have not a bring the system down
148
- logger&.error "Coverband: view_tracker failed to store, error #{e.class.name}"
225
+ logger&.error "Coverband: view_tracker failed to store, error #{e.class.name}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
149
226
  end
150
227
 
151
228
  private
152
229
 
230
+ def api_key
231
+ ENV['COVERBAND_API_KEY'] || Coverband.configuration.api_key
232
+ end
233
+
234
+ def logger
235
+ Coverband.configuration.logger
236
+ end
237
+
153
238
  def save_tracked_views(views:, reported_time:)
154
239
  uri = URI("#{COVERBAND_SERVICE_URL}/api/collector")
155
- req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json', 'Coverband-Token' => ENV['COVERBAND_API_KEY'])
240
+ req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json', 'Coverband-Token' => api_key)
156
241
  data = {
157
242
  collection_type: 'view_tracker_delta',
158
243
  collection_data: {
@@ -169,25 +254,53 @@ module Coverband
169
254
  http.request(req)
170
255
  end
171
256
  rescue StandardError => e
172
- puts "Coverband: Error while saving coverage #{e}"
257
+ logger&.error "Coverband: Error while saving coverage #{e}" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
173
258
  end
174
259
  end
175
260
  end
176
261
  end
177
262
 
263
+ module Coverband
264
+ class Configuration
265
+ attr_accessor :api_key
266
+ end
267
+ end
268
+
269
+ ENV['COVERBAND_DISABLE_AUTO_START'] = COVERBAND_ORIGINAL_START
178
270
  Coverband.configure do |config|
179
- # Use The Test Service Adapter
180
- config.store = Coverband::Adapters::Service.new(COVERBAND_SERVICE_URL)
271
+ # Use the Service Adapter
272
+ if defined?(Net::HTTP::Persistent)
273
+ config.store = Coverband::Adapters::PersistentService.new(Coverband::COVERBAND_SERVICE_URL)
274
+ else
275
+ config.store = Coverband::Adapters::Service.new(Coverband::COVERBAND_SERVICE_URL)
276
+ end
181
277
 
182
278
  # default to tracking views true
183
- config.track_views = ENV['COVERBAND_ENABLE_VIEW_TRACKER'] ? true : false
279
+ config.track_views = if ENV['COVERBAND_DISABLE_VIEW_TRACKER']
280
+ false
281
+ elsif Coverband.service_disabled_dev_test_env?
282
+ false
283
+ else
284
+ true
285
+ end
184
286
 
185
287
  # report every 10m by default
186
- config.background_reporting_sleep_seconds = COVERBAND_ENV == 'production' ? 600 : 60
288
+ config.background_reporting_sleep_seconds = Coverband::COVERBAND_ENV == 'production' ? Coverband::COVERBAND_REPORT_PERIOD : 60
187
289
  # add a wiggle to avoid service stampede
188
- config.reporting_wiggle = COVERBAND_ENV == 'production' ? 90 : 6
290
+ config.reporting_wiggle = Coverband::COVERBAND_ENV == 'production' ? 90 : 6
189
291
 
190
- if COVERBAND_ENV == 'test'
292
+ if Coverband::COVERBAND_ENV == 'test'
191
293
  config.background_reporting_enabled = false
192
294
  end
193
295
  end
296
+
297
+ # NOTE: it is really hard to bypass / overload our config we should fix this in Coverband
298
+ # this hopefully detects anyone that has both gems and was trying to configure Coverband themselves.
299
+ if File.exist?('./config/coverband.rb')
300
+ puts "Warning: config/coverband.rb found, this overrides coverband service allowing one to setup open source Coverband" if Coverband.configuration.verbose || COVERBAND_ENABLE_DEV_MODE
301
+ end
302
+
303
+ Coverband.configure('./config/coverband_service.rb') if File.exist?('./config/coverband_service.rb')
304
+ Coverband.start
305
+ require "coverband/utils/railtie" if defined? ::Rails::Railtie
306
+ require "coverband/integrations/resque" if defined? ::Resque
@@ -1,7 +1,7 @@
1
1
  module Coverband
2
2
  module Service
3
3
  module Client
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.12.rc'
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coverband-service-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.12.rc
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Mayer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-04-04 00:00:00.000000000 Z
12
+ date: 2020-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -106,9 +106,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ">="
109
+ - - ">"
110
110
  - !ruby/object:Gem::Version
111
- version: '0'
111
+ version: 1.3.1
112
112
  requirements: []
113
113
  rubygems_version: 3.0.3
114
114
  signing_key: