skylight-core 3.1.5 → 4.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/lib/skylight/core/config.rb +103 -108
  3. data/lib/skylight/core/deprecation.rb +3 -3
  4. data/lib/skylight/core/fanout.rb +3 -3
  5. data/lib/skylight/core/formatters/http.rb +1 -2
  6. data/lib/skylight/core/gc.rb +31 -36
  7. data/lib/skylight/core/instrumentable.rb +17 -16
  8. data/lib/skylight/core/instrumenter.rb +25 -23
  9. data/lib/skylight/core/middleware.rb +11 -10
  10. data/lib/skylight/core/normalizers/action_controller/process_action.rb +9 -9
  11. data/lib/skylight/core/normalizers/action_controller/send_file.rb +16 -16
  12. data/lib/skylight/core/normalizers/action_view/render_collection.rb +2 -2
  13. data/lib/skylight/core/normalizers/action_view/render_partial.rb +2 -2
  14. data/lib/skylight/core/normalizers/action_view/render_template.rb +2 -2
  15. data/lib/skylight/core/normalizers/active_job/perform.rb +11 -23
  16. data/lib/skylight/core/normalizers/active_model_serializers/render.rb +6 -6
  17. data/lib/skylight/core/normalizers/active_record/instantiation.rb +2 -3
  18. data/lib/skylight/core/normalizers/active_record/sql.rb +1 -2
  19. data/lib/skylight/core/normalizers/active_support/cache.rb +12 -10
  20. data/lib/skylight/core/normalizers/active_support/cache_clear.rb +2 -2
  21. data/lib/skylight/core/normalizers/active_support/cache_decrement.rb +3 -3
  22. data/lib/skylight/core/normalizers/active_support/cache_delete.rb +3 -3
  23. data/lib/skylight/core/normalizers/active_support/cache_exist.rb +3 -3
  24. data/lib/skylight/core/normalizers/active_support/cache_fetch_hit.rb +3 -3
  25. data/lib/skylight/core/normalizers/active_support/cache_generate.rb +3 -3
  26. data/lib/skylight/core/normalizers/active_support/cache_increment.rb +3 -3
  27. data/lib/skylight/core/normalizers/active_support/cache_read.rb +3 -3
  28. data/lib/skylight/core/normalizers/active_support/cache_read_multi.rb +3 -3
  29. data/lib/skylight/core/normalizers/active_support/cache_write.rb +3 -3
  30. data/lib/skylight/core/normalizers/coach/handler_finish.rb +6 -6
  31. data/lib/skylight/core/normalizers/coach/middleware_finish.rb +2 -2
  32. data/lib/skylight/core/normalizers/couch_potato/query.rb +20 -0
  33. data/lib/skylight/core/normalizers/data_mapper/sql.rb +1 -1
  34. data/lib/skylight/core/normalizers/default.rb +1 -4
  35. data/lib/skylight/core/normalizers/elasticsearch/request.rb +5 -5
  36. data/lib/skylight/core/normalizers/grape/endpoint.rb +6 -5
  37. data/lib/skylight/core/normalizers/grape/endpoint_render.rb +3 -4
  38. data/lib/skylight/core/normalizers/grape/endpoint_run.rb +2 -3
  39. data/lib/skylight/core/normalizers/grape/endpoint_run_filters.rb +1 -2
  40. data/lib/skylight/core/normalizers/grape/format_response.rb +1 -2
  41. data/lib/skylight/core/normalizers/moped/query.rb +38 -43
  42. data/lib/skylight/core/normalizers/render.rb +26 -25
  43. data/lib/skylight/core/normalizers/sequel/sql.rb +1 -1
  44. data/lib/skylight/core/normalizers/sql.rb +5 -5
  45. data/lib/skylight/core/normalizers.rb +11 -12
  46. data/lib/skylight/core/probes/action_controller.rb +15 -14
  47. data/lib/skylight/core/probes/action_dispatch/request_id.rb +2 -3
  48. data/lib/skylight/core/probes/action_dispatch/routing/route_set.rb +2 -3
  49. data/lib/skylight/core/probes/action_dispatch.rb +2 -2
  50. data/lib/skylight/core/probes/action_view.rb +11 -17
  51. data/lib/skylight/core/probes/active_job.rb +8 -4
  52. data/lib/skylight/core/probes/active_job_enqueue.rb +2 -2
  53. data/lib/skylight/core/probes/active_model_serializers.rb +7 -6
  54. data/lib/skylight/core/probes/delayed_job.rb +46 -0
  55. data/lib/skylight/core/probes/elasticsearch.rb +3 -4
  56. data/lib/skylight/core/probes/excon/middleware.rb +4 -7
  57. data/lib/skylight/core/probes/excon.rb +2 -2
  58. data/lib/skylight/core/probes/faraday.rb +1 -2
  59. data/lib/skylight/core/probes/httpclient.rb +4 -4
  60. data/lib/skylight/core/probes/middleware.rb +1 -1
  61. data/lib/skylight/core/probes/mongo.rb +29 -34
  62. data/lib/skylight/core/probes/mongoid.rb +2 -3
  63. data/lib/skylight/core/probes/moped.rb +10 -11
  64. data/lib/skylight/core/probes/net_http.rb +5 -5
  65. data/lib/skylight/core/probes/redis.rb +3 -4
  66. data/lib/skylight/core/probes/sequel.rb +4 -4
  67. data/lib/skylight/core/probes/sinatra.rb +6 -11
  68. data/lib/skylight/core/probes/tilt.rb +1 -1
  69. data/lib/skylight/core/probes.rb +16 -17
  70. data/lib/skylight/core/railtie.rb +120 -119
  71. data/lib/skylight/core/sidekiq.rb +8 -4
  72. data/lib/skylight/core/subscriber.rb +25 -29
  73. data/lib/skylight/core/test.rb +13 -17
  74. data/lib/skylight/core/trace.rb +76 -76
  75. data/lib/skylight/core/user_config.rb +9 -11
  76. data/lib/skylight/core/util/allocation_free.rb +0 -1
  77. data/lib/skylight/core/util/clock.rb +16 -16
  78. data/lib/skylight/core/util/gzip.rb +1 -2
  79. data/lib/skylight/core/util/inflector.rb +5 -4
  80. data/lib/skylight/core/util/logging.rb +11 -14
  81. data/lib/skylight/core/util/platform.rb +52 -49
  82. data/lib/skylight/core/util/proxy.rb +3 -3
  83. data/lib/skylight/core/util.rb +5 -5
  84. data/lib/skylight/core/version.rb +1 -2
  85. data/lib/skylight/core/vm/gc.rb +1 -3
  86. data/lib/skylight/core.rb +21 -21
  87. metadata +47 -44
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1bff6f694c07d0d3b51b9c6e204c0b8a773975f400bd0267b7bdcd8196f26f3
4
- data.tar.gz: 1187010fff1083dbfab549871ff5d69f68227e3dc6dc94e66f0807cfeff3b2a0
3
+ metadata.gz: ed175a550ab9f4dfeecf785242272a6415e1bc9b6a723f3d5df527bc806c020c
4
+ data.tar.gz: dc65a9796aaafef2b25ab8f8aa2bc065b69653fa8f03f7696c08c3b78c26d7e1
5
5
  SHA512:
