-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathappsignal.rb
634 lines (578 loc) · 21 KB
/
appsignal.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# frozen_string_literal: true
require "json"
require "securerandom"
require "stringio"
require "appsignal/logger"
require "appsignal/utils/stdout_and_logger_message"
require "appsignal/helpers/instrumentation"
require "appsignal/helpers/metrics"
# AppSignal for Ruby gem's main module.
#
# Provides method to control the AppSignal instrumentation and the system
# agent. Also provides direct access to instrumentation helpers (from
# {Appsignal::Helpers::Instrumentation}) and metrics helpers (from
# {Appsignal::Helpers::Metrics}) for ease of use.
module Appsignal
class << self
include Helpers::Instrumentation
include Helpers::Metrics
# The loaded AppSignal configuration.
# Returns the current AppSignal configuration.
#
# Can return `nil` if no configuration has been set or automatically loaded
# by an automatic integration or by calling {.start}.
#
# @example
# Appsignal.config
#
# @return [Config, nil]
# @see configure
# @see Config
attr_reader :config
# Accessor for toggle if the AppSignal C-extension is loaded.
#
# Can be `nil` if extension has not been loaded yet. See
# {.extension_loaded?} for a boolean return value.
#
# @api private
# @return [Boolean, nil]
# @see Extension
# @see extension_loaded?
attr_accessor :extension_loaded
# @!attribute [rw] internal_logger
# Accessor for the internal AppSignal logger.
#
# Not to be confused with our logging feature.
# This is part of our private internal API. Do not call this method
# directly.
#
# If no logger has been set, it will return a "in memory logger", using
# {Utils::IntegrationMemoryLogger}. Once AppSignal is started (using
# {.start}) the contents of the "in memory logger" is written to the new
# logger.
#
# @api private
# @return [Utils::IntegrationLogger or Utils::IntegrationMemoryLogger]
# @see start
attr_writer :internal_logger
# Returns the error that was encountered while loading the `appsignal.rb`
# config file.
#
# It does not include any error that occurred while loading the
# `appsignal.yml` file.
#
# If the value is `nil`, no error was encountered or AppSignal wasn't
# started yet.
#
# @return [NilClass/Exception]
attr_reader :config_error
alias config_error? config_error
# @api private
def testing?
false
end
# Start the AppSignal integration.
#
# Starts AppSignal with the given configuration. If no configuration is set
# yet it will try to automatically load the configuration using the
# environment loaded from environment variables and the currently working
# directory.
#
# This is not required for the automatic integrations AppSignal offers, but
# this is required for all non-automatic integrations and pure Ruby
# applications. For more information, see our [integrations
# list](https://docs.appsignal.com/ruby/integrations/) and our [Integrating
# AppSignal](https://docs.appsignal.com/ruby/instrumentation/integrating-appsignal.html)
# guide.
#
# @example
# Appsignal.start
#
# @example with custom loaded configuration
# Appsignal.configure(:production) do |config|
# config.ignore_actions = ["My action"]
# end
# Appsignal.start
#
# @return [void]
# @since 0.7.0
def start # rubocop:disable Metrics/AbcSize
if ENV.fetch("_APPSIGNAL_DIAGNOSE", false)
internal_logger.info("Skipping start in diagnose context")
return
end
if started?
internal_logger.warn("Ignoring call to Appsignal.start after AppSignal has started")
return
end
if config_file_context?
internal_logger.warn(
"Ignoring call to Appsignal.start in config file context."
)
return
end
unless extension_loaded?
internal_logger.info("Not starting AppSignal, extension is not loaded")
return
end
internal_logger.debug("Loading AppSignal gem")
_load_config!
_start_logger
if config.valid?
if config.active?
@started = true
internal_logger.info "Starting AppSignal #{Appsignal::VERSION} " \
"(#{$PROGRAM_NAME}, Ruby #{RUBY_VERSION}, #{RUBY_PLATFORM})"
config.write_to_environment
Appsignal::Extension.start
Appsignal::Hooks.load_hooks
Appsignal::Loaders.start
if config[:enable_allocation_tracking] && !Appsignal::System.jruby?
Appsignal::Extension.install_allocation_event_hook
Appsignal::Environment.report_enabled("allocation_tracking")
end
Appsignal::Probes.start if config[:enable_minutely_probes]
collect_environment_metadata
@config.freeze
else
internal_logger.info("Not starting, not active for #{config.env}")
end
else
internal_logger.error("Not starting, no valid config for this environment")
end
nil
end
# PRIVATE METHOD. DO NOT USE.
#
# @param env_param [String, NilClass] Used by diagnose CLI to pass through
# the environment CLI option value.
# @api private
def _load_config!(env_param = nil, &block)
# Ensure it's not an empty string if it's a value
proper_env_param = env_param&.to_s&.strip
# Unset it if it's an empty string
proper_env_param = nil if proper_env_param&.empty?
context = Appsignal::Config::Context.new(
:env => Config.determine_env(env_param),
:root_path => Config.determine_root_path
)
# If there's a config/appsignal.rb file
if context.dsl_config_file?
if config
# When calling `Appsignal.configure` from an app, not the
# `config/appsignal.rb` file, with also a Ruby config file present.
message = "The `Appsignal.configure` helper is called from within an " \
"app while a `#{context.dsl_config_file}` file is present. " \
"The `config/appsignal.rb` file is ignored when the " \
"config is loaded with `Appsignal.configure` from within an app. " \
"We recommend moving all config to the `config/appsignal.rb` file " \
"or the `Appsignal.configure` helper in the app."
Appsignal::Utils::StdoutAndLoggerMessage.warning(message)
else
# Load it when no config is present
#
# We don't pass the `env_var` or `context.env` here so that the
# `Appsignal.configure` or `Appsignal.start` can figure out the
# environment themselves if it was not explicitly set.
# This way we prevent forcing an environment that's auto loaded on
# the to-be-loaded config file.
#
# The `(proper)_env_param` is only set when it's explicitly set,
# which means it needs to override any auto detected environment.
load_dsl_config_file(context.dsl_config_file, proper_env_param)
end
else
# Load config if no config file was found and no config is present yet
# This will load the config/appsignal.yml file automatically
@config ||= Config.new(context.root_path, context.env)
end
# Allow a block to be given to customize the config and override any
# loaded config before it's validated.
block.call(config) if block_given?
# Validate the config, if present
config&.validate
end
# Stop AppSignal's agent.
#
# Stops the AppSignal agent. Call this before the end of your program to
# make sure the agent is stopped as well.
#
# @example
# Appsignal.start
# # Run your application
# Appsignal.stop
#
# @param called_by [String] Name of the thing that requested the agent to
# be stopped. Will be used in the AppSignal log file.
# @return [void]
# @since 1.0.0
def stop(called_by = nil)
Thread.new do
if called_by
internal_logger.info("Stopping AppSignal (#{called_by})")
else
internal_logger.info("Stopping AppSignal")
end
Appsignal::Extension.stop
Appsignal::Probes.stop
Appsignal::CheckIn.stop
end.join
nil
end
# Configure the AppSignal Ruby gem using a DSL.
#
# Pass a block to the configure method to configure the Ruby gem.
#
# Each config option defined in our docs can be fetched, set and modified
# via a helper method in the given block.
#
# After AppSignal has started using {start}, the configuration can not be
# modified. Any calls to this helper will be ignored.
#
# This helper should not be used to configure multiple environments, like
# done in the YAML file. Configure the environment you want active when the
# application starts.
#
# @example Configure AppSignal for the application
# Appsignal.configure do |config|
# config.path = "/the/app/path"
# config.active = ENV["APP_ACTIVE"] == "true"
# config.push_api_key = File.read("appsignal_key.txt").chomp
# config.ignore_actions = ENDPOINTS.select { |e| e.public? }.map(&:name)
# config.request_headers << "MY_CUSTOM_HEADER"
# end
#
# @example Configure AppSignal for the application and select the environment
# Appsignal.configure(:production) do |config|
# config.active = true
# end
#
# @example Automatically detects the app environment
# # Tries to determine the app environment automatically from the
# # environment and the libraries it integrates with.
# ENV["RACK_ENV"] = "production"
#
# Appsignal.configure do |config|
# config.env # => "production"
# end
#
# @example Calling configure multiple times for different environments resets the configuration
# Appsignal.configure(:development) do |config|
# config.ignore_actions = ["My action"]
# end
#
# Appsignal.configure(:production) do |config|
# config.ignore_actions # => []
# end
#
# @example Load config without a block
# # This will require either ENV vars being set
# # or the config/appsignal.yml being present
# Appsignal.configure
# # Or for the environment given as an argument
# Appsignal.configure(:production)
#
# @param env_param [String, Symbol] The environment to load.
# @param root_path [String] The path to look the `config/appsignal.yml` config file in.
# Defaults to the current working directory.
# @yield [Config] Gives the {Config} instance to the block.
# @return [void]
# @see config
# @see Config
# @see https://docs.appsignal.com/ruby/configuration.html Configuration guide
# @see https://docs.appsignal.com/ruby/configuration/options.html Configuration options
def configure(env_param = nil, root_path: nil)
if Appsignal.started?
Appsignal.internal_logger
.warn("AppSignal is already started. Ignoring `Appsignal.configure` call.")
return
end
root_path_param = root_path
if params_match_loaded_config?(env_param, root_path_param)
config
else
@config = Config.new(
root_path_param || Config.determine_root_path,
Config.determine_env(env_param),
# If in the context of an `config/appsignal.rb` config file, do not
# load the `config/appsignal.yml` file.
# The `.rb` file is a replacement for the `.yml` file so it shouldn't
# load both.
:load_yaml_file => !config_file_context?
)
end
# When calling `Appsignal.configure` from a Rails initializer and a YAML
# file is present. We will not load the YAML file in the future.
if !config_file_context? && config.yml_config_file?
message = "The `Appsignal.configure` helper is called while a " \
"`config/appsignal.yml` file is present. In future versions the " \
"`config/appsignal.yml` file will be ignored when loading the " \
"config. We recommend moving all config to the " \
"`config/appsignal.rb` file, or the `Appsignal.configure` helper " \
"in Rails initializer file, and remove the " \
"`config/appsignal.yml` file."
Appsignal::Utils::StdoutAndLoggerMessage.warning(message)
end
config_dsl = Appsignal::Config::ConfigDSL.new(config)
return unless block_given?
yield config_dsl
config.merge_dsl_options(config_dsl.dsl_options)
nil
end
# @return [void]
def forked
return unless active?
Appsignal._start_logger
internal_logger.debug("Forked process, resubscribing and restarting extension")
Appsignal::Extension.start
nil
end
# Load an AppSignal integration.
#
# Load one of the supported integrations via our loader system.
# This will set config defaults and integratie with the library if
# AppSignal is active upon start.
#
# @example Load Sinatra integrations
# # First load the integration
# Appsignal.load(:sinatra)
# # Start AppSignal
# Appsignal.start
#
# @example Load Sinatra integrations and define custom config
# # First load the integration
# Appsignal.load(:sinatra)
#
# # Customize config
# Appsignal.configure do |config|
# config.ignore_actions = ["GET /ping"]
# end
#
#
# # Start AppSignal
# Appsignal.start
#
# @param integration_name [String, Symbol] Name of the integration to load.
# @return [void]
# @since 3.12.0
def load(integration_name)
Loaders.load(integration_name)
nil
end
# @api private
def get_server_state(key)
Appsignal::Extension.get_server_state(key)
end
# @api private
def in_memory_logger
@in_memory_logger ||=
Appsignal::Utils::IntegrationMemoryLogger.new.tap do |l|
l.formatter = log_formatter("appsignal")
end
end
# @api private
def internal_logger
@internal_logger ||= in_memory_logger
end
# @api private
def log_formatter(prefix = nil)
pre = "#{prefix}: " if prefix
proc do |severity, datetime, _progname, msg|
"[#{datetime.strftime("%Y-%m-%dT%H:%M:%S")} (process) " \
"##{Process.pid}][#{severity}] #{pre}#{msg}\n"
end
end
# Start the AppSignal internal logger.
#
# Sets the log level and sets the logger. Uses a file-based logger or the
# STDOUT-based logger. See the `:log` configuration option.
#
# @api private
# @return [void]
def _start_logger
if config && config[:log] == "file" && config.log_file_path
start_internal_file_logger(config.log_file_path)
else
start_internal_stdout_logger
end
internal_logger.level =
if config
config.log_level
else
Appsignal::Config::DEFAULT_LOG_LEVEL
end
return unless @in_memory_logger
messages = @in_memory_logger.messages_for_level(internal_logger.level)
internal_logger << messages.join
@in_memory_logger = nil
end
# Returns if the C-extension was loaded properly.
#
# @return [Boolean]
# @see Extension
# @since 1.0.0
def extension_loaded?
!!extension_loaded
end
# Returns if {.start} has been called before with a valid config to start
# AppSignal.
#
# @return [Boolean]
# @see Extension
# @since 3.12.0
def started?
defined?(@started) ? @started : false
end
# Returns the active state of the AppSignal integration.
#
# Conditions apply for AppSignal to be marked as active:
#
# - There is a config set on the {.config} attribute.
# - The set config is active {Config.active?}.
# - The AppSignal Extension is loaded {.extension_loaded?}.
#
# This logic is used within instrument helper such as {.instrument} so it's
# not necessary to wrap {.instrument} calls with this method.
#
# @example Do this
# Appsignal.instrument(..) do
# # Do this
# end
#
# @example Don't do this
# if Appsignal.active?
# Appsignal.instrument(..) do
# # Don't do this
# end
# end
#
# @return [Boolean]
# @since 0.2.7
def active?
config&.active? && extension_loaded?
end
# @api private
def dsl_config_file_loaded?
defined?(@dsl_config_file_loaded) ? true : false
end
# Check if the AppSignal Ruby gem has started successfully.
#
# If it has not (yet) started or encountered an error in the
# `config/appsignal.rb` config file during start up that prevented it from
# starting, it will raise a {Appsignal::NotStartedError}.
#
# If there an error raised from the config file, it will include it as the
# error cause of the raised error.
#
# @raise [Appsignal::NotStartedError]
# @return [void]
def check_if_started!
return if started?
begin
raise config_error if config_error?
rescue
# Raise the NotStartedError and make the config error the error cause
raise NotStartedError, config_error
end
# Raise the NotStartedError as normal
raise NotStartedError
end
private
def params_match_loaded_config?(env_param, root_path_param)
# No config present: can't match any config
return false unless config
# Check if the params, if present, match the loaded config
(env_param.nil? || config.env == env_param.to_s) &&
(root_path_param.nil? || config.root_path == root_path_param)
end
# Load the `config/appsignal.rb` config file, if present.
#
# If the config file has already been loaded once and it's trying to be
# loaded more than once, which should never happen, it will not do
# anything.
def load_dsl_config_file(path, env_param = nil)
return if defined?(@dsl_config_file_loaded)
begin
ENV["_APPSIGNAL_CONFIG_FILE_CONTEXT"] = "true"
ENV["_APPSIGNAL_CONFIG_FILE_ENV"] = env_param if env_param
@dsl_config_file_loaded = true
require path
rescue => error
@config_error = error
message = "Not starting AppSignal because an error occurred while " \
"loading the AppSignal config file.\n" \
"File: #{path.inspect}\n" \
"#{error.class.name}: #{error}"
Kernel.warn "appsignal ERROR: #{message}"
internal_logger.error "#{message}\n#{error.backtrace.join("\n")}"
ensure
unless Appsignal.config
# Ensure _a config object_ is present, even if something went wrong
# loading it or the file is empty. In this config file context, see
# the context env vars, it will intentionally not load the YAML file.
Appsignal.configure
# Disable if no config was loaded from the file but it is present
config[:active] = false
end
# Disable on config file error
config[:active] = false if defined?(@config_error)
ENV.delete("_APPSIGNAL_CONFIG_FILE_CONTEXT")
ENV.delete("_APPSIGNAL_CONFIG_FILE_ENV")
end
end
# Returns true if we're currently in the `config/appsignal.rb` file
# context.
def config_file_context?
ENV.fetch("_APPSIGNAL_CONFIG_FILE_CONTEXT", nil) == "true"
end
def start_internal_stdout_logger
@internal_logger = Appsignal::Utils::IntegrationLogger.new($stdout)
internal_logger.formatter = log_formatter("appsignal")
end
def start_internal_file_logger(path)
@internal_logger = Appsignal::Utils::IntegrationLogger.new(path)
internal_logger.formatter = log_formatter
rescue SystemCallError => error
start_internal_stdout_logger
internal_logger.warn "Unable to start internal logger with log path '#{path}'."
internal_logger.warn error
end
def collect_environment_metadata
Appsignal::Environment.report("ruby_version") do
"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
end
Appsignal::Environment.report("ruby_engine") { RUBY_ENGINE }
if defined?(RUBY_ENGINE_VERSION)
Appsignal::Environment.report("ruby_engine_version") do
RUBY_ENGINE_VERSION
end
end
Appsignal::Environment.report_supported_gems
end
end
end
require "appsignal/internal_errors"
require "appsignal/loaders"
require "appsignal/sample_data"
require "appsignal/environment"
require "appsignal/system"
require "appsignal/utils"
require "appsignal/extension"
require "appsignal/auth_check"
require "appsignal/config"
require "appsignal/event_formatter"
require "appsignal/hooks"
require "appsignal/probes"
require "appsignal/marker"
require "appsignal/custom_marker"
require "appsignal/garbage_collection"
require "appsignal/rack"
require "appsignal/rack/body_wrapper"
require "appsignal/rack/abstract_middleware"
require "appsignal/rack/instrumentation_middleware"
require "appsignal/rack/event_handler"
require "appsignal/integrations/railtie" if defined?(::Rails)
require "appsignal/transaction"
require "appsignal/version"
require "appsignal/transmitter"
require "appsignal/check_in"