Class: Rollbar::Truncation::RemoveAnyKeyStrategy

Inherits:
Object
  • Object
show all
Includes:
Mixin
Defined in:
lib/rollbar/truncation/remove_any_key_strategy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin

#dump, #select_frames, #truncate?

Constructor Details

#initialize(payload) ⇒ RemoveAnyKeyStrategy

Returns a new instance of RemoveAnyKeyStrategy.



14
15
16
17
18
# 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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/rollbar/truncation/remove_any_key_strategy.rb', line 8

def data
  @data
end

#extracted_titleObject

Returns the value of attribute extracted_title.



8
9
10
# File 'lib/rollbar/truncation/remove_any_key_strategy.rb', line 8

def extracted_title
  @extracted_title
end

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/rollbar/truncation/remove_any_key_strategy.rb', line 8

def payload
  @payload
end

#sizesObject

Returns the value of attribute sizes.



8
9
10
# File 'lib/rollbar/truncation/remove_any_key_strategy.rb', line 8

def sizes
  @sizes
end

Class Method Details

.call(payload) ⇒ Object



10
11
12
# File 'lib/rollbar/truncation/remove_any_key_strategy.rb', line 10

def self.call(payload)
  new(payload).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
# 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_keysObject



106
107
108
109
110
111
112
# 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) ⇒ Object



91
92
93
94
95
96
97
98
# 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) ⇒ Object



100
101
102
103
104
# 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_keyObject



82
83
84
85
86
87
88
89
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# 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_keysObject



38
39
40
41
42
43
44
45
46
# 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_keysObject



30
31
32
33
34
35
36
# 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_bodyObject



60
61
62
63
# 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_keysObject



72
73
74
75
# 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) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# 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_keysObject



77
78
79
80
# 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_keyObject



65
66
67
68
69
70
# 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