class Rollbar::Truncation::RemoveAnyKeyStrategy

Attributes

data[RW]
extracted_title[RW]
payload[RW]
sizes[RW]

Public Class Methods

call(payload) click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 10
def self.call(payload)
  new(payload).call
end
new(payload) click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 14
def initialize(payload)
  @payload = payload
  @data = payload['data']
  @extracted_title = extract_title(data['body']) if data['body']
end

Public Instance Methods

call() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 20
def call
  remove_unknown_root_keys

  json_payload = remove_oversized_data_keys

  return json_payload if json_payload

  dump(payload)
end
data_keys() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 106
def data_keys
  @data_keys ||= {}.tap do |hash|
    data.keys.reject { |key| skip_keys.include?(key) }.each do |key|
      set_key_size(key, hash)
    end
  end
end
extract_title(body) click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 91
def extract_title(body)
  return body['message']['body'] if body['message'] && body['message']['body']
  return extract_title_from_trace(body['trace']) if body['trace']

  return unless body['trace_chain'] && body['trace_chain'][0]

  extract_title_from_trace(body['trace_chain'][0])
end
extract_title_from_trace(trace) click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 100
def extract_title_from_trace(trace)
  exception = trace['exception']

  "#{exception['class']}: #{exception['message']}"
end
message_key() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 82
def message_key
  # use this message if data.body gets removed
  {
    'message' => {
      'body' => 'Payload keys removed due to oversized payload. See diagnostic key'
    }
  }
end
remove_key_and_return_payload(key) click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 48
def remove_key_and_return_payload(key)
  size = data_keys[key]

  data.delete(key)

  replace_message_body if key == 'body'

  truncation_key[key] = "key removed, size: #{size} bytes"

  dump(payload)
end
remove_oversized_data_keys() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 38
def remove_oversized_data_keys
  data_keys.keys.sort { |a, b| data_keys[b] <=> data_keys[a] }.each do |key|
    json_payload = remove_key_and_return_payload(key)

    return json_payload unless truncate?(json_payload)
  end

  false
end
remove_unknown_root_keys() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 30
def remove_unknown_root_keys
  payload.keys.reject { |key| root_keys.include?(key) }.each do |key|
    truncation_key['root'] ||= {}
    size = dump(payload.delete(key)).bytesize
    truncation_key['root'][key] = "unknown root key removed, size: #{size} bytes"
  end
end
replace_message_body() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 60
def replace_message_body
  data['body'] = message_key
  data['title'] ||= extracted_title if extracted_title
end
root_keys() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 72
def root_keys
  # Valid keys in root of payload
  %w[access_token data]
end
set_key_size(key, hash) click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 114
def set_key_size(key, hash)
  size = dump(data[key]).bytesize
  hash[key] = size
rescue ::JSON::GeneratorError
  hash[key] = 0 # don't try to truncate non JSON object

  # Log it
  truncation_key['non_json_keys'] ||= {}
  truncation_key['non_json_keys'][key] = data[key].class
end
skip_keys() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 77
def skip_keys
  # Don't try to truncate these data keys
  %w[notifier uuid title platform language framework level]
end
truncation_key() click to toggle source
# File lib/rollbar/truncation/remove_any_key_strategy.rb, line 65
def truncation_key
  @truncation_key ||=
    # initialize the diagnostic key for truncation
    (data['notifier']['diagnostic'] ||= {}) &&
    (data['notifier']['diagnostic']['truncation'] ||= {})
end