sqreen 1.15.4 → 1.15.5

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: b477f1130500369e463ea5be1787fbe4c5bf23f88288ab48e1df4a625cf2af9f
4
- data.tar.gz: c965568a045fc2a1f928ca52ad68f842a6687124da88a154efdb082de6e551e2
3
+ metadata.gz: 4f460325e9377e9204840feb8c4f53d580b1f4c84fdf1d361889887d22f7bccf
4
+ data.tar.gz: 0a92e132141222360ef9deb652ef755e60c863aca6070e3bc2b102d9f43dfbbe
5
5
  SHA512:
6
- metadata.gz: 1f6c32b6e2fec81bfe459dd42f935d094bf41d26c610e9028132e00ff7fbdd1de26673de1212acd198ef505fa452301fd11607dde91e3f77d92a70d7f68184dc
7
- data.tar.gz: dc32adb2c6b3be835d198d421ae58fcb31462c333a375c229abc2a6941c655e5616ba90a42086d129518809740dea2aa6a03979c03e7d5798291ab68bee15414
6
+ metadata.gz: 331ef90dfd3164b75a56bbe9ee35bef1e64204feedd14b60074dc08f32dcfd8b9750f33793bf0dbc11c3063d011a8f9a824dddb1647b0cadad0b72d8d1ae9663
7
+ data.tar.gz: b5feb7683f00fe75714dea4fc163b5335272b05bb79dfbba59f9af93398a5ee8fda8f636dfb02b9f35b953fee6066154eeb4df5a6f95b3299224295aad8d3525
@@ -3,6 +3,8 @@
3
3
 
4
4
  require 'sqreen/deliveries/simple'
5
5
  require 'sqreen/events/remote_exception'
6
+ require 'sqreen/mono_time'
7
+
6
8
  module Sqreen
7
9
  module Deliveries
8
10
  # Simple delivery method that batch event already seen in a batch
@@ -41,22 +43,23 @@ module Sqreen
41
43
  def stale?
42
44
  min = @first_seen.values.min
43
45
  return false if min.nil?
44
- (min + max_staleness) < Time.now
46
+ (min + max_staleness) < Sqreen.time
45
47
  end
46
48
 
47
49
  def post_batch_needed?(event)
48
- now = Time.now
50
+ now = Sqreen.time
51
+ # do not use any? {} due to side effects inside block
49
52
  event_keys(event).map do |key|
50
53
  was = @first_seen[key]
51
54
  @first_seen[key] ||= now
52
- was.nil? || current_batch.size > max_batch || (was + max_staleness) < now
55
+ was.nil? || current_batch.size > max_batch || now > (was + max_staleness)
53
56
  end.any?
54
57
  end
55
58
 
56
59
  def post_batch
57
60
  session.post_batch(current_batch)
58
61
  current_batch.clear
59
- now = Time.now
62
+ now = Sqreen.time
60
63
  @first_seen.each_key do |key|
61
64
  @first_seen[key] = now
62
65
  end
@@ -5,6 +5,8 @@ require 'sqreen/shared_storage'
5
5
  require 'sqreen/events/request_record'
6
6
  require 'sqreen/performance_notifications/log_performance'
7
7
  require 'sqreen/performance_notifications/newrelic'
8
+ require 'sqreen/log'
9
+ require 'sqreen/runner'
8
10
 
9
11
  module Sqreen
10
12
  # Store event/observations that happened in this request
@@ -12,6 +12,7 @@ require 'sqreen/shared_storage'
12
12
  require 'sqreen/rules_callbacks/record_request_context'
13
13
  require 'sqreen/rules_callbacks/run_req_start_actions'
14
14
  require 'sqreen/rules_callbacks/run_block_user_actions'
15
+ require 'sqreen/mono_time'
15
16
  require 'set'
16
17
 
17
18
  # How to override a class method:
@@ -37,6 +38,11 @@ require 'set'
37
38
  module Sqreen
38
39
  class Instrumentation
39
40
  OVERTIME_METRIC = 'request_overtime'.freeze
41
+
42
+ PRE_CB = 'pre'.freeze
43
+ POST_CB = 'post'.freeze
44
+ FAILING_CB = 'failing'.freeze
45
+
40
46
  MGMT_COST = 0.000025
41
47
  @@override_semaphore = Mutex.new