6
- metadata.gz: 037f6038e8f7c9608b1a0096cd63708c331f90340e50de56a6975909b49dc71daf5c6f334716e5ed345114aa1db8e1c8c42e43784d34c150215610b6a48e4dc6
7
- data.tar.gz: 19d2b4f48531891e4924522fd5df46a5d7a6abb021768f4b8d3ac5d2810252387f03b712573b0ded27af1f3447280aba7910fb4acf1e2a259c57a557d728e6e1
6
+ metadata.gz: 5316aa9d177e810193ae1ed0c93c0ea8e307ed1cace0891083a797d260dd650a03a1afc6a287495b2a706a409a5c03543d22edc38cc0562667a87111d00f74ce
7
+ data.tar.gz: 0524413c1fd1b13334ec6faa1b8372200c71b7a049d0c6aed4371e0220b5331c7a286333d3b48a348a064d43400e09650ca42b02a91fe5c3135760d017bf08c0
@@ -1,11 +1,10 @@
1
- require 'yaml'
2
- require 'fileutils'
3
- require 'thread'
4
- require 'erb'
5
- require 'json'
6
- require 'skylight/core/util/logging'
7
- require 'skylight/core/util/proxy'
8
- require 'skylight/core/errors'
1
+ require "yaml"
2
+ require "fileutils"
3
+ require "erb"
4
+ require "json"
5
+ require "skylight/core/util/logging"
6
+ require "skylight/core/util/proxy"
7
+ require "skylight/core/errors"
9
8
 
