logbrew-sdk 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logbrew"
4
+ require "rails/railtie"
5
+ require_relative "rails_integration"
6
+
7
+ module LogBrew
8
+ # Process-safe Rails integration installed by RailsRailtie.
9
+ module Rails
10
+ class << self
11
+ attr_reader :runtime
12
+
13
+ def install(application:, environment: ENV)
14
+ @installation_mutex ||= Mutex.new
15
+ @installation_mutex.synchronize do
16
+ return @runtime unless @runtime.nil?
17
+
18
+ configuration = Configuration.from_environment(
19
+ environment,
20
+ application_name: application_name(application),
21
+ rails_environment: ::Rails.env.to_s,
22
+ rails_version: ::Rails.version.to_s
23
+ )
24
+ @runtime = Runtime.new(
25
+ configuration,
26
+ on_error: ->(stage, error) { log_capture_failure(application, stage, error) }
27
+ )
28
+ register_shutdown
29
+ @runtime
30
+ end
31
+ end
32
+
33
+ def client
34
+ @runtime&.client
35
+ end
36
+
37
+ def delivery_health
38
+ @runtime&.delivery_health
39
+ end
40
+
41
+ def shutdown
42
+ @runtime&.shutdown
43
+ end
44
+
45
+ private
46
+
47
+ def application_name(application)
48
+ application_class = application.class
49
+ if application_class.respond_to?(:module_parent_name)
50
+ name = application_class.module_parent_name.to_s
51
+ return name unless name.empty?
52
+ end
53
+
54
+ name = application_class.name.to_s.sub(/::Application\z/, "")
55
+ name.empty? ? "rails" : name
56
+ end
57
+
58
+ def register_shutdown
59
+ return if @shutdown_registered
60
+
61
+ at_exit { shutdown }
62
+ @shutdown_registered = true
63
+ end
64
+
65
+ def log_capture_failure(application, stage, error)
66
+ logger = application.respond_to?(:logger) ? application.logger : nil
67
+ logger ||= ::Rails.logger if ::Rails.respond_to?(:logger)
68
+ return unless logger.respond_to?(:warn)
69
+
70
+ logger.warn("LogBrew Rails #{stage} failed (#{error.class.name}); application behavior was preserved")
71
+ rescue StandardError
72
+ nil
73
+ end
74
+ end
75
+ end
76
+
77
+ class RailsRailtie < ::Rails::Railtie
78
+ initializer "logbrew.install", after: :load_config_initializers do |application|
79
+ runtime = LogBrew::Rails.install(application: application)
80
+ middleware = application.config.middleware
81
+ if defined?(::ActionDispatch::ShowExceptions)
82
+ middleware.insert_after(
83
+ ::ActionDispatch::ShowExceptions,
84
+ LogBrew::Rails::RequestMiddleware,
85
+ runtime: runtime
86
+ )
87
+ else
88
+ middleware.use(LogBrew::Rails::RequestMiddleware, runtime: runtime)
89
+ end
90
+ end
91
+
92
+ config.after_initialize do |application|
93
+ runtime = LogBrew::Rails.runtime
94
+ next if runtime.nil? || !runtime.configuration.enabled?
95
+
96
+ reporter = LogBrew::Rails::ErrorReporter.new(runtime)
97
+ if ::Rails.respond_to?(:error) && ::Rails.error.respond_to?(:subscribe)
98
+ ::Rails.error.subscribe(reporter)
99
+ elsif application.respond_to?(:executor) && application.executor.respond_to?(:error_reporter)
100
+ error_reporter = application.executor.error_reporter
101
+ error_reporter.subscribe(reporter) if error_reporter.respond_to?(:subscribe)
102
+ end
103
+ end
104
+ end
105
+ end