Class: NdrError::Log

Inherits:
Object
  • Object
show all
Includes:
BacktraceCompression, Fuzzing, UuidBuilder
Defined in:
app/models/ndr_error/log.rb

Overview

Stores instances of logs. Child of NdrError::Fingerprint. Log records may be purged, whereas fingerprints are designed to be eternally persisted.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UuidBuilder

#construct_uuid

Methods included from Fuzzing

#fuzz

Methods included from BacktraceCompression

#application_trace, #backtrace, #backtrace=

Class Method Details

.filter_by_keywords(keywords) ⇒ Object



45
46
47
48
49
50
# File 'app/models/ndr_error/log.rb', line 45

def self.filter_by_keywords(keywords)
  fragment   = text_columns.map { |column| "lower(#{column}) LIKE lower(:key)" }.join(' OR ')
  name_match = keywords.map { |part| sanitize_sql([fragment, key: "%#{part}%"]) }.join(' OR ')

  where(name_match)
end

.perform_cleanup!Object

Deletes all those errors that have been flagged for deletion and whose soft-delete grace period has ended.

Returns true if any records were deleted.



56
57
58
59
60
61
62
63
# File 'app/models/ndr_error/log.rb', line 56

def self.perform_cleanup!
  destroys = deleted.map do |record|
    stamp_string = record.status.sub(/^[^\d]+/, '')
    record.destroy if Time.zone.parse(stamp_string) < NdrError.log_grace_period.ago
  end

  destroys.any?
end

.text_columnsObject

Raises:

  • (SecurityError)


33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/ndr_error/log.rb', line 33

def self.text_columns
  whitelist   = %w[error_class description]
  column_name = NdrError.user_column.to_s
  user_column = columns_hash[column_name]

  raise SecurityError, 'User column missing!' unless user_column

  # allow the user column to be searched if it is textual:
  whitelist << column_name if user_column.type == :string
  whitelist
end

Instance Method Details

#clock_drift?Boolean

Returns true if clock drift of more than 3 seconds was present at the time of the error.

Returns:

  • (Boolean)


147
148
149
# File 'app/models/ndr_error/log.rb', line 147

def clock_drift?
  clock_drift && clock_drift >= 3.0
end

#error_stringObject

Formats error to be like the ruby error.



141
142
143
# File 'app/models/ndr_error/log.rb', line 141

def error_string
  [error_class, description].compact.join(': ')
end

#flag_as_deleted!(time = Time.current) ⇒ Object

Performs a soft-delete of this log.



87
88
89
# File 'app/models/ndr_error/log.rb', line 87

def flag_as_deleted!(time = Time.current)
  update_attribute(:status, "deleted at #{time.to_formatted_s(:db)}")
end

#md5_digestObject

Creates (and caches) the md5 of this error, which is used to match to similar errors.



153
154
155
# File 'app/models/ndr_error/log.rb', line 153

def md5_digest
  @_digest ||= fuzz(description, backtrace, parent_print)
end

#md5_digest=(digest) ⇒ Object

Allow the digest to be set manually if so desired.



158
159
160
# File 'app/models/ndr_error/log.rb', line 158

def md5_digest=(digest)
  @_digest = digest
end

#nextObject

Returns the next historical occurence, or nil if there hasn’t been one.



80
81
82
83
84
# File 'app/models/ndr_error/log.rb', line 80

def next
  lookup = similar_errors.to_a
  index  = lookup.index(self)
  lookup[0...index].last
end

#parametersObject

Returns the params hash associated with the request.



136
137
138
# File 'app/models/ndr_error/log.rb', line 136

def parameters
  YAML.load(parameters_yml)
end

#parameters_yml=(params) ⇒ Object Also known as: set_parameters_yml

Store as much of ‘params’ as possible in the YAML parameters column.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/ndr_error/log.rb', line 114

def parameters_yml=(params)
  yml_dump = {}.to_yaml
  parts    = params.sort_by { |_k, v| v.inspect.length }

  # `parts' was sorted by length so that we
  # can capture as many different parameters
  # as possible in the given column space:
  (1..parts.length).each do |length|
    sub_hash = Hash[parts.first(length)]
    sub_yml  = sub_hash.to_yaml

    break if sub_yml.length >= 4000
    yml_dump = sub_yml
  end

  self[:parameters_yml] = yml_dump
end

#previousObject

Returns the previous historical occurence, or nil if there wasn’t one.



72
73
74
75
76
# File 'app/models/ndr_error/log.rb', line 72

def previous
  lookup = similar_errors.reverse
  index  = lookup.index(self)
  lookup[0...index].last
end

#register_exception(exception) ⇒ Object

Copy across attributes from the exception object.



92
93
94
95
96
# File 'app/models/ndr_error/log.rb', line 92

def register_exception(exception)
  self.error_class = exception.class.to_s
  self.backtrace   = exception.backtrace
  self.description = description_from(exception.message)
end

#register_parent(parent_print) ⇒ Object

If we have more details about a parent error, track that too so it can be mixed in to the MD5 digest.



108
109
110
# File 'app/models/ndr_error/log.rb', line 108

def register_parent(parent_print)
  @parent_print = parent_print.id if parent_print
end

#register_request(request) ⇒ Object

Stores parameters from the given request object as YAML. Will attempt to store as many as possible of the parameters in the available 4000 chars.



101
102
103
104
# File 'app/models/ndr_error/log.rb', line 101

def register_request(request)
  extract_request_params(request)
  extract_request_attributes(request)
end

#similar_errorsObject

returns all sibling occurences, including self



66
67
68
# File 'app/models/ndr_error/log.rb', line 66

def similar_errors
  error_fingerprint.error_logs.not_deleted
end