10
9
  module Skylight::Core
11
10
  class Config
@@ -14,30 +13,32 @@ module Skylight::Core
14
13
  # @api private
15
14
  MUTEX = Mutex.new
16
15
 
16
+ # rubocop:disable Style/SingleLineMethods, Layout/EmptyLineBetweenDefs
17
17
  def self.log_name; "Skylight" end
18
18
  def self.service_name; log_name end
19
19
  def self.support_email; "support@skylight.io" end
20
20
  def self.env_matcher; /^(?:SK|SKYLIGHT)_(.+)$/ end
21
21
  def self.env_prefix; "SKYLIGHT_" end
22
+ # rubocop:enable Style/SingleLineMethods, Layout/EmptyLineBetweenDefs
22
23
 
23
24
  # Map environment variable keys with Skylight configuration keys
24
25
  def self.env_to_key
25
26
  {
26
27
  # == Logging ==
27
- 'LOG_FILE' => :log_file,
28
- 'LOG_LEVEL' => :log_level,
29
- 'ALERT_LOG_FILE' => :alert_log_file,
30
- 'LOG_SQL_PARSE_ERRORS' => :log_sql_parse_errors,
28
+ "LOG_FILE" => :log_file,
29
+ "LOG_LEVEL" => :log_level,
30
+ "ALERT_LOG_FILE" => :alert_log_file,
31
+ "LOG_SQL_PARSE_ERRORS" => :log_sql_parse_errors,
31
32
 
32
33
  # == Proxy ==
33
- 'PROXY_URL' => :proxy_url,
34
+ "PROXY_URL" => :proxy_url,
34
35
 
35
36
  # == Instrumenter ==
36
37
  "ENABLE_SEGMENTS" => :enable_segments,
37
38
  "ENABLE_SIDEKIQ" => :enable_sidekiq,
38
39
 
39
40
  # == User config settings ==
40
- "USER_CONFIG_PATH" => :'user_config_path',
41
+ "USER_CONFIG_PATH" => :user_config_path,
41
42
 
42
43
  # == Heroku settings ==
43
44
  "HEROKU_DYNO_INFO_PATH" => :'heroku.dyno_info_path'
@@ -47,13 +48,13 @@ module Skylight::Core
47
48
  # Default values for Skylight configuration keys
48
49
  def self.default_values
49
50
  {
50
- :log_file => '-'.freeze,
51
- :log_level => 'INFO'.freeze,
52
- :alert_log_file => '-'.freeze,
53
- :log_sql_parse_errors => true,
54
- :enable_segments => true,
55
- :enable_sidekiq => false,
56
- :'heroku.dyno_info_path' => '/etc/heroku/dyno'
51
+ log_file: "-".freeze,
52
+ log_level: "INFO".freeze,
53
+ alert_log_file: "-".freeze,
54
+ log_sql_parse_errors: true,
55
+ enable_segments: true,
56
+ enable_sidekiq: false,
57
+ 'heroku.dyno_info_path': "/etc/heroku/dyno"
57
58
  }
58
59
  end
59
60
 
@@ -69,10 +70,10 @@ module Skylight::Core
69
70
  end
70
71
 
71
72
  def self.native_env_keys
72
- [
73
- :version,
74
- :root,
75
- :proxy_url
73
+ %i[
74
+ version
75
+ root
76
+ proxy_url
76
77
  ]
77
78
  end
78
79
 
@@ -94,7 +95,7 @@ module Skylight::Core
94
95
  def initialize(*args)
95
96
  attrs = {}
96
97
 
97
- if Hash === args.last
98
+ if args.last.is_a?(Hash)
98
99
  attrs = args.pop.dup
99
100
  end
100
101
 
@@ -106,7 +107,7 @@ module Skylight::Core
106
107
 
107
108
  p = attrs.delete(:priority)
108
109
 
109
- if @environment = args[0]
110
+ if (@environment = args[0])
110
111
  @regexp = /^#{Regexp.escape(@environment)}\.(.+)$/
111
112
  end
112
113
 
@@ -122,16 +123,14 @@ module Skylight::Core
122
123
  end
123
124
 
124
125
  def self.load(opts = {}, env = ENV)
125
- attrs = {}
126
- version = nil
127
-
126
+ attrs = {}
128
127
  path = opts.delete(:file)
129
128
  environment = opts.delete(:environment)
130
129
 
131
130
  if path
132
131
  error = nil
133
132
  begin
134
- attrs = YAML.load(ERB.new(File.read(path)).result)
133
+ attrs = YAML.safe_load(ERB.new(File.read(path)).result)
135
134
  error = "empty file" unless attrs
136
135
  error = "invalid format" if attrs && !attrs.is_a?(Hash)
137
136
  rescue Exception => e
@@ -139,8 +138,6 @@ module Skylight::Core
139
138
  end
140
139
 
141
140
  raise ConfigError, "could not load config file; msg=#{error}" if error
142
-
143
- version = File.mtime(path).to_i
144
141
  end
145
142
 
146
143
  if env
@@ -168,24 +165,23 @@ module Skylight::Core
168
165
  return ret unless env
169
166
 
170
167
  # Only set if it exists, we don't want to set to a nil value
171
- if proxy_url = Util::Proxy.detect_url(env)
168
+ if (proxy_url = Util::Proxy.detect_url(env))
172
169
  ret[:proxy_url] = proxy_url
173
170
  end
174
171
 
175
172
  env.each do |k, val|
176
173
  next unless k =~ env_matcher
177
-
178
- if key = env_to_key[$1]
179
- ret[key] =
180
- case val
181
- when /^false$/i then false
182
- when /^true$/i then true
183
- when /^(nil|null)$/i then nil
184
- when /^\d+$/ then val.to_i
185
- when /^\d+\.\d+$/ then val.to_f
186
- else val
187
- end
188
- end
174
+ next unless (key = env_to_key[$1])
175
+
176
+ ret[key] =
177
+ case val
178
+ when /^false$/i then false
179
+ when /^true$/i then true
180
+ when /^(nil|null)$/i then nil
181
+ when /^\d+$/ then val.to_i
182
+ when /^\d+\.\d+$/ then val.to_f
183
+ else val
184
+ end
189
185
  end
190
186
 
191
187
  ret
@@ -228,7 +224,7 @@ module Skylight::Core
228
224
  end
229
225
 
230
226
  def check_logfile_permissions(log_file, key)
231
- return if log_file == '-' # STDOUT
227
+ return if log_file == "-" # STDOUT
232
228
  log_file = File.expand_path(log_file, root)
233
229
  check_file_permissions(log_file, key)
234
230
  end
@@ -238,7 +234,7 @@ module Skylight::Core
238
234
  @priority.key?(key) || @values.key?(key)
239
235
  end
240
236
 
241
- def get(key, default = nil, &blk)
237
+ def get(key, default = nil)
242
238
  key = self.class.remap_key(key)
243
239
 
244
240
  return @priority[key] if @priority.key?(key)
@@ -247,8 +243,8 @@ module Skylight::Core
247
243
 
248
244
  if default
249
245
  return default
250
- elsif blk
251
- return blk.call(key)
246
+ elsif block_given?
247
+ return yield key
252
248
  end
253
249
 
254
250
  nil
@@ -258,17 +254,17 @@ module Skylight::Core
258
254
 
259
255
  def set(key, val, scope = nil)
260
256
  if scope
261
- key = [scope, key].join('.')
257
+ key = [scope, key].join(".")
262
258
  end
263
259
 
264
- if Hash === val
260
+ if val.is_a?(Hash)
265
261
  val.each do |k, v|
266
262
  set(k, v, key)
267
263
  end
268
264
  else
269
265
  k = self.class.remap_key(key)
270
266
 
271
- if validator = self.class.validators[k]
267
+ if (validator = self.class.validators[k])
272
268
  blk, msg = validator
273
269
 
274
270
  unless blk.call(val, self)
@@ -288,8 +284,8 @@ module Skylight::Core
288
284
 
289
285
  alias []= set
290
286
 
291
- def send_or_get(v)
292
- respond_to?(v) ? send(v) : get(v)
287
+ def send_or_get(val)
288
+ respond_to?(val) ? send(val) : get(val)
293
289
  end
294
290
 
295
291
  def duration_ms(key, default = nil)
@@ -310,13 +306,17 @@ module Skylight::Core
310
306
  end
311
307
  end
312
308
 
313
- def to_json
314
- JSON.generate(
309
+ def to_json(*)
310
+ JSON.generate(as_json)
311
+ end
312
+
313
+ def as_json(*)
314
+ {
315
315
  config: {
316
316
  priority: @priority,
317
317
  values: @values
318
318
  }
319
- )
319
+ }
320
320
  end
321
321
 
322
322
  def to_native_env
@@ -333,7 +333,7 @@ module Skylight::Core
333
333
  ret
334
334
  end
335
335
 
336
- def write(path)
336
+ def write(_path)
337
337
  raise "not implemented"
338
338
  end
339
339
 
@@ -349,7 +349,7 @@ module Skylight::Core
349
349
 
350
350
  # @api private
351
351
  def gc
352
- @gc ||= GC.new(self, get('gc.profiler', VM::GC.new))
352
+ @gc ||= GC.new(self, get("gc.profiler", VM::GC.new))
353
353
  end
354
354
 
355
355
  # @api private
@@ -381,15 +381,13 @@ module Skylight::Core
381
381
  end
382
382
  end
383
383
 
384
- def logger=(logger)
385
- @logger = logger
386
- end
384
+ attr_writer :logger
387
385
 
388
386
  def alert_logger
389
387
  @alert_logger ||= MUTEX.synchronize do
390
- unless l = @alert_logger
388
+ unless (l = @alert_logger)
391
389
  out = get(:alert_log_file)
392
- out = Util::AlertLogger.new(load_logger) if out == '-'
390
+ out = Util::AlertLogger.new(load_logger) if out == "-"
393
391
 
394
392
  l = create_logger(out)
395
393
  l.level = Logger::DEBUG
@@ -399,9 +397,7 @@ module Skylight::Core
399
397
  end
400
398
  end
401
399
 
402
- def alert_logger=(logger)
403
- @alert_logger = logger
404
- end
400
+ attr_writer :alert_logger
405
401
 
406
402
  def enable_segments?
407
403
  !!get(:enable_segments)
@@ -419,56 +415,55 @@ module Skylight::Core
419
415
  File.exist?(get(:'heroku.dyno_info_path'))
420
416
  end
421
417
 
422
- private
418
+ private
423
419
 
424
- def create_logger(out)
425
- l = begin
426
- if out.is_a?(String)
427
- out = File.expand_path(out, root)
428
- # May be redundant since we also do this in the permissions check
429
- FileUtils.mkdir_p(File.dirname(out))
430
- end
420
+ def create_logger(out)
421
+ l = begin
422
+ if out.is_a?(String)
423
+ out = File.expand_path(out, root)
424
+ # May be redundant since we also do this in the permissions check
425
+ FileUtils.mkdir_p(File.dirname(out))
426
+ end
431
427
 
432
- Logger.new(out)
433
- rescue
434
- Logger.new(STDOUT)
428
+ Logger.new(out)
429
+ rescue
430
+ Logger.new(STDOUT)
431
+ end
432
+ l.progname = self.class.log_name
433
+ l
435
434
  end
436
- l.progname = self.class.log_name
437
- l
438
- end
439
435
 
440
- def load_logger
441
- unless l = @logger
442
- out = get(:log_file)
443
- out = STDOUT if out == '-'
436
+ def load_logger
437
+ unless (l = @logger)
438
+ out = get(:log_file)
439
+ out = STDOUT if out == "-"
444
440
 
445
- l = create_logger(out)
446
- if trace?
447
- l.level = Logger::DEBUG
448
- else
441
+ l = create_logger(out)
449
442
  l.level =
450
- case get(:log_level)
451
- when /^debug$/i then Logger::DEBUG
452
- when /^info$/i then Logger::INFO
453
- when /^warn$/i then Logger::WARN
454
- when /^error$/i then Logger::ERROR
455
- when /^fatal$/i then Logger::FATAL
456
- else Logger::ERROR
443
+ if trace?
444
+ Logger::DEBUG
445
+ else
446
+ case get(:log_level)
447
+ when /^debug$/i then Logger::DEBUG
448
+ when /^info$/i then Logger::INFO
449
+ when /^warn$/i then Logger::WARN
450
+ when /^error$/i then Logger::ERROR
451
+ when /^fatal$/i then Logger::FATAL
452
+ else Logger::ERROR
453
+ end
457
454
  end
458
455
  end
459
- end
460
456
 
461
- l
462
- end
463
-
464
- def cast_for_env(v)
465
- case v
466
- when true then 'true'
467
- when false then 'false'
468
- when nil then 'nil'
469
- else v.to_s
457
+ l
470
458
  end
471
- end
472
459
 
460
+ def cast_for_env(val)
461
+ case val
462
+ when true then "true"
463
+ when false then "false"
464
+ when nil then "nil"
465
+ else val.to_s
466
+ end
467
+ end
473
468
  end
474
469
  end
@@ -1,7 +1,7 @@
1
- require 'active_support/deprecation'
1
+ require "active_support/deprecation"
2
2
 
3
3
  module Skylight::Core
4
- SKYLIGHT_GEM_ROOT = File.expand_path("../../..", __FILE__) + "/"
4
+ SKYLIGHT_GEM_ROOT = File.expand_path("../..", __dir__) + "/"
5
5
 
6
6
  class Deprecation < ActiveSupport::Deprecation
7
7
  private
@@ -11,5 +11,5 @@ module Skylight::Core
11
11
  end
12
12
  end
13
13
 
14
- DEPRECATOR = Deprecation.new('4.0', 'skylight-core')
14
+ DEPRECATOR = Deprecation.new("4.0", "skylight-core")
15
15
  end
@@ -14,7 +14,7 @@ module Skylight
14
14
  end
15
15
 
16
16
  def self.trace(*args)
17
- registered.map{|r| r.trace(*args) }
17
+ registered.map { |r| r.trace(*args) }
18
18
  end
19
19
 
20
20
  def self.instrument(*args)
@@ -33,8 +33,8 @@ module Skylight
33
33
  end
34
34
  end
35
35
 
36
- def self.done(spans, meta=nil)
37
- spans.reverse.each do |(target, span)|
36
+ def self.done(spans, meta = nil)
37
+ spans.reverse_each do |(target, span)|
38
38
  target.done(span, meta)
39
39
  end
40
40
  end
@@ -2,7 +2,6 @@ module Skylight
2
2
  module Core
3
3
  module Formatters
4
4
  module HTTP
5
-
6
5
  # Build instrumentation options for HTTP queries
7
6
  #
8
7
  # @param [String] method HTTP method, e.g. get, post
@@ -12,7 +11,7 @@ module Skylight
12
11
  # @param [String] path Request path
13
12
  # @param [String] query Request query string
14
13
  # @return [Hash] a hash containing `:category`, `:title`, and `:annotations`
15
- def self.build_opts(method, scheme, host, port, path, query)
14
+ def self.build_opts(method, _scheme, host, _port, _path, _query)
16
15
  { category: "api.http.#{method.downcase}",
17
16
  title: "#{method.upcase} #{host}",
18
17
  meta: { host: host } }
@@ -1,9 +1,7 @@
1
- require 'thread'
2
-
3
1
  module Skylight::Core
4
2
  # @api private
5
3
  class GC
6
- METHODS = [ :enable, :total_time ]
4
+ METHODS = %i[enable total_time].freeze
7
5
  TH_KEY = :SK_GC_CURR_WINDOW
8
6
  MAX_COUNT = 1000
9
7
  MAX_TIME = 30_000_000
@@ -36,9 +34,7 @@ module Skylight::Core
36
34
  end
37
35
 
38
36
  def track
39
- unless @profiler
40
- win = Window.new(nil)
41
- else
37
+ if @profiler
42
38
  win = Window.new(self)
43
39
 
44
40
  @lock.synchronize do
@@ -46,17 +42,17 @@ module Skylight::Core
46
42
  @listeners << win
47
43
 
48
44
  # Cleanup any listeners that might have leaked
49
- until @listeners[0].time < MAX_TIME
50
- @listeners.shift
51
- end
45
+ @listeners.shift until @listeners[0].time < MAX_TIME
52
46
 
53
47
  if @listeners.length > MAX_COUNT
54
48
  @listeners.shift
55
49
  end
56
50
  end
57
- end
58
51
 
59
- win
52
+ win
53
+ else
54
+ Window.new(nil)
55
+ end
60
56
  end
61
57
 
62
58
  def release(win)
@@ -73,40 +69,39 @@ module Skylight::Core
73
69
  nil
74
70
  end
75
71
 
76
- private
72
+ private
77
73
 
78
- def __update
79
- time = @profiler.total_time
80
- diff = time - @time
81
- @time = time
74
+ def __update
75
+ time = @profiler.total_time
76
+ diff = time - @time
77
+ @time = time
82
78
 
83
- if diff > 0
84
- @listeners.each do |l|
85
- l.add(diff)
79
+ if diff > 0
80
+ @listeners.each do |l|
81
+ l.add(diff)
82
+ end
86
83
  end
87
84
  end
88
- end
89
85
 
90
- class Window
91
- attr_reader :time
86
+ class Window
87
+ attr_reader :time
92
88
 
93
- def initialize(global)
94
- @global = global
95
- @time = 0
96
- end
89
+ def initialize(global)
90
+ @global = global
91
+ @time = 0
92
+ end
97
93
 
98
- def update
99
- @global.update if @global
100
- end
94
+ def update
95
+ @global.update if @global
96
+ end
101
97
 
102
- def add(time)
103
- @time += time
104
- end
98
+ def add(time)
99
+ @time += time
100
+ end
105
101
 
106
- def release
107
- @global.release(self) if @global
102
+ def release
103
+ @global.release(self) if @global
104
+ end
108
105
  end
109
- end
110
-
111
106
  end
112
107
  end
@@ -1,7 +1,6 @@
1
1
  module Skylight
2
2
  module Core
3
3
  module Instrumentable
4
-
5
4
  def self.included(base)
6
5
  base.extend(Util::Logging)
7
6
  base.extend(ClassMethods)
@@ -16,7 +15,6 @@ module Skylight
16
15
  end
17
16
 
18
17
  module ClassMethods
19
-
20
18
  def instrumenter_class
21
19
  Skylight::Core::Instrumenter
22
20
  end
@@ -38,7 +36,7 @@ module Skylight
38
36
  end
39
37
 
40
38
  # Start instrumenting
41
- def start!(config=nil)
39
+ def start!(config = nil)
42
40
  return @instrumenter if @instrumenter
43
41
 
44
42
  const_get(:LOCK).synchronize do
@@ -50,11 +48,14 @@ module Skylight
50
48
  @instrumenter = instrumenter_class.new(config).start!
51
49
  end
52
50
  rescue => e
53
- level, message = if e.is_a? ConfigError
54
- [:warn, sprintf("Unable to start Instrumenter due to a configuration error: %s", e.message)]
55
- else
56
- [:error, sprintf("Unable to start Instrumenter; msg=%s; class=%s", e.message, e.class)]
57
- end
51
+ level, message =
52
+ if e.is_a?(ConfigError)
53
+ [:warn, format("Unable to start Instrumenter due to a configuration error: %<message>s",
54
+ message: e.message)]
55
+ else
56
+ [:error, format("Unable to start Instrumenter; msg=%<message>s; class=%<klass>s",
57
+ message: e.message, klass: e.class)]
58
+ end
58
59
 
59
60
  if config && config.respond_to?("log_#{level}") && config.respond_to?(:log_trace)
60
61
  config.send("log_#{level}", message)
@@ -82,21 +83,23 @@ module Skylight
82
83
 
83
84
  # Check tracing
84
85
  def tracing?
85
- t { "checking tracing?; thread=#{Thread.current.object_id}"}
86
+ t { "checking tracing?; thread=#{Thread.current.object_id}" }
86
87
  instrumenter && instrumenter.current_trace
87
88
  end
88
89
 
89
90
  # Start a trace
90
- def trace(endpoint=nil, cat=nil, title=nil, meta=nil)
91
+ def trace(endpoint = nil, cat = nil, title = nil, meta = nil, segment: nil)
91
92
  unless instrumenter
92
93
  return yield if block_given?
93
94
  return
94
95
  end
95
96
 
97
+ cat ||= DEFAULT_CATEGORY
98
+
96
99
  if block_given?
97
- instrumenter.trace(endpoint, cat || DEFAULT_CATEGORY, title, nil, meta) { yield }
100
+ instrumenter.trace(endpoint, cat, title, nil, meta, segment: segment) { |tr| yield tr }
98
101
  else
99
- instrumenter.trace(endpoint, cat || DEFAULT_CATEGORY, title, nil, meta)
102
+ instrumenter.trace(endpoint, cat, title, nil, meta, segment: segment)
100
103
  end
101
104
  end
102
105
 
@@ -107,7 +110,7 @@ module Skylight
107
110
  return
108
111
  end
109
112
 
110
- if Hash === opts
113
+ if opts.is_a?(Hash)
111
114
  category = opts[:category] || DEFAULT_CATEGORY
112
115
  title = opts[:title]
113
116
  desc = opts[:description]
@@ -131,7 +134,7 @@ module Skylight
131
134
  end
132
135
 
133
136
  # End a span
134
- def done(span, meta=nil)
137
+ def done(span, meta = nil)
135
138
  return unless instrumenter
136
139
  instrumenter.done(span, meta)
137
140
  end
@@ -155,9 +158,7 @@ module Skylight
155
158
  return unless instrumenter
156
159
  instrumenter.config
157
160
  end
158
-
159
161
  end
160
-
161
162
  end
162
163
  end
163
164
  end