Search code examples
racknewrelic

NewRelic::Rack middleware not exposing Sinatra::Base settings


I have a modular Sinatra app, where I use

run Rack::URLMap

to add various services, where each service is a Sinatra::Base

I've added a middleware in one of the services that uses the settings method. It seems that the NewRelic rack middlewares are not exposing this method.

class MyService < Sinatra::Base
  configure do
    set :optional_auth, [
      { method: :get, path: ''},
      { method: :get, path: '/:id'},
      { method: :get, path: '/:id/attachments'},
      { method: :get, path: '/:id/comments'}
    ]
    mime_type :json, 'application/json'
    use Rack::PostBodyContentTypeParser
    use MyMiddleware
  end

  get '/' ....

Locally, things work fine, however, when deployed, it seems that MyMiddleware is being added after NewRelic::Rack, so when the middleware is invoked, the @app is no longer MyService, it's NewRelic::Rack without the exposed settings method.

Has anyone else experienced this? I'm running the application with bundle exec puma


Solution

  • I was able to work around this by updating my middleware

        def initialize app, &block
          @app = app
          @block = block
        end
    
        def call env
          @block.call(env)
          do_something(env[:my_middleware_option])
          ...
          @app.call(env)
        end
    

    And initializing my middleware with

        use MyMiddleware do |env|
          env[:my_middleware_option] = "any data type"
        end