42
48
  @@overriden_singleton_methods = false
@@ -64,7 +70,7 @@ module Sqreen
64
70
  @@overriden_methods
65
71
  end
66
72
  def self.callback_wrapper_pre(callbacks, framework, budget, _klass, method, instance, args, &block)
67
- all_start = Sqreen::PerformanceNotifications.time
73
+ all_start = Sqreen.time
68
74
  #Instrumentation.guard_call(method, []) do
69
75
  returns = []
70
76
  callbacks.each do |cb|
@@ -76,9 +82,9 @@ module Sqreen
76
82
  end
77
83
  Sqreen.log.debug { "running pre cb #{cb}" }
78
84
  begin
79
- start = Sqreen::PerformanceNotifications.time
85
+ start = Sqreen.time
80
86
  res = cb.pre(instance, args, budget, &block)
81
- stop = Sqreen::PerformanceNotifications.time
87
+ stop = Sqreen.time
82
88
  # The first few pre callbacks could not have a request & hence a budget just yet so we try harder to find it
83
89
  budget = framework.remaining_perf_budget if framework && !budget && Sqreen.performance_budget
84
90
  if budget
@@ -106,21 +112,21 @@ module Sqreen
106
112
  end
107
113
  next
108
114
  end
109
- Sqreen::PerformanceNotifications.notify("Callbacks/#{rule || cb.class.name}/pre", start, stop)
115
+ Sqreen::PerformanceNotifications.notify(rule || cb.class.name, PRE_CB, start, stop)
110
116
  end
111
- all_stop = Sqreen::PerformanceNotifications.time
117
+ all_stop = Sqreen.time
112
118
  framework.remaining_perf_budget = budget - (all_stop - all_start) - MGMT_COST * callbacks.size if framework && budget
113
- Sqreen::PerformanceNotifications.notify('Callbacks/hooks_pre/pre', all_start, all_stop)
119
+ Sqreen::PerformanceNotifications.notify('hooks_pre', PRE_CB, all_start, all_stop)
114
120
  returns
115
121
  #end
116
122
  rescue StandardError => e
117
- Sqreen.log.warn { "we catched an exception between cbs: #{e.inspect}" }
123
+ Sqreen.log.warn { "we caught an exception between cbs: #{e.inspect}" }
118
124
  Sqreen::RemoteException.record(e)
119
125
  []
120
126
  end
121
127
 
122
128
  def self.callback_wrapper_post(callbacks, framework, budget, _klass, method, return_val, instance, args, &block)
123
- all_start = Sqreen::PerformanceNotifications.time
129
+ all_start = Sqreen.time
124
130
  #Instrumentation.guard_call(method, []) do
125
131
  returns = []
126
132
  callbacks.reverse_each do |cb|
@@ -131,9 +137,9 @@ module Sqreen
131
137
  end
132
138
  Sqreen.log.debug { "running post cb #{cb}" }
133
139
  begin
134
- start = Sqreen::PerformanceNotifications.time
140
+ start = Sqreen.time
135
141
  res = cb.post(return_val, instance, args, budget, &block)
136
- stop = Sqreen::PerformanceNotifications.time
142
+ stop = Sqreen.time
137
143
  if budget
138
144
  budget -= (stop - start)
139
145
  cb.overtime! if budget <= 0.0
@@ -158,23 +164,23 @@ module Sqreen
158
164
  end
159
165
  next
160
166
  end
161
- Sqreen::PerformanceNotifications.notify("Callbacks/#{rule || cb.class.name}/post", start, stop)
167
+ Sqreen::PerformanceNotifications.notify(rule || cb.class.name, POST_CB, start, stop)
162
168
  end
163
- all_stop = Sqreen::PerformanceNotifications.time
169
+ all_stop = Sqreen.time
164
170
  if framework && budget && framework.remaining_perf_budget
165
171
  framework.remaining_perf_budget = budget - (all_stop - all_start) - MGMT_COST * callbacks.size
166
172
  end
167
- Sqreen::PerformanceNotifications.notify('Callbacks/hooks_post/post', all_start, all_stop)
173
+ Sqreen::PerformanceNotifications.notify('hooks_post', POST_CB, all_start, all_stop)
168
174
  returns
169
175
  #end
170
176
  rescue StandardError => e
