Module: ScoutApm::Serializers::PayloadSerializerToJson

Defined in:
lib/scout_apm/serializers/payload_serializer_to_json.rb

Constant Summary collapse

ESCAPE_MAPPINGS =
{
  "\b" => '\\b',
  "\t" => '\\t',
  "\n" => '\\n',
  "\f" => '\\f',
  "\r" => '\\r',
  '"'  => '\\"',
  '\\' => '\\\\',
}

Class Method Summary collapse

Class Method Details

.escape(string) ⇒ Object



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

def escape(string)
  ESCAPE_MAPPINGS.inject(string.to_s) {|s, (bad, good)| s.gsub(bad, good) }
end

.format_by_type(formatee) ⇒ Object



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

def format_by_type(formatee)
  case formatee
  when Hash
    jsonify_hash(formatee)
  when Array
    all_the_elements = formatee.map {|value_guy| format_by_type(value_guy)}
    "[#{all_the_elements.join(",")}]"
  when Numeric
    formatee
  when Time
    %Q["#{formatee.iso8601}"]
  when nil
    "null"
  else # strings and everything
    %Q["#{escape(formatee)}"]
  end
end

.jsonify_hash(hash) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 29

def jsonify_hash(hash)
  str_parts = []
  hash.each do |key, value|
    formatted_key = format_by_type(key)
    formatted_value = format_by_type(value)
    str_parts << "#{formatted_key}:#{formatted_value}"
  end
  "{#{str_parts.join(",")}}"
end

.rearrange_the_metrics(metrics) ⇒ Object

Old style of metric serializing.



17
18
19
20
21
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 17

def rearrange_the_metrics(metrics)
  metrics.to_a.map do |meta, stats|
    stats.as_json.merge(:key => meta.as_json)
  end
end

.rearrange_the_slow_transactions(slow_transactions) ⇒ Object



23
24
25
26
27
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 23

def rearrange_the_slow_transactions(slow_transactions)
  slow_transactions.to_a.map do |slow_t|
    slow_t.as_json.merge(:metrics => rearrange_the_metrics(slow_t.metrics))
  end
end

.serialize(metadata, metrics, slow_transactions, jobs, slow_jobs) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 5

def serialize(, metrics, slow_transactions, jobs, slow_jobs)
  .merge!({:payload_version => 2})

  jsonify_hash({:metadata => ,
                :metrics => rearrange_the_metrics(metrics),
                :slow_transactions => rearrange_the_slow_transactions(slow_transactions),
                :jobs => JobsSerializerToJson.new(jobs).as_json,
                :slow_jobs => SlowJobsSerializerToJson.new(slow_jobs).as_json,
              })
end