Skip to content

Capture route names for Grape 4 - #635

Merged
jrothrock merged 3 commits into
masterfrom
fix/grape-4-endpoint-config
Sep 23, 2026
Merged

jrothrock merged 3 commits into
masterfrom
fix/grape-4-endpoint-config

Conversation

@jrothrock

Copy link
Copy Markdown
Contributor

Summary

Grape 4 endpoints were all being reported as Grape/Unknown instead of their real route name (e.g. Grape/GET/WidgetAPI/widgets/:id), so every Grape transaction collapsed into one bucket in Scout.

Fixes #634.

Root cause

The instrument built the transaction name from Grape 3 internals — endpoint.options[:method], options[:for], options[:path]. Grape 4 moved that route metadata off the public options Hash onto the (protected) Endpoint#config (Grape::Endpoint::Options), and renamed options[:for] → endpoint.api. Under Grape 4 those options[...] reads return nil, .first raises, and the rescue falls back to Grape/Unknown.

Fix

Extract the name-building logic (previously duplicated across the alias-method and prepend wrappers) into a shared GrapeEndpointNaming.name_for helper that:

  • reads http_methods / api / path from config on Grape 4, and
  • falls back to the legacy options[...] reads on Grape 3,

preserving the existing name format and the rescue-to-Grape/Unknown fallback.

Tests

Adds test_endpoint_name_reflects_the_route, which drives a request through an endpoint and asserts the captured Controller layer name is the real route name (and specifically not Grape/Unknown). It fails on the old code and passes with the fix.

CI: json pin

Separately, json 3.0 (released 2026-09) removed the quirks_mode keyword that Grape 3's ActiveSupport JSON formatter still passes, which broke the Ruby 3.0 / gems/instruments.gemfile matrix cell on any JSON-rendering request (unrelated to this change, but it turns that cell red). Pinned json < 3 in that gemfile to keep the Grape 3 cell green until it moves off Grape 3.

Verification

Verified end-to-end against a real multi-endpoint Grape 4 app: booted it, hit GET/POST/DELETE routes (incl. path params and nested namespaces) over real HTTP, and confirmed the reported Scout checkin payload contained correct per-endpoint metrics and slow-transaction traces — each named Grape/<METHOD>/<API>/<path>, none Grape/Unknown.

jrothrock and others added 3 commits September 22, 2026 19:33
Grape 4 moved endpoint route metadata (method/path/api) off the public
`options` Hash and onto the protected `Endpoint#config` object
(Grape::Endpoint::Options). Reading the old `options[:method]`,
`options[:for]`, and `options[:path]` keys under Grape 4 silently
returns nil, so every request was named "Grape/Unknown" instead of the
real endpoint name.

Extract the name-building logic (duplicated between the alias-method
and prepend instrumentation paths) into a shared
GrapeEndpointNaming.name_for helper that reads from `config` when
available (Grape 4) and falls back to the old `options` reads
otherwise (Grape 3), preserving the existing name format and
rescue-to-"Grape/Unknown" fallback behavior.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Testing the Grape 4 fix required a bundle install with a local
--path for gems/instruments-ruby4.gemfile (no writable system gem
dir in the sandbox); ignore the resulting vendor directory so it
never gets picked up as repo content.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
json 3.0 (2026-09) removed the quirks_mode keyword that Grape 3's
ActiveSupport JSON formatter still passes, breaking the Ruby 3.0 /
gems/instruments.gemfile matrix cell on any JSON-rendering request.
Pin to the maintained 2.x line until that cell moves off Grape 3.
@jrothrock jrothrock changed the title Capture real Grape route names under Grape 4 (fixes Grape/Unknown) Capture Grape route names for Grape 4 (fixes Grape/Unknown) Sep 22, 2026
@jrothrock jrothrock changed the title Capture Grape route names for Grape 4 (fixes Grape/Unknown) Capture Grape route names for Grape 4 Sep 22, 2026
@jrothrock jrothrock changed the title Capture Grape route names for Grape 4 Capture route names for Grape 4 Sep 22, 2026

@quinnmil quinnmil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍇

@jrothrock
jrothrock merged commit e014d7b into master Sep 23, 2026
42 checks passed
@jrothrock
jrothrock deleted the fix/grape-4-endpoint-config branch September 23, 2026 21:12
@jrothrock jrothrock mentioned this pull request Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support grape version 4 Endpoint#config

2 participants