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.



17
18
19
# File 'lib/appmap/detect_enabled.rb', line 17

def initialize(recording_method)
  @recording_method = recording_method
end

Class Method Details

.clear_cacheObject



12
13
14
# File 'lib/appmap/detect_enabled.rb', line 12

def clear_cache
  @@detected_for_method = {}
end

Instance Method Details

#detect_app_envObject



81
82
83
84
85
86
87
# File 'lib/appmap/detect_enabled.rb', line 81

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

#detect_enabledObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/appmap/detect_enabled.rb', line 41

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 = []
  while enabled.nil? && !detection_functions.empty?
    message, enabled = method(detection_functions.shift).call
  end

  unless enabled.nil?
    _, enabled_by_env = enabled_by_app_env?
    return [ message, enabled, enabled_by_env ]
  else
    return [ 'it is not enabled by any configuration or framework', false, false ]
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/appmap/detect_enabled.rb', line 21

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

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

  message, enabled, enabled_by_env = detect_enabled

  @@detected_for_method[@recording_method] = enabled

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

  enabled
end

#enabled_by_app_env?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'lib/appmap/detect_enabled.rb', line 70

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

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

#enabled_by_testing?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/appmap/detect_enabled.rb', line 64

def enabled_by_testing?
  if %i[rspec minitest cucumber].member?(@recording_method)
    [ "running tests with #{@recording_method}", true ]
  end
end

#globally_disabled?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/appmap/detect_enabled.rb', line 96

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

#globally_enabled?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/appmap/detect_enabled.rb', line 89

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.()
end

#rails_envObject



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

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

  return ENV['RAILS_ENV']
end

#recording_method_disabled?Boolean

Returns:

  • (Boolean)


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

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)


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

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