Class: AppMap::DetectEnabled

Inherits:
Object
  • Object
show all
Defined in:
lib/appmap/detect_enabled.rb

Overview

Detects whether AppMap recording should be enabled. This test can be performed generally, or for a particular recording method. Recording can be enabled explicitly, for example via APPMAP=true, or it can be enabled implicitly, by running in a dev or test web application environment. Recording can also disabled explicitly, using environment variables.

Constant Summary collapse

@@detected_for_method =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recording_method) ⇒ DetectEnabled

Returns a new instance of DetectEnabled.



43
44
45
# File 'lib/appmap/detect_enabled.rb', line 43

def initialize(recording_method)
  @recording_method = recording_method
end

Class Method Details

.clear_cacheObject



38
39
40
# File 'lib/appmap/detect_enabled.rb', line 38

def clear_cache
  @@detected_for_method = {}
end

.discourage_conflicting_recording_methods(recording_method) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/appmap/detect_enabled.rb', line 12

def discourage_conflicting_recording_methods(recording_method)
  return if ENV['APPMAP_DISCOURAGE_CONFLICTING_RECORDING_METHODS'] == 'false'

  return unless enabled?(recording_method.to_sym) && enabled?(:requests)

  warn Util.color <<~MSG, :yellow
AppMap recording is enabled for both 'requests' and '#{recording_method}'. This is not recommended
because the recordings will contain duplicitive information, and in some case may conflict with each other.
  MSG

  return unless ENV['APPMAP'] == 'true'

  warn Util.color <<~MSG, :yellow
The environment contains APPMAP=true, which is not recommended in this application environment because
it enables all recording methods. Consider letting AppMap detect the appropriate recording method,
or explicitly enabling only the recording methods you want to use using environment variables like
APPMAP_RECORD_REQUESTS, APPMAP_RECORD_RSPEC, etc.

See https://appmap.io/docs/reference/appmap-ruby.html#advanced-runtime-options for more information.
  MSG
end

.enabled?(recording_method) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/appmap/detect_enabled.rb', line 34

def enabled?(recording_method)
  new(recording_method).enabled?
end

Instance Method Details

#detect_app_envObject



100
101
102
103
104
105
106
# File 'lib/appmap/detect_enabled.rb', line 100

def detect_app_env
  if rails_env
    [ 'RAILS_ENV', rails_env ]
  elsif ENV['APP_ENV']
    [ 'APP_ENV', ENV['APP_ENV']]
  end
end

#detect_enabledObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/appmap/detect_enabled.rb', line 67

def detect_enabled
  detection_functions = %i[
    globally_disabled?
    recording_method_disabled?
    enabled_by_testing?
    enabled_by_app_env?
    recording_method_enabled?
    globally_enabled?
  ]

  message, enabled = []
  message, enabled = method(detection_functions.shift).call while enabled.nil? && !detection_functions.empty?

  return [ 'it is not enabled by any configuration or framework', false, false ] if enabled.nil?

  _, enabled_by_env = enabled_by_app_env?
  [ message, enabled, enabled_by_env ]
end

#enabled?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/appmap/detect_enabled.rb', line 47

def enabled?
  return @@detected_for_method[@recording_method] unless @@detected_for_method[@recording_method].nil?

  if @recording_method && !AppMap::RECORDING_METHODS.member?(@recording_method)
    raise "Unrecognized recording method: #{@recording_method}"
  end

  message, enabled, enabled_by_env = detect_enabled

  @@detected_for_method[@recording_method] = enabled

  if @recording_method && (enabled && enabled_by_app_env?)
    warn AppMap::Util.color(
      "AppMap #{@recording_method.nil? ? '' : "#{@recording_method} "}recording is enabled because #{message}", :magenta
    )
  end

  enabled
end

#enabled_by_app_env?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/appmap/detect_enabled.rb', line 92

def enabled_by_app_env?
  env_name, app_env = detect_app_env
  return [ "#{env_name} is '#{app_env}'", true ] if @recording_method.nil? && %w[test development].member?(app_env)

  return unless %i[remote requests].member?(@recording_method)
  return [ "#{env_name} is '#{app_env}'", true ] if app_env == 'development'
end

#enabled_by_testing?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/appmap/detect_enabled.rb', line 86

def enabled_by_testing?
  return unless %i[rspec minitest cucumber].member?(@recording_method)

  [ "running tests with #{@recording_method}", true ]
end

#globally_disabled?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/appmap/detect_enabled.rb', line 115

def globally_disabled?
  [ 'APPMAP=false', false ] if ENV['APPMAP'] == 'false'
end

#globally_enabled?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/appmap/detect_enabled.rb', line 108

def globally_enabled?
  # Don't auto-enable request recording in the 'test' environment, because users probably don't want
  # AppMaps of both test cases and requests. Requests recording can always be enabled by APPMAP_RECORD_REQUESTS=true.
  requests_recording_in_test = -> { [ :requests ].member?(@recording_method) && detect_app_env == 'test' }
  [ 'APPMAP=true', true ] if ENV['APPMAP'] == 'true' && !requests_recording_in_test.call
end

#rails_envObject



133
134
135
136
137
# File 'lib/appmap/detect_enabled.rb', line 133

def rails_env
  return Rails.env if defined?(::Rails::Railtie)

  ENV.fetch('RAILS_ENV', nil)
end

#recording_method_disabled?Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/appmap/detect_enabled.rb', line 119

def recording_method_disabled?
  return false unless @recording_method

  env_var = [ 'APPMAP', 'RECORD', @recording_method.upcase ].join('_')
  [ "#{[ 'APPMAP', 'RECORD', @recording_method.upcase ].join('_')}=false", false ] if ENV[env_var] == 'false'
end

#recording_method_enabled?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
# File 'lib/appmap/detect_enabled.rb', line 126

def recording_method_enabled?
  return false unless @recording_method

  env_var = [ 'APPMAP', 'RECORD', @recording_method.upcase ].join('_')
  [ "#{[ 'APPMAP', 'RECORD', @recording_method.upcase ].join('_')}=true", true ] if ENV[env_var] == 'true'
end