171
- Sqreen.log.warn { "we catched an exception between cbs: #{e.inspect}" }
177
+ Sqreen.log.warn { "we caught an exception between cbs: #{e.inspect}" }
172
178
  Sqreen::RemoteException.record(e)
173
179
  []
174
180
  end
175
181
 
176
182
  def self.callback_wrapper_failing(callbacks, framework, budget, exception, _klass, method, instance, args, &block)
177
- all_start = Sqreen::PerformanceNotifications.time
183
+ all_start = Sqreen.time
178
184
  #Instrumentation.guard_call(method, []) do
179
185
  returns = []
180
186
  callbacks.each do |cb|
@@ -185,9 +191,9 @@ module Sqreen
185
191
  end
186
192
  Sqreen.log.debug { "running failing cb #{cb}" }
187
193
  begin
188
- start = Sqreen::PerformanceNotifications.time
194
+ start = Sqreen.time
189
195
  res = cb.failing(exception, instance, args, budget, &block)
190
- stop = Sqreen::PerformanceNotifications.time
196
+ stop = Sqreen.time
191
197
  if budget
192
198
  budget -= (stop - start)
193
199
  cb.overtime! if budget <= 0.0
@@ -212,17 +218,17 @@ module Sqreen
212
218
  end
213
219
  next
214
220
  end
215
- Sqreen::PerformanceNotifications.notify("Callbacks/#{rule || cb.class.name}/failing", start, stop)
221
+ Sqreen::PerformanceNotifications.notify(rule || cb.class.name, FAILING_CB, start, stop)
216
222
  end
217
- all_stop = Sqreen::PerformanceNotifications.time
223
+ all_stop = Sqreen.time
218
224
  if framework && budget && framework.remaining_perf_budget
219
225
  framework.remaining_perf_budget = budget - (all_stop - all_start) - MGMT_COST * callbacks.size
220
226
  end
221
- Sqreen::PerformanceNotifications.notify('Callbacks/hooks_failing/failing', all_start, all_stop)
227
+ Sqreen::PerformanceNotifications.notify('hooks_failing', FAILING_CB, all_start, all_stop)
222
228
  returns
223
229
  # end
224
230
  rescue StandardError => e
225
- Sqreen.log.warn "we catched an exception between cbs: #{e.inspect}"
231
+ Sqreen.log.warn "we caught an exception between cbs: #{e.inspect}"
226
232
  Sqreen::RemoteException.record(e)
227
233
  []
228
234
  end
@@ -246,11 +252,8 @@ module Sqreen
246
252
 
247
253
  def self.define_callback_method(meth, original_meth, klass_name)
248
254
  @sqreen_multi_instr ||= nil
249
- proc do |*args, &block|
250
- record_req_hp = @@record_request_hookpoints.include?([klass_name, meth]) &&
251
- Sqreen::PerformanceNotifications.listen_for?
252
- Sqreen::PerformanceNotifications::BinnedMetrics.start_request if record_req_hp
253
255
 
256
+ proc do |*args, &block|
254
257
  budget = nil
255
258
  skip_call = Thread.current[:sqreen_in_use]
256
259
  begin
@@ -386,16 +389,28 @@ module Sqreen
386
389
  end
387
390
  result
388
391
  ensure
389
- if record_req_hp
390
- Sqreen::PerformanceNotifications.instrument('Callbacks/hooks_reporting/pre') do
391
- Sqreen::PerformanceNotifications::LogPerformance.next_request
392
- Sqreen::PerformanceNotifications::NewRelic.next_request
393
- Sqreen::PerformanceNotifications::BinnedMetrics.finish_request
394
- end
395
- end
396
392
  Thread.current[:sqreen_in_use] = false
397
393
  end
398
394
  end
395
+ end # end of proc
396
+ end
397
+
398
+ def self.request_hookpoint_method(original_meth)
399
+ proc do |*args, &block|
400
+ has_notifications = Sqreen::PerformanceNotifications.listen_for?
401
+ Sqreen::PerformanceNotifications::BinnedMetrics.start_request if has_notifications
402
+
403
+ begin
404
+ send(original_meth, *args, &block)
405
+ ensure
406
+ if has_notifications
407
+ Sqreen::PerformanceNotifications.instrument('next_req_notifs', PRE_CB) do
408
+ Sqreen::PerformanceNotifications::LogPerformance.next_request
409
+ Sqreen::PerformanceNotifications::NewRelic.next_request
410
+ Sqreen::PerformanceNotifications::BinnedMetrics.finish_request
411
+ end
412
+ end
413
+ end
399
414
  end
