Class: Beacon::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/beacon/rails.rb

Defined Under Namespace

Modules: ForkHook

Constant Summary collapse

ROUTE_FORMAT_SUFFIX =
"(.:format)".freeze

Class Method Summary collapse

Class Method Details

.route_template_for(env, payload) ⇒ Object

Extract “<METHOD> <path-template>” from a start_processing payload. Returns nil when no template is available — the middleware then falls through to Beacon::PathNormalizer, which is the documented cross-client fallback and emits the same shape. We deliberately do not emit a controller#action string here: mixing two label shapes on one dashboard produces confusing groupings.

Source of truth: ActionDispatch::Request#route_uri_pattern (Rails 7.1+). This is a method on the request object, not an env key — the matched pattern is stored on the request instance by the router and read via the public accessor.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/beacon/rails.rb', line 145

def self.route_template_for(env, payload)
  method  = env["REQUEST_METHOD"]
  request = payload[:request]
  return nil unless method && request && request.respond_to?(:route_uri_pattern)

  pattern = request.route_uri_pattern
  return nil unless pattern && !pattern.empty?

  # Strip the "(.:format)" tail without allocating on the common path.
  if pattern.end_with?(ROUTE_FORMAT_SUFFIX)
    pattern = pattern[0, pattern.length - ROUTE_FORMAT_SUFFIX.length]
  end
  "#{method} #{pattern}"
end