Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def path(endpoint)
return '' unless endpoint.routes

namespace = endpoint.routes.first.namespace
version = endpoint.routes.first.options[:version]&.to_s
version = endpoint.routes.first.options[:version]
version = config[:version_format].call(version)
prefix = endpoint.routes.first.options[:prefix]&.to_s
parts = [prefix, version] + namespace.split('/') + endpoint.options[:path]
parts.reject { |p| p.nil? || p.empty? || p.eql?('/') }.join('/').prepend('/')
Expand All @@ -110,6 +111,10 @@ def formatter_type(formatter)
def built_in_grape_formatter?(formatter)
formatter.respond_to?(:name) && formatter.name.include?('Grape::Formatter')
end

def config
Grape::Instrumentation.instance.config
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base

option :ignored_events, default: [], validate: :array
option :install_rack, default: true, validate: :boolean
option :version_format, default: ->(version) { version&.to_s }, validate: :callable

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,76 @@ class BasicAPI < Grape::API
end
end
end

describe 'when with multiple version in api' do
class BasicAPI < Grape::API
version 'v5', 'v6', 'v7', using: :path
format :json

get :hello do
{ message: 'Hello, world!' }
end
end

let(:app) do
builder = Rack::Builder.app do
run BasicAPI
end
Rack::MockRequest.new(builder)
end

let(:request_path) { '/v5/hello' }
let(:expected_span_name) { 'HTTP GET /hello' }

describe 'default version_format' do
let(:config) { { install_rack: false } }

let(:app) do
build_rack_app(BasicAPI)
end

before do
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install
end

it 'creates a span' do
app.get request_path
_(exporter.finished_spans.first.attributes).must_equal(
'code.namespace' => 'BasicAPI',
'http.method' => 'GET',
'http.host' => 'unknown',
'http.scheme' => 'http',
'http.target' => '/v5/hello',
'http.route' => '/["v5", "v6", "v7"]/hello',
'http.status_code' => 200
)
end
end

describe 'customized version_format' do
let(:config) { { install_rack: false, version_format: ->(version) { version.is_a?(Array) ? "{#{version.join('|')}}" : version.to_s } } }

let(:app) do
build_rack_app(BasicAPI)
end

before do
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install
end

it 'creates a span' do
app.get request_path
_(exporter.finished_spans.first.attributes).must_equal(
'code.namespace' => 'BasicAPI',
'http.method' => 'GET',
'http.host' => 'unknown',
'http.scheme' => 'http',
'http.target' => '/v5/hello',
'http.route' => '/{v5|v6|v7}/hello',
'http.status_code' => 200
)
end
end
end
end
end