Class: ScoutApm::DbQueryMetricSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scout_apm/db_query_metric_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ DbQueryMetricSet

Returns a new instance of DbQueryMetricSet.



17
18
19
20
21
22
# File 'lib/scout_apm/db_query_metric_set.rb', line 17

def initialize(context)
  @context = context

  # A hash of DbQueryMetricStats values, keyed by DbQueryMetricStats.key
  @metrics = Hash.new
end

Instance Attribute Details

#metricsObject (readonly)

the raw metrics. You probably want #metrics_to_report



6
7
8
# File 'lib/scout_apm/db_query_metric_set.rb', line 6

def metrics
  @metrics
end

Instance Method Details

#<<(stat) ⇒ Object

Add a single DbQueryMetricStats object to this set.

Looks up an existing one under this key and merges, or just saves a new one under the key



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scout_apm/db_query_metric_set.rb', line 54

def <<(stat)
  existing_stat = metrics[stat.key]
  if existing_stat
    existing_stat.combine!(stat)
  elsif at_limit?
    # We're full up, can't add any more.
    # Should I log this? It may get super noisy?
  else
    metrics[stat.key] = stat
  end
end

#at_limit?Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/scout_apm/db_query_metric_set.rb', line 91

def at_limit?
  @limit ||= context.config.value('database_metric_limit')
  metrics.size >= @limit
end

#combine!(other) ⇒ Object

Take another set, and merge it with this one



43
44
45
46
47
48
# File 'lib/scout_apm/db_query_metric_set.rb', line 43

def combine!(other)
  other.each do |metric|
    self << metric
  end
  self
end

#contextObject

Need to look this up again if we end up as nil. Which I guess can happen after a Marshal load?



26
27
28
# File 'lib/scout_apm/db_query_metric_set.rb', line 26

def context
  @context ||= ScoutApm::Agent.instance.context
end

#eachObject



30
31
32
33
34
# File 'lib/scout_apm/db_query_metric_set.rb', line 30

def each
  metrics.each do |_key, db_query_metric_stat|
    yield db_query_metric_stat
  end
end

#increment_transaction_count!Object



66
67
68
69
70
# File 'lib/scout_apm/db_query_metric_set.rb', line 66

def increment_transaction_count!
  metrics.each do |_key, db_query_metric_stat|
    db_query_metric_stat.increment_transaction_count!
  end
end

#inspectObject



85
86
87
88
89
# File 'lib/scout_apm/db_query_metric_set.rb', line 85

def inspect
  metrics.map {|key, metric|
    "#{key.inspect} - Count: #{metric.call_count}, Total Time: #{"%.2f" % metric.call_time}"
  }.join("\n")
end

#lookup(other) ⇒ Object

Looks up a DbQueryMetricStats instance in the @metrics hash. Sets the value to other if no key Returns a DbQueryMetricStats instance



38
39
40
# File 'lib/scout_apm/db_query_metric_set.rb', line 38

def lookup(other)
  metrics[other.key] ||= other
end

#marshal_dumpObject



8
9
10
# File 'lib/scout_apm/db_query_metric_set.rb', line 8

def marshal_dump
  [ @metrics ]
end

#marshal_load(array) ⇒ Object



12
13
14
15
# File 'lib/scout_apm/db_query_metric_set.rb', line 12

def marshal_load(array)
  @metrics = array.first
  @context = ScoutApm::Agent.instance.context
end

#metrics_to_reportObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/scout_apm/db_query_metric_set.rb', line 72

def metrics_to_report
  report_limit = context.config.value('database_metric_report_limit')
  if metrics.size > report_limit
    metrics.
      values.
      sort_by {|stat| stat.call_time }.
      reverse.
      take(report_limit)
  else
    metrics.values
  end
end