Skip to content

Commit

Permalink
feat: Make Rack install optional for sinatra
Browse files Browse the repository at this point in the history
This allows for scenarios when there are multiple Rack Applications mounted in the same builder and you want to avoid installing the Rack Events middleware multiple times in the stack.

A common example is a Rails application that mounts a Sinatra App in the routes file:

```ruby

Rails.application.routes.draw do
  mount Sinatra::Application, at: '/sinatra'
end

```

This results in the Rack middleware being installed multiple times.

Once by the Rails ActionPack instrumentation and then by the Sinatra application leading to multiple Rack spans being created in the same trace.

This change allows you to avoid this by setting the install_rack option to false when using Sinatra instrumentation and allowing users to manually configure the middleware in the stack as they see fit.
  • Loading branch information
arielvalentin committed Jun 16, 2024
1 parent 2230403 commit 2b8f182
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def render(_engine, data, *)
end
end
end
app.use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args)
app.use(Middlewares::TracerMiddleware)

Instrumentation.instance.install_middleware(app)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ module Sinatra
# The Instrumentation class contains logic to detect and install the Sinatra
# instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
install do |_|
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install({})
install do |config|
OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.install({}) if config[:install_rack]

::Sinatra::Base.register Extensions::TracerExtension
end

option :install_rack, true, validate: :boolean

present do
defined?(::Sinatra)
end

def install_middleware(app)
app.use(*OpenTelemetry::Instrumentation::Rack::Instrumentation.instance.middleware_args) if config[:install_rack]
app.use(Middlewares::TracerMiddleware)
end
end
end
end
Expand Down

0 comments on commit 2b8f182

Please sign in to comment.