400
415
  end
401
416
 
@@ -477,6 +492,12 @@ module Sqreen
477
492
 
478
493
  define_method(new_method, p)
479
494
 
495
+ if @@record_request_hookpoints.include?([klass_name, meth])
496
+ p = Instrumentation.request_hookpoint_method(new_method)
497
+ new_method = "#{new_method}_req_hp_wrapper"
498
+ define_method(new_method, p)
499
+ end
500
+
480
501
  if public_method_defined?(meth)
481
502
  method_kind = :public
482
503
  elsif protected_method_defined?(meth)
@@ -569,6 +590,8 @@ module Sqreen
569
590
  method = cb.method
570
591
  key = [klass, method]
571
592
 
593
+ @@record_request_hookpoints << key if cb.is_a?(Sqreen::Rules::RecordRequestContext)
594
+
572
595
  already_overriden = @@overriden_methods.include? key
573
596
 
574
597
  if !already_overriden
@@ -610,7 +633,6 @@ module Sqreen
610
633
 
611
634
  @@registered_callbacks.add(cb)
612
635
  @@unovertimable_hookpoints << key unless cb.overtimeable
613
- @@record_request_hookpoints << key if cb.is_a?(Sqreen::Rules::RecordRequestContext)
614
636
  @@instrumented_pid = Process.pid
615
637
  end
616
638
  end
@@ -9,7 +9,7 @@ module Sqreen
9
9
  class Average < Base
10
10
  # from class attr_accessor :aggregate
11
11
 
12
- def update(_at, key, value)
12
+ def update(key, value)
13
13
  super
14
14
  @sums[key] ||= 0
15
15
  @sums[key] += value
@@ -18,12 +18,12 @@ module Sqreen
18
18
  # @param _at [Time] when was the observation made
19
19
  # @param _key [String] which aggregation key was it made for
20
20
  # @param _value [Object] The observation
21
- def update(_at, _key, _value)
21
+ def update(_key, _value)
22
22
  raise Sqreen::Exception, 'No current sample' unless @sample
23
23
  end
24
24
 
25
25
  # create a new empty sample and publish the last one
26
- # @param time [Time] Time of start/finish
26
+ # @param time [Float] Time of start of new sample/end of the last one
27
27
  def next_sample(time)
28
28
  finalize_sample(time) unless @sample.nil?
29
29
  current_sample = @sample
@@ -33,10 +33,12 @@ module Sqreen
33
33
 
34
34
  protected
35
35
 
36
+ # @param time [Float]
36
37
  def new_sample(time)
37
38
  @sample = { OBSERVATION_KEY => {}, START_KEY => time }
38
39
  end
39
40
 
41
+ # @param time [Float]
40
42
  def finalize_sample(time)
41
43
  @sample[FINISH_KEY] = time
42
44
  end
@@ -1,6 +1,7 @@
1
1
  # Copyright (c) 2018 Sqreen. All Rights Reserved.
2
2
  # Please refer to our terms for more information: https://www.sqreen.io/terms.html
3
3
 
4
+ require 'sqreen/mono_time'
4
5
  require 'sqreen/metrics/base'
5
6
 
6
7
  module Sqreen
@@ -20,10 +21,10 @@ module Sqreen
20
21
  log_factor = Math.log(@factor)
21
22
  @inv_log_base = 1 / log_base
22
23
  @add_parcel = - log_factor / log_base
23
- new_sample(Time.now.utc)
24
+ new_sample(Sqreen.time)
24
25
  end
25
26
 
26
- def update(_at, _key, x)
27
+ def update(_key, x)
27
28
  h = @sample[OBSERVATION_KEY]
28
29
  bin = bin_no(x)
29
30
  h[bin] += 1
@@ -11,7 +11,7 @@ module Sqreen
11
11
  class Collect < Base
12
12
  # from class attr_accessor :aggregate
13
13
 
14
- def update(_at, key, value)
14
+ def update(key, value)
15
15
  super
16
16
  s = @sample[OBSERVATION_KEY]
