class Sqreen::PerformanceNotifications::BinnedMetrics

Logs callback performance

Constants

DEFAULT_PERF_BASE
DEFAULT_PERF_PCT_BASE
DEFAULT_PERF_PCT_UNIT
DEFAULT_PERF_UNIT
EVENT_PERCENT
EVENT_REQ
EVENT_SQ_THREAD_CPU_PCT
EVENT_TOTAL_TIME

Attributes

instance[R]

@return [Sqreen::PerformanceNotifications::BinnedMetrics]

metrics_store[R]

@return [Sqreen::MetricsStore]

period[R]

Public Class Methods

new(metrics_store, period, perf_metric_opts, perf_metric_percent_opts) click to toggle source

@param metrics_store [Sqreen::MetricsStore]

# File lib/sqreen/performance_notifications/binned_metrics.rb, line 23
def initialize(metrics_store, period, perf_metric_opts, perf_metric_percent_opts)
  @metrics_store = metrics_store
  @period = period
  @subid = nil
  @perf_metric_opts = perf_metric_opts
  @perf_metric_percent_opts = perf_metric_percent_opts
  @clock_time = Sqreen.time
  @watcher_cpu_time = 0
end

Private Class Methods

disable() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 154
def disable
  return unless instance
  instance.disable
  @instance = nil
end
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) click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 142
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)
  disable
  @instance = new(metrics_store, period,
                  {'base'=> base, 'factor' => factor},
                  {'base' => base_pct, 'factor' => factor_pct}
                 ).tap(&:enable)
end
finish_request() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 165
def finish_request
  return unless instance
  instance.finish_request
end
finish_watcher_run() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 170
def finish_watcher_run
  return unless instance
  instance.finish_watcher_run
end
start_request() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 160
def start_request
  return unless instance
  instance.start_request
end

Public Instance Methods

disable() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 55
def disable
  return if @subid.nil?
  Sqreen::PerformanceNotifications.unsubscribe(@subid)
  @subid = nil
end
enable() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 33
def enable
  return unless @subid.nil?

  metrics_store.create_metric(
    'name' => EVENT_REQ, 'period' => period, 'kind' => 'Binning', 'options' => @perf_metric_opts
  )
  metrics_store.create_metric(
    'name' => EVENT_TOTAL_TIME, 'period' => period, 'kind' => 'Binning', 'options' => @perf_metric_opts
  )
  metrics_store.create_metric(
    'name' => EVENT_PERCENT, 'period' => period, 'kind' => 'Binning', 'options' => @perf_metric_percent_opts
  )

  if Sqreen.thread_cpu_time?
    metrics_store.create_metric('name' => EVENT_SQ_THREAD_CPU_PCT,
                                'period' => period, 'kind' => 'Binning',
                                'options' => @perf_metric_percent_opts)
  end

  @subid = Sqreen::PerformanceNotifications.subscribe(&method(:log))
end
finish_request() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 77
def finish_request
  start_time = SharedStorage[:request_start_time]
  finish_time = Sqreen.time
  duration_millis = (finish_time - start_time) * 1000

  finish_time_obj = Time.now.utc
  # format of evt is [cat, key, value, timestamp]
  Sqreen.observations_queue.push(
    [EVENT_REQ, nil, duration_millis, finish_time_obj]
  )
  total_t = SharedStorage[:sqreen_request_time]
  Sqreen.observations_queue.push(
    [EVENT_TOTAL_TIME, nil,
     total_t, finish_time_obj]
  )
  return if !duration_millis or total_t >= duration_millis
  Sqreen.observations_queue.push(
    [EVENT_PERCENT, nil,
     (total_t*100.0)/(duration_millis-total_t), finish_time_obj]
  )
end
finish_watcher_run() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 99
def finish_watcher_run
  return unless Sqreen.thread_cpu_time?
  new_clock_time = Sqreen.time
  # collect observation at min 30 second intervals so it's nicely averaged
  return if new_clock_time - @clock_time < 30.0

  clock_time_before = @clock_time
  watcher_cpu_time_before = @watcher_cpu_time
  @clock_time = new_clock_time
  @watcher_cpu_time = Sqreen.thread_cpu_time

  clock_time_diff = @clock_time - clock_time_before
  watcher_cpu_diff = @watcher_cpu_time - watcher_cpu_time_before

  Sqreen.observations_queue.push(
    [EVENT_SQ_THREAD_CPU_PCT, nil,
     (watcher_cpu_diff * 100.0) / clock_time_diff, Time.now.utc]
  )
end
log(rule, cb, start, finish, _meta) click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 61
def log(rule, cb, start, finish, _meta)
  metric_name = "sq.#{rule}.#{cb}"
  ensure_metric(metric_name)

  time_millis = (finish - start) * 1000
  # Ensure we always have a timings if we somehow missed the request start
  SharedStorage[:sqreen_request_time] =
    (SharedStorage[:sqreen_request_time] || 0) + time_millis
  metrics_store.update(metric_name, finish, nil, time_millis)
end
start_request() click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 72
def start_request
  SharedStorage[:request_start_time] = Sqreen.time
  SharedStorage[:sqreen_request_time] = 0.0
end

Private Instance Methods

ensure_metric(metric_name, rule = nil) click to toggle source
# File lib/sqreen/performance_notifications/binned_metrics.rb, line 125
def ensure_metric(metric_name, rule = nil)
  return if metrics_store.metric?(metric_name)
  metrics_store.create_metric(
    {
      'name' => metric_name,
      'period' => period,
      'kind' => 'Binning',
      'options' => @perf_metric_opts,
    },
    rule
  )
end