class ScoutApm::Serializers::MetricsToJsonSerializer

Attributes

metrics[R]

Public Class Methods

new(metrics) click to toggle source

A hash of meta => stat pairs

# File lib/scout_apm/serializers/metrics_to_json_serializer.rb, line 7
def initialize(metrics)
  @metrics = metrics
end

Public Instance Methods

as_json() click to toggle source
# 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={}) click to toggle source

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

# 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) click to toggle source
# 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