17
17
  s[key] ||= []
@@ -9,7 +9,7 @@ module Sqreen
9
9
  class Sum < Base
10
10
  # from class attr_accessor :aggregate
11
11
 
12
- def update(_at, key, value)
12
+ def update(key, value)
13
13
  super
14
14
  s = @sample[OBSERVATION_KEY]
15
15
  s[key] ||= 0
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'sqreen/exception'
5
5
  require 'sqreen/metrics'
6
+ require 'sqreen/mono_time'
6
7
 
7
8
  module Sqreen
8
9
  # This store and register metrics
@@ -17,6 +18,7 @@ module Sqreen
17
18
  class AlreadyRegisteredMetric < Sqreen::Exception
18
19
  end
19
20
 
21
+ # definition keys
20
22
  NAME_KEY = 'name'.freeze
21
23
  KIND_KEY = 'kind'.freeze
22
24
  PERIOD_KEY = 'period'.freeze
@@ -29,7 +31,7 @@ module Sqreen
29
31
 
30
32
  def initialize
31
33
  @store = []
32
- @metrics = {}
34
+ @metrics = {} # name => (metric, period, start)
33
35
  end
34
36
 
35
37
  # Definition contains a name,period and aggregate at least
@@ -54,17 +56,17 @@ module Sqreen
54
56
  @metrics.key?(name)
55
57
  end
56
58
 
59
+ # @params at [Time] when is the store emptied
57
60
  def update(name, at, key, value)
58
- at = at.utc
59
61
  metric, period, start = @metrics[name]
60
62
  raise UnregisteredMetric, "Unknown metric #{name}" unless metric
61
63
  next_sample(name, at) if start.nil? || (start + period) < at
62
- metric.update(at, key, value)
64
+ metric.update(key, value)
63
65
  end
64
66
 
65
67
  # Drains every metrics and returns the store content
66
68
  # @params at [Time] when is the store emptied
67
- def publish(flush = true, at = Time.now.utc)
69
+ def publish(flush = true, at = Sqreen.time)
68
70
  @metrics.each do |name, (_, period, start)|
69
71
  next_sample(name, at) if flush || !start.nil? && (start + period) < at
70
72
  end
@@ -78,10 +80,13 @@ module Sqreen
78
80
  def next_sample(name, at)
79
81
  metric = @metrics[name][0]
80
82
  r = metric.next_sample(at)
81
- @metrics[name][2] = at # start
83
+ @metrics[name][2] = at # new start
82
84
  if r
83
85
  r[NAME_KEY] = name
84
86
  obs = r[Metric::OBSERVATION_KEY]
87
+ start_of_mono = Time.now.utc - Sqreen.time
88
+ r[Metric::START_KEY] = start_of_mono + r[Metric::START_KEY]
89
+ r[Metric::FINISH_KEY] = start_of_mono + r[Metric::FINISH_KEY]
85
90
  @store << r if obs && (!obs.respond_to?(:empty?) || !obs.empty?)
86
91
  end
87
92
  r
@@ -0,0 +1,18 @@
1
+ module Sqreen
2
+ has_mono_time = begin
3
+ Process.clock_gettime Process::CLOCK_MONOTONIC
4
+ true
5
+ rescue StandardError
6
+ false
7
+ end
8
+
9
+ if has_mono_time
10
+ def self.time
11
+ Process.clock_gettime Process::CLOCK_MONOTONIC
12
+ end
13
+ else
14
+ def self.time
15
+ Time.now.to_f
16
+ end
17
+ end
18
+ end
@@ -1,12 +1,7 @@
1
1
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
2
  # Please refer to our terms for more information: https://www.sqreen.io/terms.html
3
3
 
4
- begin
5
- Process.clock_gettime Process::CLOCK_MONOTONIC
6
- SQREEN_MONO_TIME = Process::CLOCK_MONOTONIC
7
- rescue StandardError
8
- SQREEN_MONO_TIME = nil
9
- end
4
+ require 'sqreen/mono_time'
10
5
 
11
6
  module Sqreen
12
7
  # This module enable us to keep track of sqreen resource usage
@@ -17,39 +12,29 @@ module Sqreen
17
12
  @subscriptions_all = {}
18
13
  @subscription_id = 0
19
14
  class << self
