Class: ScoutApm::DbQueryMetricStats

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/db_query_metric_stats.rb

Constant Summary collapse

DEFAULT_HISTOGRAM_SIZE =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name, operation, scope, call_count, call_time, rows_returned) ⇒ DbQueryMetricStats

Returns a new instance of DbQueryMetricStats.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/scout_apm/db_query_metric_stats.rb', line 24

def initialize(model_name, operation, scope, call_count, call_time, rows_returned)
  @model_name = model_name
  @operation = operation

  @call_count = call_count

  @call_time = call_time
  @min_call_time = call_time
  @max_call_time = call_time

  @rows_returned = rows_returned
  @min_rows_returned = rows_returned
  @max_rows_returned = rows_returned

  # Should we have a histogram for timing, and one for rows_returned?
  # This histogram is for call_time
  @histogram = NumericHistogram.new(DEFAULT_HISTOGRAM_SIZE)
  @histogram.add(call_time)

  @transaction_count = 0

  @scope = scope
end

Instance Attribute Details

#call_countObject (readonly)

Returns the value of attribute call_count.



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

def call_count
  @call_count
end

#call_timeObject (readonly)

Returns the value of attribute call_time.



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

def call_time
  @call_time
end

#histogramObject (readonly)

Returns the value of attribute histogram.



22
23
24
# File 'lib/scout_apm/db_query_metric_stats.rb', line 22

def histogram
  @histogram
end

#max_call_timeObject (readonly)

Returns the value of attribute max_call_time.



17
18
19
# File 'lib/scout_apm/db_query_metric_stats.rb', line 17

def max_call_time
  @max_call_time
end

#max_rows_returnedObject (readonly)

Returns the value of attribute max_rows_returned.



20
21
22
# File 'lib/scout_apm/db_query_metric_stats.rb', line 20

def max_rows_returned
  @max_rows_returned
end

#min_call_timeObject (readonly)

Returns the value of attribute min_call_time.



16
17
18
# File 'lib/scout_apm/db_query_metric_stats.rb', line 16

def min_call_time
  @min_call_time
end

#min_rows_returnedObject (readonly)

Returns the value of attribute min_rows_returned.



19
20
21
# File 'lib/scout_apm/db_query_metric_stats.rb', line 19

def min_rows_returned
  @min_rows_returned
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



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

def model_name
  @model_name
end

#operationObject (readonly)

Returns the value of attribute operation.



7
8
9
# File 'lib/scout_apm/db_query_metric_stats.rb', line 7

def operation
  @operation
end

#rows_returnedObject (readonly)

Returns the value of attribute rows_returned.



14
15
16
# File 'lib/scout_apm/db_query_metric_stats.rb', line 14

def rows_returned
  @rows_returned
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

#transaction_countObject (readonly)

Returns the value of attribute transaction_count.



10
11
12
# File 'lib/scout_apm/db_query_metric_stats.rb', line 10

def transaction_count
  @transaction_count
end

Instance Method Details

#as_jsonObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/scout_apm/db_query_metric_stats.rb', line 72

def as_json
  json_attributes = [
    :model_name,
    :operation,
    :scope,

    :transaction_count,
    :call_count,

    :histogram,
    :call_time,
    :max_call_time,
    :min_call_time,

    :max_rows_returned,
    :min_rows_returned,
    :rows_returned,
  ]

  ScoutApm::AttributeArranger.call(self, json_attributes)
end

#combine!(other) ⇒ Object

Combine data from another DbQueryMetricStats into self. Modifies and returns self



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/scout_apm/db_query_metric_stats.rb', line 54

def combine!(other)
  return self if other == self

  @transaction_count += other.transaction_count
  @call_count += other.call_count
  @rows_returned += other.rows_returned
  @call_time += other.call_time

  @min_call_time = other.min_call_time if @min_call_time.zero? or other.min_call_time < @min_call_time
  @max_call_time = other.max_call_time if other.max_call_time > @max_call_time

  @min_rows_returned = other.min_rows_returned if @min_rows_returned.zero? or other.min_rows_returned < @min_rows_returned
  @max_rows_returned = other.max_rows_returned if other.max_rows_returned > @max_rows_returned

  @histogram.combine!(other.histogram)
  self
end

#increment_transaction_count!Object

Called by the Set on each DbQueryMetricStats object that it holds, only once during the recording of a transaction.

Don’t call elsewhere, and don’t set to 1 in the initializer.



98
99
100
# File 'lib/scout_apm/db_query_metric_stats.rb', line 98

def increment_transaction_count!
  @transaction_count += 1
end

#keyObject

Merge data in this scope. Used in DbQueryMetricSet



49
50
51
# File 'lib/scout_apm/db_query_metric_stats.rb', line 49

def key
  @key ||= [model_name, operation, scope]
end