Question

Because of the javascript injected due to newrelic which changes on every requests, the content of the page is changing, thus forcing a new etag to be generated everytime.

I understand that the Rack::Etag middleware needs to be before the newrelic middlewares, but I just can't find the newrelic middlewares. As per the documentation of newrelic_rpm, it says that for rails, the gem will include the middlewares, however on running rake middleware, I do not see any newrelic middlewares.

I can add the middlewares myself, but is there a better way?

Was it helpful?

Solution

I work for New Relic.

The reason that New Relic's middlewares are not showing up when running rake middleware is that they are conditionally inserted into the middleware stack. These middlewares are inserted only if the agent is configured to run in the current environment. You can force New Relic's middlewares to be inserted when running rake middleware in order to inspect the middleware stack by setting NEW_RELIC_AGENT_ENABLED=true on the command line when starting the rake task.

Adding the following code to config/application.rb should ensure that the Rack::ETag middleware has a chance to calculate and inject the ETag before the browser monitoring middleware injects its dynamic content:

 config.after_initialize do
      config.middleware.delete "Rack::ETag"
      config.middleware.insert_after "NewRelic::Rack::BrowserMonitoring", "Rack::ETag"
    end

The reason the JavaScript code injected into responses by the NewRelic::Rack::BrowserMonitoring middleware is dynamic is that it contains timings of how long the response took to generate on the server-side, and (if applicable) how long it was queued before reaching the Rails stack. These timings will vary with each incoming request. If ETags are generated based on hashing the page content before the dynamic information is inserted, then out-of-date server-side timings will potentially be used when a response is serviced from the cache. You can read details about how this is handled by New Relic here: https://newrelic.com/docs/features/how-does-real-user-monitoring-work#cached-pages

This is also a nice overview of ordering your middleware correctly: http://verboselogging.com/2010/01/20/proper-rack-middleware-ordering

If you need more in-depth help, please open up a ticket with us by emailing support@newrelic.com

OTHER TIPS

Another option:

  1. Set browser_monitoring.auto_instrument: false in the newrelic.yml file to disable automatic insertion of the BrowserMonitoring middleware
  2. Add the following code to config/application.rb:

    config.middleware.delete "Rack::ETag"

    require 'new_relic/rack/browser_monitoring'

    config.middleware.use NewRelic::Rack::BrowserMonitoring

    config.middleware.use "Rack::ETag"

This will remove the ETag middleware, append the BrowserMonitoring middleware, and then append the ETag middleware again so that it runs before BrowserMonitoring injects its dynamic payload.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top