Class: ScoutApm::Environment

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/scout_apm/environment.rb

Constant Summary collapse

STDOUT_LOGGER =
begin
  l = Logger.new(STDOUT)
  l.level = Logger::INFO
  l
end
SERVER_INTEGRATIONS =

I’ve put Thin and Webrick last as they are often used in development and included in Gemfiles but less likely used in production.

[
  ScoutApm::ServerIntegrations::Passenger.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Unicorn.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Rainbows.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Puma.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Thin.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Webrick.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Null.new(STDOUT_LOGGER), # must be last
]
BACKGROUND_JOB_INTEGRATIONS =
[
  ScoutApm::BackgroundJobIntegrations::Resque.new,
  ScoutApm::BackgroundJobIntegrations::Sidekiq.new,
  ScoutApm::BackgroundJobIntegrations::Shoryuken.new,
  ScoutApm::BackgroundJobIntegrations::Sneakers.new,
  ScoutApm::BackgroundJobIntegrations::DelayedJob.new,
  ScoutApm::BackgroundJobIntegrations::Que.new,
  ScoutApm::BackgroundJobIntegrations::Faktory.new,
  ScoutApm::BackgroundJobIntegrations::GoodJob.new,
  ScoutApm::BackgroundJobIntegrations::SolidQueue.new,
]
FRAMEWORK_INTEGRATIONS =
[
  ScoutApm::FrameworkIntegrations::Rails2.new,
  ScoutApm::FrameworkIntegrations::Rails3Or4.new,
  ScoutApm::FrameworkIntegrations::Sinatra.new,
  ScoutApm::FrameworkIntegrations::Ruby.new, # Fallback if none match
]
PLATFORM_INTEGRATIONS =
[
  ScoutApm::PlatformIntegrations::Heroku.new,
  ScoutApm::PlatformIntegrations::CloudFoundry.new,
  ScoutApm::PlatformIntegrations::Server.new,
]

Instance Method Summary collapse

Instance Method Details

#app_serverObject

App server’s name (symbol)



140
141
142
# File 'lib/scout_apm/environment.rb', line 140

def app_server
  app_server_integration.name
end

#app_server_integration(force = false) ⇒ Object

Returns the whole integration object This needs to be improved. Frequently, multiple app servers gem are present and which ever is checked first becomes the designated app server.

Next step: (1) list out all detected app servers (2) install hooks for those that need it (passenger, rainbows, unicorn).



134
135
136
137
# File 'lib/scout_apm/environment.rb', line 134

def app_server_integration(force=false)
  @app_server = nil if force
  @app_server ||= SERVER_INTEGRATIONS.detect{ |integration| integration.present? }
end

#application_nameObject



67
68
69
70
71
# File 'lib/scout_apm/environment.rb', line 67

def application_name
  Agent.instance.context.config.value("name") ||
    framework_integration.application_name ||
    "App"
end

#background_job_integrationsObject



150
151
152
153
154
155
156
# File 'lib/scout_apm/environment.rb', line 150

def background_job_integrations
  if Agent.instance.context.config.value("enable_background_jobs")
    @background_job_integrations ||= BACKGROUND_JOB_INTEGRATIONS.select {|integration| integration.present?}
  else
    []
  end
end

#database_engineObject



73
74
75
# File 'lib/scout_apm/environment.rb', line 73

def database_engine
  framework_integration.database_engine
end

#envObject



51
52
53
# File 'lib/scout_apm/environment.rb', line 51

def env
  @env ||= framework_integration.env
end

#forking?Boolean

If forking, don’t start worker thread in the master process. Since it’s started as a Thread, it won’t survive the fork.

Returns:

  • (Boolean)


146
147
148
# File 'lib/scout_apm/environment.rb', line 146

def forking?
  app_server_integration.forking? || (background_job_integration && background_job_integration.forking?)
end

#frameworkObject



55
56
57
# File 'lib/scout_apm/environment.rb', line 55

def framework
  framework_integration.name
end

#framework_integrationObject



59
60
61
# File 'lib/scout_apm/environment.rb', line 59

def framework_integration
  @framework ||= FRAMEWORK_INTEGRATIONS.detect{ |integration| integration.present? }
end

#framework_rootObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/scout_apm/environment.rb', line 106

def framework_root
  if override_root = Agent.instance.context.config.value("application_root")
    return override_root
  end
  if framework == :rails
    RAILS_ROOT.to_s
  elsif framework == :rails3_or_4
    Rails.root
  elsif framework == :sinatra
    Sinatra::Application.root || "."
  else
    '.'
  end
end

#git_revisionObject



125
126
127
# File 'lib/scout_apm/environment.rb', line 125

def git_revision
  @git_revision ||= ScoutApm::GitRevision.new(Agent.instance.context)
end

#hostnameObject



121
122
123
# File 'lib/scout_apm/environment.rb', line 121

