Skip to content

Commit 7dd1c5d

Browse files
authored
feat: make the install of rack instrumentation by grape instrumentation optional (#1043)
* feat: optional rack instrumentation install by grape instrumentation In some circumstances we may want to defer the installation of the rack instrumentation that is orchestrated during the grape instrumentation installation. For example, we may want to customise the installation of the rack instrumentation or we can rely on the installation being orchestrated by another framework's instrumentation (such as when Grape is mounted with Rails). This change allows for Grape to skip the installation of the Rack instrumentation so that it can manually installed at a later time. * move the comments above the class name for RubyDocs
1 parent e14d6b0 commit 7dd1c5d

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

instrumentation/grape/lib/opentelemetry/instrumentation/grape/instrumentation.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ module OpenTelemetry
88
module Instrumentation
99
module Grape
1010
# The Instrumentation class contains logic to detect and install the Grape instrumentation
11+
# # Configuration keys and options
12+
# ## `:ignored_events`
13+
#
14+
# Default is `[]`. Specifies which ActiveSupport::Notifications events published by Grape to ignore.
15+
# Ignored events will not be published as Span events.
16+
#
17+
# ## `:install_rack`
18+
#
19+
# Default is `true`. Specifies whether or not to install the Rack instrumentation as part of installing the Grape instrumentation.
20+
# This is useful in cases where you have multiple Rack applications but want to manually specify where to insert the tracing middleware.
1121
class Instrumentation < OpenTelemetry::Instrumentation::Base
1222
# Minimum Grape version needed for compatibility with this instrumentation
1323
MINIMUM_VERSION = Gem::Version.new('1.2.0')
@@ -27,6 +37,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
2737
end
2838

2939
option :ignored_events, default: [], validate: :array
40+
option :install_rack, default: true, validate: :boolean
3041

3142
private
3243

@@ -35,6 +46,8 @@ def gem_version
3546
end
3647

3748
def install_rack_instrumentation
49+
return unless config[:install_rack]
50+
3851
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install({})
3952
end
4053

instrumentation/grape/test/opentelemetry/instrumentation/grape_test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,5 +418,56 @@ class IgnoredEventAPI < Grape::API
418418
_(events_per_name('grape.endpoint_render').length).must_equal 0
419419
end
420420
end
421+
422+
describe 'when install_rack is set to false' do
423+
class BasicAPI < Grape::API
424+
format :json
425+
get :hello do
426+
{ message: 'Hello, world!' }
427+
end
428+
end
429+
430+
let(:config) { { install_rack: false } }
431+
432+
let(:app) do
433+
builder = Rack::Builder.app do
434+
run BasicAPI
435+
end
436+
Rack::MockRequest.new(builder)
437+
end
438+
439+
let(:request_path) { '/hello' }
440+
let(:expected_span_name) { 'HTTP GET /hello' }
441+
442+
describe 'missing rack installation' do
443+
it 'disables tracing' do
444+
app.get request_path
445+
_(exporter.finished_spans).must_be_empty
446+
end
447+
end
448+
449+
describe 'when rack is manually installed' do
450+
let(:app) do
451+
build_rack_app(BasicAPI)
452+
end
453+
454+
before do
455+
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install
456+
end
457+
458+
it 'creates a span' do
459+
app.get request_path
460+
_(exporter.finished_spans.first.attributes).must_equal(
461+
'code.namespace' => 'BasicAPI',
462+
'http.method' => 'GET',
463+
'http.host' => 'unknown',
464+
'http.scheme' => 'http',
465+
'http.target' => '/hello',
466+
'http.route' => '/hello',
467+
'http.status_code' => 200
468+
)
469+
end
470+
end
471+
end
421472
end
422473
end

0 commit comments

Comments
 (0)