Module: ScoutApm::Serializers::PayloadSerializerToJson

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

Class Method Summary collapse

Class Method Details

.escape(string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 34

def escape(string)
  string = string.to_s
  escapers = {
    '\b' => '\\b',
    '\t' => '\\t',
    '\n' => '\\n',
    '\f' => '\\f',
    '\r' => '\\r',
    '"'  => '\\"',
    '\\' => '\\\\'
  }
  # TODO escape control chars
  escapers.each {|bad, good| string = string.gsub(bad, good)}
  string
end

.format_by_type(formatee) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 50

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 nil
    "null"
  else # strings and everything
    %Q["#{escape(formatee)}"]
  end
end

.jsonify_hash(hash) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 24

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



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

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



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

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) ⇒ Object



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

def serialize(, metrics, slow_transactions)
  rearranged_metrics = rearrange_the_metrics(metrics)
  rearranged_slow_transactions = rearrange_the_slow_transactions(slow_transactions)
  .merge!({:payload_version => 2})
  jsonify_hash({:metadata => , :metrics => rearranged_metrics, :slow_transactions => rearranged_slow_transactions})
end