def hostname
  @hostname ||= Agent.instance.context.config.value("hostname") || platform_integration.hostname
end

#interactive?Boolean

If both stdin & stdout are interactive and the Rails::Console constant is defined

Returns:

  • (Boolean)


159
160
161
# File 'lib/scout_apm/environment.rb', line 159

def interactive?
  defined?(::Rails::Console) && $stdout.isatty && $stdin.isatty
end

#jruby?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/scout_apm/environment.rb', line 169

def jruby?
  defined?(JRuby)
end

#osObject

Returns a string representation of the OS (ex: darwin, linux)



209
210
211
212
213
214
215
216
217
218
# File 'lib/scout_apm/environment.rb', line 209

def os
  return @os if @os
  raw_os = RbConfig::CONFIG['target_os']
  match = raw_os.match(/([a-z]+)/)
  if match
    @os = match[1]
  else
    @os = raw_os
  end
end

#platform_integrationObject



63
64
65
# File 'lib/scout_apm/environment.rb', line 63

def platform_integration
  @platform ||= PLATFORM_INTEGRATIONS.detect{ |integration| integration.present? }
end

#processorsObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/scout_apm/environment.rb', line 81

def processors
  @processors ||= begin
                    proc_file = '/proc/cpuinfo'
                    processors = if !File.exist?(proc_file)
                                   1
                                 else
                                   lines = File.read("/proc/cpuinfo").lines.to_a
                                   lines.grep(/^processor\s*:/i).size
                                 end
                    [processors, 1].compact.max
                  end
end

#raw_database_adapterObject



77
78
79
# File 'lib/scout_apm/environment.rb', line 77

def raw_database_adapter
  framework_integration.raw_database_adapter
end

#rootObject



102
103
104
# File 'lib/scout_apm/environment.rb', line 102

def root
  @root ||= framework_root
end

#rubinius?Boolean

ruby checks

Returns:

  • (Boolean)


165
166
167
# File 'lib/scout_apm/environment.rb', line 165

def rubinius?
  RUBY_VERSION =~ /rubinius/i
end

#ruby_187?Boolean

Returns:

  • (Boolean)


178
179
180
181
# File 'lib/scout_apm/environment.rb', line 178

def ruby_187?
  return @ruby_187 if defined?(@ruby_187)
  @ruby_187 = defined?(RUBY_VERSION) && RUBY_VERSION.match(/^1\.8\.7/)
end

#ruby_19?Boolean

Returns:

  • (Boolean)


173
174
175
176
# File 'lib/scout_apm/environment.rb', line 173

def ruby_19?
  return @ruby_19 if defined?(@ruby_19)
  @ruby_19 = defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION.match(/^1\.9/)
end

#ruby_2?Boolean

Returns:

  • (Boolean)


183
184
185
186
# File 'lib/scout_apm/environment.rb', line 183

def ruby_2?
  return @ruby_2 if defined?(@ruby_2)
  @ruby_2 = defined?(RUBY_VERSION) && RUBY_VERSION.match(/^2/)
end

#ruby_3?Boolean

Returns:

  • (Boolean)


188
189
190
191
# File 'lib/scout_apm/environment.rb', line 188

def ruby_3?
  return @ruby_3 if defined?(@ruby_3)
  @ruby_3 = defined?(RUBY_VERSION) && RUBY_VERSION.match(/^3/)
end

#ruby_minorObject



193
194
195
196
# File 'lib/scout_apm/environment.rb', line 193

def ruby_minor
  return @ruby_minor if defined?(@ruby_minor)
  @ruby_minor = defined?(RUBY_VERSION) && RUBY_VERSION.split(".")[1].to_i
end

#scm_subdirectoryObject



94
95
96
97
98
99
100
# File 'lib/scout_apm/environment.rb', line 94

def scm_subdirectory
  @scm_subdirectory ||= if Agent.instance.context.config.value('scm_subdirectory').empty?
    ''
  else
    Agent.instance.context.config.value('scm_subdirectory').sub(/^\//, '') # Trim any leading slash
  end
end

#sinatra?Boolean

framework checks

Returns:

  • (Boolean)


222
223
224
# File 'lib/scout_apm/environment.rb', line 222

def sinatra?
  framework_integration.name == :sinatra
end

#supports_kwarg_delegation?Boolean

Returns true if this Ruby version makes positional and keyword arguments incompatible

Returns:

  • (Boolean)


204
205
206
# File 'lib/scout_apm/environment.rb', line 204

def supports_kwarg_delegation?
  ruby_3? || (ruby_2? && ruby_minor >= 7)
end

#supports_module_prepend?Boolean

Returns true if this Ruby version supports Module#prepend.

Returns:

  • (Boolean)


199
200
201
# File 'lib/scout_apm/environment.rb', line 199

def supports_module_prepend?
  ruby_2? || ruby_3?
end