Class: DeprecationCollector::Deprecation

Inherits:
Object
  • Object
show all
Defined in:
lib/deprecation_collector/deprecation.rb

Overview

:nodoc:

Constant Summary collapse

CLEANUP_REGEXES =
{
  # rails views generated methods names are unique per-worker
  /_app_views_(\w+)__(\d+)_(\d+)/ => "_app_views_\\1__",

  # repl line numbers are not important, may be ignore all repl at all
  /\A\((pry|irb)\):\d+/ => '(\1)'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, realm = nil, backtrace = [], cleanup_prefixes = []) ⇒ Deprecation



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/deprecation_collector/deprecation.rb', line 17

def initialize(message, realm = nil, backtrace = [], cleanup_prefixes = [])
  # backtrace is Thread::Backtrace::Location or array of strings for other realms
  @message = message.dup
  @realm = realm
  @occurences = 0
  @gem_traceline = find_gem_traceline(backtrace)
  @app_traceline = find_app_traceline(backtrace)
  @first_timestamp = Time.now.to_i

  cleanup_prefixes.each do |path|
    @gem_traceline.delete_prefix!(path)
    @message.gsub!(path, "")
  end

  CLEANUP_REGEXES.each_pair do |regex, replace|
    @gem_traceline&.gsub!(regex, replace)
    @app_traceline&.gsub!(regex, replace)
  end

  @full_backtrace = backtrace.map(&:to_s) if DeprecationCollector.instance.save_full_backtrace
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



7
8
9
# File 'lib/deprecation_collector/deprecation.rb', line 7

def app_name
  @app_name
end

#app_tracelineObject (readonly)

Returns the value of attribute app_traceline.



6
7
8
# File 'lib/deprecation_collector/deprecation.rb', line 6

def app_traceline
  @app_traceline
end

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/deprecation_collector/deprecation.rb', line 7

def context
  @context
end

#custom_fingerprintObject

Returns the value of attribute custom_fingerprint.



7
8
9
# File 'lib/deprecation_collector/deprecation.rb', line 7

def custom_fingerprint
  @custom_fingerprint
end

#first_timestampObject (readonly)

Returns the value of attribute first_timestamp.



6
7
8
# File 'lib/deprecation_collector/deprecation.rb', line 6

def first_timestamp
  @first_timestamp
end

#full_backtraceObject (readonly)

Returns the value of attribute full_backtrace.



6
7
8
# File 'lib/deprecation_collector/deprecation.rb', line 6

def full_backtrace
  @full_backtrace
end

#gem_tracelineObject (readonly)

Returns the value of attribute gem_traceline.



6
7
8
# File 'lib/deprecation_collector/deprecation.rb', line 6

def gem_traceline
  @gem_traceline
end

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/deprecation_collector/deprecation.rb', line 6

def message
  @message
end

#occurencesObject (readonly)

Returns the value of attribute occurences.



6
7
8
# File 'lib/deprecation_collector/deprecation.rb', line 6

def occurences
  @occurences
end

#realmObject (readonly)

Returns the value of attribute realm.



6
7
8
# File 'lib/deprecation_collector/deprecation.rb', line 6

def realm
  @realm
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/deprecation_collector/deprecation.rb', line 62

def as_json(_options = {})
  {
    app: app_name,
    message: message,
    realm: realm,
    app_traceline: app_traceline,
    gem_traceline: (gem_traceline != app_traceline && gem_traceline) || nil,
    full_backtrace: full_backtrace,
    ruby_version: RUBY_VERSION,
    rails_version: (defined?(Rails) && Rails.version),
    hostname: Socket.gethostname,
    revision: DeprecationCollector.instance.app_revision,
    count: @occurences, # output anyway for frequency estimation (during write_interval inside single process)
    first_timestamp: first_timestamp, # this may not be accurate, a worker with later timestamp may dump earlier
    digest_base: digest_base, # for debug purposes
    context: context
  }.compact
end

#digestObject



53
54
55
# File 'lib/deprecation_collector/deprecation.rb', line 53

def digest
  @digest ||= Digest::MD5.hexdigest(digest_base)
end

#digest_baseObject



57
58
59
60
# File 'lib/deprecation_collector/deprecation.rb', line 57

def digest_base
  "1:#{RUBY_VERSION}:#{defined?(Rails) && Rails.version}:#{message_for_digest}:#{gem_traceline}:#{app_traceline}" \
    "#{":#{custom_fingerprint}" if custom_fingerprint}"
end

#ignored?Boolean



43
44
45
# File 'lib/deprecation_collector/deprecation.rb', line 43

def ignored?
  false
end

#message_for_digestObject



47
48
49
50
51
# File 'lib/deprecation_collector/deprecation.rb', line 47

def message_for_digest
  # some gems like rest-client put data in warnings, need to aggregate
  # + some bactrace per-worker unique method names may be there
  @message.gsub(/"(?:[^"\\]|\\.)*"/, '""').gsub(/__\d+_\d+/, "___").gsub(/\((pry|irb)\):\d+/, '(\1)')
end

#to_json(_options = {}) ⇒ Object



81
82
83
# File 'lib/deprecation_collector/deprecation.rb', line 81

def to_json(_options = {})
  as_json.to_json
end

#touchObject



39
40
41
# File 'lib/deprecation_collector/deprecation.rb', line 39

def touch
  @occurences += 1
end