20
- # Subsribe to receive notificiations about an event
21
- # returns a subscription indentitifcation
15
+ # Subscribe to receive notifications about an event
16
+ # returns a subscription identification
22
17
  def subscribe(&block)
23
18
  id = (@subscription_id += 1)
24
19
  @subscriptions_all[id] = block
25
20
  id
26
21
  end
27
22
 
28
- if SQREEN_MONO_TIME
29
- def time
30
- Process.clock_gettime(SQREEN_MONO_TIME)
31
- end
32
- else
33
- def time
34
- Time.now
35
- end
36
- end
37
-
38
23
  # Is there a subscriber
39
24
  def listen_for?
40
25
  !@subscriptions_all.empty?
41
26
  end
42
27
 
43
28
  # Instrument a call identified by key
44
- def instrument(key, meta = {}, &block)
29
+ def instrument(rule, cb, meta = {}, &block)
45
30
  return yield unless listen_for?
46
- _instrument(key, meta, &block)
31
+ _instrument(rule, cb, meta, &block)
47
32
  end
48
33
 
49
- def notify(key, start, stop, meta = {})
34
+ def notify(rule, cb, start, stop, meta = {})
50
35
  return unless listen_for?
51
36
  notifiers.each do |callable|
52
- callable.call(key, start, stop, meta)
37
+ callable.call(rule, cb, start, stop, meta)
53
38
  end
54
39
  end
55
40
 
@@ -70,22 +55,12 @@ module Sqreen
70
55
  @subscriptions_all.values
71
56
  end
72
57
 
73
- if SQREEN_MONO_TIME
74
- def _instrument(key, meta)
75
- start = Process.clock_gettime(SQREEN_MONO_TIME)
76
- yield
77
- ensure
78
- stop = Process.clock_gettime(SQREEN_MONO_TIME)
79
- notify(key, start, stop, meta)
80
- end
81
- else
82
- def _instrument(key, meta)
83
- start = Time.now
84
- yield
85
- ensure
86
- stop = Time.now
87
- notify(key, start, stop, meta)
88
- end
58
+ def _instrument(rule, cb, meta)
59
+ start = Sqreen.time
60
+ yield
61
+ ensure
62
+ stop = Sqreen.time
63
+ notify(rule, cb, start, stop, meta)
89
64
  end
90
65
  end
91
66
  end
@@ -16,8 +16,6 @@ module Sqreen
16
16
  EVENT_TOTAL_TIME = 'sq'.freeze # sqreen total overhead callback time
17
17
  EVENT_PERCENT = 'pct'.freeze # sqreen total overhead percent of time
18
18
 
19
- EVT_NAME_REGEXP = %r{\ACallbacks/([^/]+)/([^/]+)\z}
20
-
21
19
  # @param metrics_store [Sqreen::MetricsStore]
22
20
  def initialize(metrics_store, period, perf_metric_opts, perf_metric_percent_opts)
23
21
  @metrics_store = metrics_store
@@ -49,32 +47,28 @@ module Sqreen
49
47
  @subid = nil
50
48
  end
51
49
 
52
- def log(event, start, finish, _meta)
53
- return unless event =~ EVT_NAME_REGEXP
54
- rule, cb = Regexp.last_match.captures
55
-
50
+ def log(rule, cb, start, finish, _meta)
56
51
  metric_name = "sq.#{rule}.#{cb}"
57
52
  ensure_metric(metric_name)
58
53
 
59
- finish_time = SQREEN_MONO_TIME ? Time.now.utc : finish
60
54
  time_millis = (finish - start) * 1000
61
55
  # Ensure we always have a timings if we somehow missed the request start
62
- SharedStorage[:sqreen_request_time] ||= 0
63
- SharedStorage[:sqreen_request_time] += time_millis
64
- metrics_store.update(metric_name, finish_time, nil, time_millis)
56
+ SharedStorage[:sqreen_request_time] =
57
+ (SharedStorage[:sqreen_request_time] || 0) + time_millis
58
+ metrics_store.update(metric_name, finish, nil, time_millis)
65
59
  end
66
60
 
67
61
  def start_request
68
- SharedStorage[:request_start_time] = PerformanceNotifications.time
62
+ SharedStorage[:request_start_time] = Sqreen.time
69
63
  SharedStorage[:sqreen_request_time] = 0.0
