Class: Contrast::Agent::Inventory::DependencyUsageAnalysis

Inherits:
Object
  • Object
show all
Includes:
Dependencies, Components::Interface, Singleton
Defined in:
lib/contrast/agent/inventory/dependency_usage_analysis.rb

Overview

Used to analyze class usage for reporting

Constant Summary

Constants included from Dependencies

Contrast::Agent::Inventory::Dependencies::CONTRAST_AGENT

Instance Method Summary collapse

Methods included from Dependencies

#loaded_specs

Methods included from Components::Interface

included

Constructor Details

#initializeDependencyUsageAnalysis

Returns a new instance of DependencyUsageAnalysis.



20
21
22
23
24
# File 'lib/contrast/agent/inventory/dependency_usage_analysis.rb', line 20

def initialize
  return unless enabled?

  @gemdigest_cache = Contrast::Agent::Inventory::GemfileDigestCache.new
end

Instance Method Details

#associate_file(path) ⇒ Object

This method is invoked once per TracePoint :end - to map a specific file being required to the gem it belongs to

Parameters:

  • path (String)

    the result of TracePoint#path from the :end event in which the Module was defined



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/contrast/agent/inventory/dependency_usage_analysis.rb', line 50

def associate_file path
  return unless enabled?

  spec_lookup_path = adjust_path_for_spec_lookup(path)

  spec = Gem::Specification.find_by_path(spec_lookup_path)
  unless spec
    logger.debug('Unable to resolve gem spec for path', path: path)
    return
  end

  digest = Contrast::Utils::Sha256Builder.instance.build_from_spec(spec)
  unless digest
    logger.debug('Unable to resolve digest for gem spec', spec: spec.to_s)
    return
  end
  report_path = adjust_path_for_reporting(path, spec)
  @gemdigest_cache.get(digest) << report_path
rescue StandardError => e
  logger.error('Unable to inventory file path', e, path: path)
end

#catchupObject

This method is invoked once, along with the rest of our catchup code to report libraries and their associated files that have already been loaded pre-contrast



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/contrast/agent/inventory/dependency_usage_analysis.rb', line 28

def catchup
  return unless enabled?

  loaded_specs.each do |_name, spec|
    # Get a digest of the Gem file itself
    next unless (digest = Contrast::Utils::Sha256Builder.instance.build_from_spec(spec))

    @gemdigest_cache.use_cache(digest) do |existing_files|
      loaded_files_from_gem = $LOADED_FEATURES.select { |f| f.start_with?(spec.full_gem_path) }
      loaded_files_from_gem.each do |file_path|
        logger.trace('Recording loaded file for inventory analysis', line: file_path)
        existing_files << adjust_path_for_reporting(file_path, spec)
      end
    end
  end
end

#generate_library_usage(activity) ⇒ Object

Populate the library_usages filed of the Activity message using the data stored in the @gemdigest_cache

Parameters:



77
78
79
80
81
82
# File 'lib/contrast/agent/inventory/dependency_usage_analysis.rb', line 77

def generate_library_usage activity
  return unless enabled?
  return if @gemdigest_cache.empty?

  @gemdigest_cache.generate_usage_data(activity)
end