Module: ScoutApm::Serializers::PayloadSerializerToJson
- Defined in:
- lib/scout_apm/serializers/payload_serializer_to_json.rb
Constant Summary collapse
- ESCAPE_MAPPINGS =
{ # Stackoverflow answer on gsub matches and backslashes # https://stackoverflow.com/a/4149087/2705125 '\\' => '\\\\\\\\', "\b" => '\\b', "\t" => '\\t', "\n" => '\\n', "\f" => '\\f', "\r" => '\\r', '"' => '\\"', }
Class Method Summary collapse
- .escape(string) ⇒ Object
- .format_by_type(formatee) ⇒ Object
- .jsonify_hash(hash) ⇒ Object
-
.rearrange_slow_transaction(slow_t) ⇒ Object
takes just one slow transaction.
-
.rearrange_the_metrics(metrics) ⇒ Object
For the old style of metric serializing.
-
.rearrange_the_slow_transactions(slow_transactions) ⇒ Object
takes an array of slow transactions.
- .serialize(metadata, metrics, slow_transactions, jobs, slow_jobs, histograms, db_query_metrics, external_service_metrics, traces) ⇒ Object
Class Method Details
.escape(string) ⇒ Object
63 64 65 66 67 |
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 63 def escape(string) ESCAPE_MAPPINGS.inject(string.to_s) {|s, (bad, good)| s.gsub(bad, good) } end |
.format_by_type(formatee) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 69 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
41 42 43 44 45 46 47 48 49 |
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 41 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_slow_transaction(slow_t) ⇒ Object
takes just one slow transaction
37 38 39 |
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 37 def rearrange_slow_transaction(slow_t) slow_t.as_json.merge(:metrics => rearrange_the_metrics(slow_t.metrics), :allocation_metrics => rearrange_the_metrics(slow_t.allocation_metrics)) end |
.rearrange_the_metrics(metrics) ⇒ Object
For the old style of metric serializing.
25 26 27 28 29 |
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 25 def rearrange_the_metrics(metrics) metrics.to_a.map do |, stats| stats.as_json.merge(:key => .as_json) end end |
.rearrange_the_slow_transactions(slow_transactions) ⇒ Object
takes an array of slow transactions
32 33 34 |
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 32 def rearrange_the_slow_transactions(slow_transactions) slow_transactions.to_a.map { |t| rearrange_slow_transaction(t) } end |
.serialize(metadata, metrics, slow_transactions, jobs, slow_jobs, histograms, db_query_metrics, external_service_metrics, traces) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/scout_apm/serializers/payload_serializer_to_json.rb', line 5 def serialize(, metrics, slow_transactions, jobs, slow_jobs, histograms, db_query_metrics, external_service_metrics, traces) .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, :histograms => HistogramsSerializerToJson.new(histograms).as_json, :db_metrics => { :query => DbQuerySerializerToJson.new(db_query_metrics).as_json, }, :es_metrics => { :http => ExternalServiceSerializerToJson.new(external_service_metrics).as_json, }, :span_traces => traces.map{ |t| t.as_json }, }) end |