Class: ScoutApm::Serializers::MetricsToJsonSerializer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metrics) ⇒ MetricsToJsonSerializer

A hash of meta => stat pairs



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

def initialize(metrics)
  @metrics = metrics
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



4
5
6
# File 'lib/scout_apm/serializers/metrics_to_json_serializer.rb', line 4

def metrics
  @metrics
end

Instance Method Details

#as_jsonObject



11
12
13
14
15
16
17
# File 'lib/scout_apm/serializers/metrics_to_json_serializer.rb', line 11

def as_json
  if metrics
    metrics.map{|meta, stat| metric_as_json(meta, stat) }
  else
    nil
  end
end

#metric_as_json(meta, stat, child_metrics = {}) ⇒ Object

Children metrics is a hash of meta=>stat pairs. Leave empty for no children. Supports only a single-level nesting, until we have redone metric classes, instead of Meta and Stats



22
23
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/serializers/metrics_to_json_serializer.rb', line 22

def metric_as_json(meta, stat, child_metrics={})
  { "bucket" => meta.type,
    "name" => meta.name, # No scope values needed here, since it's implied by the nesting.

    "count" => stat.call_count,
    "total_call_time" => stat.total_call_time,
    "total_exclusive_time" => stat.total_exclusive_time,
    "min_call_time" => stat.min_call_time,
    "max_call_time" => stat.max_call_time,

    # Pretty unsure how to synthesize histograms out of what we store now
    "total_histogram" => [
      [stat.total_exclusive_time / stat.call_count, stat.call_count],
    ],
    "exclusive_histogram" => [
      [stat.total_exclusive_time / stat.call_count, stat.call_count]
    ],

    "metrics" => transform_child_metrics(child_metrics),

    # Will later hold the exact SQL, or URL or whatever other detail
    # about this query is necessary
    "detail" => { :desc => meta.desc }.merge(meta.extra || {}),
  }
end

#transform_child_metrics(metrics) ⇒ Object



48
49
50
51
52
# File 'lib/scout_apm/serializers/metrics_to_json_serializer.rb', line 48

def transform_child_metrics(metrics)
  metrics.map do |meta, stat|
    metric_as_json(meta, stat)
  end
end