70
64
  end
71
65
 
72
66
  def finish_request
73
67
  start_time = SharedStorage[:request_start_time]
74
- finish_time = PerformanceNotifications.time
68
+ finish_time = Sqreen.time
75
69
  duration_millis = (finish_time - start_time) * 1000
76
70
 
77
- finish_time_obj = SQREEN_MONO_TIME ? Time.now.utc : finish_time
71
+ finish_time_obj = Time.now.utc
78
72
  # format of evt is [cat, key, value, timestamp]
79
73
  Sqreen.observations_queue.push(
80
74
  [EVENT_REQ, nil, duration_millis, finish_time_obj]
@@ -108,7 +102,11 @@ module Sqreen
108
102
  class << self
109
103
  # @return [Sqreen::PerformanceNotifications::BinnedMetrics]
110
104
  attr_reader :instance
111
- def enable(metrics_store, period = 60, base = DEFAULT_PERF_BASE, factor = DEFAULT_PERF_UNIT, base_pct = DEFAULT_PERF_PCT_BASE, factor_pct = DEFAULT_PERF_PCT_UNIT)
105
+ def enable(metrics_store, period = 60,
106
+ base = DEFAULT_PERF_BASE,
107
+ factor = DEFAULT_PERF_UNIT,
108
+ base_pct = DEFAULT_PERF_PCT_BASE,
109
+ factor_pct = DEFAULT_PERF_PCT_UNIT)
112
110
  disable
113
111
  @instance = new(metrics_store, period,
114
112
  {'base'=> base, 'factor' => factor},
@@ -10,14 +10,19 @@ module Sqreen
10
10
  @subid = nil
11
11
  @facility = nil
12
12
  class << self
13
- def log(event, start, finish, meta)
13
+ def log(rule, cb, start, finish, meta)
14
14
  (@facility || Sqreen.log).debug do
15
15
  meta_str = nil
16
16
  meta_str = ": #{meta.inspect}" unless meta.empty?
17
+ event = event_name(rule, cb)
17
18
  format('%s took %.2fms%s', event, (finish - start) * 1000, meta_str)
18
19
  end
19
20
  end
20
21
 
22
+ def event_name(rule, cb)
23
+ "Callbacks/#{rule}/#{cb}"
24
+ end
25
+
21
26
  def enable(facility = nil)
22
27
  return unless @subid.nil?
23
28
  @facility = facility
@@ -2,6 +2,7 @@
2
2
  # Please refer to our terms for more information: https://www.sqreen.io/terms.html
3
3
 
4
4
  require 'sqreen/performance_notifications'
5
+ require 'sqreen/performance_notifications/log'
5
6
 
6
7
  module Sqreen
7
8
  module PerformanceNotifications
@@ -23,7 +24,8 @@ module Sqreen
23
24
  SharedStorage.set(:log_performance_timings, value)
24
25
  end
25
26
 
26
- def log(event, start, finish, _meta)
27
+ def log(rule, cb, start, finish, _meta)
28
+ event = event_name(rule, cb)
27
29
  timings << [event, start, finish]
28
30
  end
29
31
 
@@ -2,17 +2,20 @@
2
2
  # Please refer to our terms for more information: https://www.sqreen.io/terms.html
3
3
 
4
4
  require 'sqreen/performance_notifications'
5
+ require 'sqreen/performance_notifications/log'
6
+ require 'sqreen/runner'
5
7
 
6
8
  module Sqreen
7
9
  module PerformanceNotifications
8
10
  # Log performances in sqreen metrics_store
9
- class Metrics
11
+ class Metrics < Log
10
12
  @subid = nil
11
13
  @facility = nil
12
14
  class << self
13
15
  EVENT_CAT = 'sqreen_time'.freeze
14
- def log(event, start, finish, _meta)
15
- evt = [EVENT_CAT, event, (finish - start) * 1000, SQREEN_MONO_TIME ? Time.now.utc : finish]
16
+ def log(rule, cb, start, finish, _meta)
17
+ event = event_name(rule, cb)
18
+ evt = [EVENT_CAT, event, (finish - start) * 1000, Time.now.utc]
16
19
  Sqreen.observations_queue.push(evt)
17
20
  end
18
21
 
@@ -2,11 +2,14 @@
2
2
  # Please refer to our terms for more information: https://www.sqreen.io/terms.html
3
3
 
4
4
  require 'sqreen/performance_notifications'
5
+ require 'sqreen/performance_notifications/log'
6
+ require 'sqreen/log'
7
+ require 'sqreen/shared_storage'
5
8
 
6
9
  module Sqreen
7
10
  module PerformanceNotifications
8
11
  # Log performances on the console
9
- class NewRelic
12
+ class NewRelic < Log
10
13
  @subid = nil
11
14
  @level = 0
12
15
 
@@ -24,7 +27,8 @@ module Sqreen
24
27
  SharedStorage.set(:log_performance_nr_timings, value)
25
28
  end
26
29
 
27
- def log(event, start, finish, _meta)
30
+ def log(rule, cb, start, finish, _meta)
31
+ event = event_name(rule, cb)
28
32
  timings << [event, start, finish]
29
33
  end
30
34
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'sqreen/rule_callback'
5
5
  require 'sqreen/binding_accessor'
6
+ require 'sqreen/mono_time'
6
7
  require 'sqreen/rules_callbacks/matcher_rule'
7
8
 
8
9
  module Sqreen
@@ -49,7 +50,7 @@ module Sqreen
49
50
 
50
51
  def pre(inst, args, budget = nil, &_block)
51
52
  unless budget.nil?
52
- finish = budget + Sqreen::PerformanceNotifications.time.to_f
53
+ finish = budget + Sqreen.time
53
54
  end
54
55
  resol_cache = Hash.new do |hash, accessor|
55
56
  hash[accessor] = accessor.resolve(binding, framework, inst, args)
@@ -61,7 +62,7 @@ module Sqreen
61
62
  next unless val.respond_to?(:each)
62
63
  next if val.respond_to?(:seek)
63
64
  val.each do |v|
64
- if !budget.nil? && Sqreen::PerformanceNotifications.time.to_f > finish
65
+ if !budget.nil? && Sqreen.time > finish
65
66
  return nil
66
67
  end
67
68
  next if !v.is_a?(String) || (!matcher.min_size.nil? && v.size < matcher.min_size)
@@ -214,9 +214,10 @@ module Sqreen
214
214
  def config_binned_metrics(level, base, factor, base_pct, factor_pct)
215
215
  level = level.to_i
216
216
  if level <= 0
217
- Sqreen.log.debug('Disabling binned metrics')
217
+ Sqreen.log.info('Disabling binned metrics')
218
218
  PerformanceNotifications::BinnedMetrics.disable
219
219
  else
220
+ Sqreen.log.info('Enabling binned metrics')
220
221
  Sqreen.log.warn("Unknown value for perf_level: #{level}. Treating as 1") unless level == 1
221
222
  PerformanceNotifications::BinnedMetrics.enable(
222
223
  metrics_engine, PERF_METRICS_PERIOD, base.to_f, factor.to_f, base_pct.to_f, factor_pct.to_f
@@ -352,9 +353,10 @@ module Sqreen
352
353
 
353
354
  def aggregate_observations
354
355
  q = Sqreen.observations_queue
356
+ conv = Sqreen.time - Time.now.utc.to_f
355
357
  q.size.times do
356
358
  cat, key, obs, t = q.pop
357
- metrics_engine.update(cat, t, key, obs)
359
+ metrics_engine.update(cat, conv + t.utc.to_f, key, obs)
358
360
  end
359
361
  end
360
362
 
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
2
  # Please refer to our terms for more information: https://www.sqreen.io/terms.html
3
3
  module Sqreen
4
- VERSION = '1.15.4'.freeze
4
+ VERSION = '1.15.5'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqreen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.15.4
4
+ version: 1.15.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sqreen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-14 00:00:00.000000000 Z
11
+ date: 2018-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sq_mini_racer
@@ -75,6 +75,7 @@ files:
75
75
  - lib/sqreen/metrics/sum.rb
76
76
  - lib/sqreen/metrics_store.rb
77
77
  - lib/sqreen/middleware.rb
78
+ - lib/sqreen/mono_time.rb
78
79
  - lib/sqreen/payload_creator.rb
79
80
  - lib/sqreen/performance_notifications.rb
80
81
  - lib/sqreen/performance_notifications/binned_metrics.rb