Skip to content

Commit

Permalink
feat: make the install of rack instrumentation by grape instrumentati…
Browse files Browse the repository at this point in the history
…on 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
  • Loading branch information
chrisholmes authored Jul 2, 2024
1 parent e14d6b0 commit 7dd1c5d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ module OpenTelemetry
module Instrumentation
module Grape
# The Instrumentation class contains logic to detect and install the Grape instrumentation
# # Configuration keys and options
# ## `:ignored_events`
#
# Default is `[]`. Specifies which ActiveSupport::Notifications events published by Grape to ignore.
# Ignored events will not be published as Span events.
#
# ## `:install_rack`
#
# Default is `true`. Specifies whether or not to install the Rack instrumentation as part of installing the Grape instrumentation.
# This is useful in cases where you have multiple Rack applications but want to manually specify where to insert the tracing middleware.
class Instrumentation < OpenTelemetry::Instrumentation::Base
# Minimum Grape version needed for compatibility with this instrumentation
MINIMUM_VERSION = Gem::Version.new('1.2.0')
Expand All @@ -27,6 +37,7 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
end

option :ignored_events, default: [], validate: :array
option :install_rack, default: true, validate: :boolean

private

Expand All @@ -35,6 +46,8 @@ def gem_version
end

def install_rack_instrumentation
return unless config[:install_rack]

OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install({})
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,5 +418,56 @@ class IgnoredEventAPI < Grape::API
_(events_per_name('grape.endpoint_render').length).must_equal 0
end
end

describe 'when install_rack is set to false' do
class BasicAPI < Grape::API
format :json
get :hello do
{ message: 'Hello, world!' }
end
end

let(:config) { { install_rack: false } }

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

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

describe 'missing rack installation' do
it 'disables tracing' do
app.get request_path
_(exporter.finished_spans).must_be_empty
end
end

describe 'when rack is manually installed' do
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' => '/hello',
'http.route' => '/hello',
'http.status_code' => 200
)
end
end
end
end
end

0 comments on commit 7dd1c5d

Please sign in to comment.