Skip to content

feat: add option to allow user format the version from path in grape#1687

Open
xuan-cao-swi wants to merge 4 commits intoopen-telemetry:mainfrom
xuan-cao-swi:issues-1431
Open

feat: add option to allow user format the version from path in grape#1687
xuan-cao-swi wants to merge 4 commits intoopen-telemetry:mainfrom
xuan-cao-swi:issues-1431

Conversation

@xuan-cao-swi
Copy link
Copy Markdown
Contributor

Description

Based on issue #1431: when a Grape API has multiple versions (for example:

class V5 < Grape::API
  version 'v5', 'v6', 'v7', using: :path

  get '/other' do
    # ...
  end
end

—although using multiple versions in this way is not best practice and may be rare), the route string can become /api/[\"v5\", \"v6\", \"v7\"]//other, which, as the user points out, is not well formatted (see attributes).

attributes=
  {"http.request.method"=>"GET",
   "server.address"=>"localhost:3000",
   "url.scheme"=>"http",
   "url.path"=>"/api/v5/other",
   "user_agent.original"=>"curl/7.74.0",
   "sw.is_entry_span"=>true,
   "code.namespace"=>"V5",
   "http.route"=>"/api/[\"v5\", \"v6\", \"v7\"]//other",
   "http.response.status_code"=>200}

This PR adds a configuration option that allows users to format the version string as they prefer. For example, to produce /api/{v5|v6|v7}/other, a user can provide a lambda like:

OpenTelemetry::SDK.configure do |c|
  c.use 'OpenTelemetry::Instrumentation::Grape',
    {
      version_format: ->(version) {
        version.is_a?(Array) ? "{#{version.join('|')}}" : version.to_s
      }
    }
end

If the user does not provide a config option, the instrumentation keeps the original formatting (for example: "/api/[\"v5\", \"v6\", \"v7\"]//other").

@ericmustin
Copy link
Copy Markdown
Contributor

@muripic Would you have any time to take a look at this? I believe you contributed the Graph instrumentation (albeit, many years ago), your input would be super helpful.

At a high level I think we'd prefer to solve this within the instrumentation code itself rather than need to support another config option like a callable.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 7, 2025

👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this pull request will be closed eventually by the stale bot

@github-actions github-actions Bot added the stale Marks an issue/PR stale label Nov 7, 2025
@xuan-cao-swi xuan-cao-swi marked this pull request as ready for review November 7, 2025 19:02
@github-actions github-actions Bot removed the stale Marks an issue/PR stale label Nov 8, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 8, 2025

👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the keep label to hold stale off permanently, or do nothing. If you do nothing this pull request will be closed eventually by the stale bot

@github-actions github-actions Bot added the stale Marks an issue/PR stale label Dec 8, 2025
@github-actions github-actions Bot closed this Jan 7, 2026
@xuan-cao-swi xuan-cao-swi reopened this Jan 13, 2026
@xuan-cao-swi xuan-cao-swi added the keep Ensures stale-bot keeps this issue/PR open label Jan 13, 2026
@thompson-tomo thompson-tomo removed the stale Marks an issue/PR stale label Mar 8, 2026
Comment on lines -96 to +97
version = endpoint.routes.first.options[:version]&.to_s
version = endpoint.routes.first.options[:version]
version = config[:version_format].call(version)
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.

Rather than making it configurable could we not add the url.template attribute with a default of /:version/foos/ etc. That way http.route is mirroring what grape/server is providing as per semconv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

instrumentation-grape keep Ensures stale-bot keeps this issue/PR open

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants