Class: ScoutApm::AgentContext

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/agent_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAgentContext

Initially start up without attempting to load a configuration file. We need to be able to lookup configuration options like “application_root” which would then in turn influence where the yaml configuration file is located

Later in initialization, we set config= to include the file.



9
10
11
12
# File 'lib/scout_apm/agent_context.rb', line 9

def initialize()
  @logger = LoggerFactory.build_minimal_logger
  @process_start_time = Time.now
end

Instance Attribute Details

#process_start_timeObject (readonly)

Accessors #



64
65
66
# File 'lib/scout_apm/agent_context.rb', line 64

def process_start_time
  @process_start_time
end

Instance Method Details

#become_remote_client!(host, port) ⇒ Object

Execute this in the child process of a remote agent. The parent is expected to have its accepting webserver up and running



53
54
55
56
57
# File 'lib/scout_apm/agent_context.rb', line 53

def become_remote_client!(host, port)
  logger.debug("Becoming Remote Agent (reporting to: #{host}:#{port})")
  @recorder = ScoutApm::Remote::Recorder.new(host, port, logger)
  @store = ScoutApm::FakeStore.new
end

#configObject



66
67
68
# File 'lib/scout_apm/agent_context.rb', line 66

def config
  @config ||= ScoutApm::Config.without_file(self)
end

#config=(config) ⇒ Object

When we set the config for any reason, there are some values we must reinitialize, since the config could have changed their settings, so nil them out here, then let them get lazily reset as needed

Don’t use this in initializer, since it’ll attempt to log immediately



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/scout_apm/agent_context.rb', line 139

def config=(config)
  @config = config

  @logger = nil

  log_configuration_settings

  @ignored_uris = nil
  @slow_request_policy = nil
  @slow_job_policy = nil
  @request_histograms = nil
  @request_histograms_by_time = nil
  @store = nil
  @layaway = nil
  @recorder = nil
end

#dev_trace_enabled?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/scout_apm/agent_context.rb', line 126

def dev_trace_enabled?
  config.value('dev_trace') && environment.env == "development"
end

#environmentObject



70
71
72
# File 'lib/scout_apm/agent_context.rb', line 70

def environment
  @environment ||= ScoutApm::Environment.instance
end

#environment=(env) ⇒ Object

I believe this is only useful for testing?



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

def environment=(env)
  @environment = env
end

#ignored_urisObject



90
91
92
# File 'lib/scout_apm/agent_context.rb', line 90

def ignored_uris
  @ignored_uris ||= ScoutApm::IgnoredUris.new(config.value('ignore'))
end

#installed!Object



156
157
158
# File 'lib/scout_apm/agent_context.rb', line 156

def installed!
  @installed = true
end

#installed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/scout_apm/agent_context.rb', line 82

def installed?
  @installed
end

#layawayObject



118
119
120
# File 'lib/scout_apm/agent_context.rb', line 118

def layaway
  @layaway ||= ScoutApm::Layaway.new(self)
end

#log_configuration_settingsObject

Called after config is reset and loaded from file



194
195
196
197
198
199
200
201
202
# File 'lib/scout_apm/agent_context.rb', line 194

def log_configuration_settings
  @config.log_settings(logger)

  if !@config.any_keys_found?
    logger.info("No configuration file loaded, and no configuration found in ENV. " +
                "For assistance configuring Scout, visit " +
                "http://help.apm.scoutapp.com/#configuration-options")
  end
end

#loggerObject



86
87
88
# File 'lib/scout_apm/agent_context.rb', line 86

def logger
  @logger ||= LoggerFactory.build(config, environment)
end

#marshal_dumpObject



14
15
16
# File 'lib/scout_apm/agent_context.rb', line 14

def marshal_dump
  []
end

#marshal_load(*args) ⇒ Object



18
19
20
21
# File 'lib/scout_apm/agent_context.rb', line 18

def marshal_load(*args)
  @logger = LoggerFactory.build_minimal_logger
  @process_start_time = Time.now
end

#recorderObject



122
123
124
# File 'lib/scout_apm/agent_context.rb', line 122

def recorder
  @recorder ||= RecorderFactory.build(self)
end

#recorder=(recorder) ⇒ Object



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

def recorder=(recorder)
  @recorder = recorder
end

#request_histogramsObject

Histogram of the cumulative requests since the start of the process



103
104
105
# File 'lib/scout_apm/agent_context.rb', line 103

def request_histograms
  @request_histograms ||= ScoutApm::RequestHistograms.new
end

#request_histograms_by_timeObject

Histogram of the requests, distinct by reporting period (minute) { StoreReportingPeriodTimestamp => RequestHistograms }



109
110
111
# File 'lib/scout_apm/agent_context.rb', line 109

def request_histograms_by_time
  @request_histograms_by_time ||= Hash.new { |h, k| h[k] = ScoutApm::RequestHistograms.new }
end

#shutting_down!Object



164
165
166
# File 'lib/scout_apm/agent_context.rb', line 164

def shutting_down!
  @shutting_down = true
end

#shutting_down?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/scout_apm/agent_context.rb', line 78

def shutting_down?
  @shutting_down
end

#slow_job_policyObject



98
99
100
# File 'lib/scout_apm/agent_context.rb', line 98

def slow_job_policy
  @slow_job_policy ||= ScoutApm::SlowJobPolicy.new(self)
end

#slow_request_policyObject



94
95
96
# File 'lib/scout_apm/agent_context.rb', line 94

def slow_request_policy
  @slow_request_policy ||= ScoutApm::SlowRequestPolicy.new(self)
end

#start_remote_server!(bind, port) ⇒ Object

Lifecycle: Remote Server/Client

This allows short lived forked processes to communicate back to the parent process.
Used in the Resque instrumentation

Parent Pre-fork: start_remote_server! once
Child Post-fork: become_remote_client! after each fork

TODO: Figure out where to extract this to


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scout_apm/agent_context.rb', line 35

def start_remote_server!(bind, port)
  return if @remote_server && @remote_server.running?

  logger.info("Starting Remote Agent Server")

  # Start the listening web server only in parent process.
  @remote_server = ScoutApm::Remote::Server.new(
    bind,
    port,
    ScoutApm::Remote::Router.new(ScoutApm::SynchronousRecorder.new(self), logger),
    logger
  )

  @remote_server.start
end

#started!Object



160
161
162
# File 'lib/scout_apm/agent_context.rb', line 160

def started!
  @started = true
end

#started?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/scout_apm/agent_context.rb', line 74

def started?
  @started
end

#storeObject



113
114
115
116
# File 'lib/scout_apm/agent_context.rb', line 113

def store
  return @store if @store
  self.store = ScoutApm::Store.new(self)
end

#store=(store) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/scout_apm/agent_context.rb', line 168

def store=(store)
  @store = store

  # Installs the default samplers
  # Don't install samplers on nil stores
  if store
    ScoutApm::Instruments::Samplers::DEFAULT_SAMPLERS.each { |s| store.add_sampler(s) }
  end
end