Module: AllStak::Integrations::Rails

Defined in:
lib/allstak/integrations/rails.rb

Class Method Summary collapse

Class Method Details

.insert_middleware(app) ⇒ Object

Insert the Rack middleware into the given Rails app’s middleware stack, unless it’s already present. Returns true when (now) present.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/allstak/integrations/rails.rb', line 34

def self.insert_middleware(app)
  stack = app.config.middleware
  mw = AllStak::Integrations::Rack::Middleware
  return true if middleware_present?(stack, mw)
  stack.use(mw)
  true
rescue => e
  # Never let observability wiring break Rails boot.
  warn("[AllStak] failed to insert Rack middleware: #{e.message}") if AllStak.respond_to?(:logger) && AllStak.logger&.debug?
  false
end

.install!Object

Auto-install the AllStak Rack middleware into a Rails application’s middleware stack so Rails apps get inbound request telemetry, trace propagation, and unhandled-exception capture WITHOUT any manual ‘config.middleware.use` wiring.

‘Railtie` is only defined (and the initializer only registered) when `Rails::Railtie` is available, so requiring this file is a no-op in non-Rails processes. The middleware-insertion itself is idempotent: the Rack stack rejects a middleware that’s already present, and we guard explicitly as well.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/allstak/integrations/rails.rb', line 16

def self.install!
  return false unless defined?(::Rails::Railtie)
  return true if defined?(AllStak::Integrations::Rails::Railtie)

  railtie_class = Class.new(::Rails::Railtie) do
    railtie_name "allstak" if respond_to?(:railtie_name)

    initializer "allstak.insert_middleware" do |app|
      AllStak::Integrations::Rails.insert_middleware(app)
    end
  end

  const_set(:Railtie, railtie_class)
  true
end

.middleware_present?(stack, mw) ⇒ Boolean

Best-effort idempotency check across Rails middleware-stack versions.

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/allstak/integrations/rails.rb', line 47

def self.middleware_present?(stack, mw)
  return stack.include?(mw) if stack.respond_to?(:include?)
  false
rescue
  false
end