Class: Sqreen::PerformanceNotifications::NewRelic

Inherits:
Log
  • Object
show all
Defined in:
lib/sqreen/performance_notifications/newrelic.rb

Overview

Log performances on the console

Class Method Summary collapse

Methods inherited from Log

event_name

Class Method Details

.disableObject



88
89
90
91
92
93
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 88

def disable
  return if @subid.nil?
  Sqreen::PerformanceNotifications.unsubscribe(@subid)
  @subid = nil
  @level = 0
end

.enable(level = 0) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 78

def enable(level = 0)
  return unless @subid.nil?
  return unless defined?(::NewRelic::Agent)
  return unless ::NewRelic::Agent.respond_to?(:add_custom_attributes) || ::NewRelic::Agent.respond_to?(:add_custom_parameters)
  return unless level > 0
  @level = level
  Sqreen.log.debug('Enabling New Relic reporting')
  @subid = Sqreen::PerformanceNotifications.subscribe(&method(:log))
end

.enabled?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 37

def enabled?
  !@subid.nil?
end

.log(rule, cb, start, finish, _meta) ⇒ Object



32
33
34
35
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 32

def log(rule, cb, start, finish, _meta)
  event = event_name(rule, cb)
  timings << [event, start, finish]
end

.next_requestObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 48

def next_request
  return unless enabled?
  if @level == 1
    overhead = timings.map do |_evt, start, finish|
      (finish - start) * 1000
    end.inject(0, &:+)
    report('sqreen_time' => overhead)
  else
    output = timings.map do |evt, start, finish|
      [evt.split('/')[1], (finish - start) * 1000]
    end
    total = 0
    count = 0
    rules = output.inject({}) do |acc, (e, t)|
      tt, cc = (acc[e] || [0, 0])
      acc[e] = [tt + t, cc + 1]
      total += t
      count += 1
      acc
    end
    attrs = rules.inject('sqreen_time' => total, 'sqreen_count' => count) do |acc, (rule, values)|
      acc["sqreen_#{rule}_time"] = values[0]
      acc["sqreen_#{rule}_count"] = values[1]
      acc
    end
    report(attrs)
  end
  self.timings = []
end

.report(hash) ⇒ Object



41
42
43
44
45
46
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 41

def report(hash)
  if ::NewRelic::Agent.respond_to?(:add_custom_attributes)
    return ::NewRelic::Agent.add_custom_attributes(hash)
  end
  ::NewRelic::Agent.add_custom_parameters(:user_id => @user.id)
end

.timingsObject



19
20
21
22
23
24
25
26
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 19

def timings
  v = SharedStorage.get(:log_performance_nr_timings)
  if v.nil?
    v = []
    self.timings = v
  end
  v
end

.timings=(value) ⇒ Object



28
29
30
# File 'lib/sqreen/performance_notifications/newrelic.rb', line 28

def timings=(value)
  SharedStorage.set(:log_performance_nr_timings, value)
end