---
title: Rack middlewares
source: https://docs.newrelic.com/docs/apm/agents/ruby-agent/instrumented-gems/rack-middlewares
---

The Ruby agent automatically instruments [Rack](http://rack.github.io/) middlewares. If you are unfamiliar with the basics of Rack middlewares, review the [resources linked by the Rails on Rack guide](http://guides.rubyonrails.org/rails_on_rack.html#resources). Additionally, the Ruby agent provides some features via Rack middlewares:

-   [Distributed traces](https://docs.newrelic.com/docs/distributed-tracing/enable-configure/language-agents-enable-distributed-tracing/)
-   Auto-instrumentation for [browser monitoring](https://docs.newrelic.com/docs/agents/ruby-agent/features/page-load-timing-ruby)

New Relic automatically installs these middlewares for Rails and Sinatra.

## Rack instrumentation [#instrumentation]

The two most common ways to configure Rack middlewares are the `Rack::Builder` API (most often from **config.ru**) and Rails' middleware stack configuration:

**Rack::Builder**

Middlewares in your **config.ru** file are configured using `Rack::Builder`. For the Ruby agent to instrument middlewares from `Rack::Builder`, your app must run version 1.1.0 or higher of the `rack` gem. This is the most common use of middlewares with Sinatra or pure-rack applications.

**Rails middlewares**

Rails uses its own class (`ActionDispatch::MiddlewareStack`) instead of `Rack::Builder` to configure middlewares. Even if you haven't explicitly added middlewares to your Rails application, many components of Rails itself are implemented as middleware, so middleware data will appear by default.

The Ruby agent automatically instruments middlewares added via `ActionDispatch::MiddlewareStack` on **Rails 3.0 or higher**. For more information about configuring middlewares with Rails, see the [Ruby on Rails guide](http://guides.rubyonrails.org/rails_on_rack.html).

## Viewing middleware data [#viewing_middleware_data]

You can view middleware data in APM.

**In the APM Summary page**

The main chart on your app's [APM **Summary** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/applications-overview-dashboard) includes a purple bar that shows average time per request spent in all Rack middlewares for your application.

![web_transactions.png](https://docs.newrelic.com/images/apm_screenshot-crop_web-transactions.webp "web_transactions.png")

**APM > (selected application) > Summary:** Middleware time appears in purple on your app's main Overview chart.

**In the APM Transactions page**

You can also see time for individual middlewares for a specific transaction name from your app's APM [**Transactions** page](https://docs.newrelic.com/docs/apm/applications-menu/monitoring/transactions-page).

![transactions_page.png](https://docs.newrelic.com/images/apm_screenshot-full_transactionspagesummary.webp "transactions_page.png")

**APM > (selected application) > Monitor > Transactions > (selected transaction) > Trace details:** Here is an example of middleware time for a selected transaction for your app.

**In APM transaction trace summary**

Transaction traces also capture detailed middleware call information.

![trace_summary.png](https://docs.newrelic.com/images/apm_screenshot-full_trace-summary.webp "trace_summary.png")

**APM > (selected application) > Monitor > Transactions > (selected transaction trace):** Here is an example of middleware details in a transaction trace.

## Disabling Rack instrumentation [#disabling]

If you do not want to instrument Rack middlewares, you may disable Rack middleware instrumentation with the [`disable_middleware_instrumentation`](https://docs.newrelic.com/docs/agents/ruby-agent/installation-configuration/ruby-agent-configuration) setting. You can also [ignore specific transactions](https://docs.newrelic.com/docs/agents/ruby-agent/installation-configuration/ignoring-specific-transactions).

## Installing Ruby agent middlewares manually [#manual]

The Ruby agent's implementation of New Relic's [cross application tracing](https://docs.newrelic.com/docs/apm/traces/cross-application-traces/cross-application-tracing) feature uses Rack middleware instrumentation to read and write HTTP headers that are necessary to pass information between monitored applications. If you are using Sinatra, have disabled middleware instrumentation as described above, and want to use cross application tracing, you must manually add the `NewRelic::Rack::AgentHooks` middleware to your middleware stack.

> #### ⚠️ IMPORTANT
>
> As of version 8.0.0, cross application tracing is deprecated in favor of [distributed tracing](https://docs.newrelic.com/docs/distributed-tracing/enable-configure/language-agents-enable-distributed-tracing/). When enabled, distributed tracing is automatically configured for all rack-based apps without the need for an additional middleware.
>
> If you would like to continue using cross application tracing, you will need to [update your configuration](https://docs.newrelic.com/docs/agents/ruby-agent/features/cross-application-tracing-ruby#configuration).

## Manual Rack instrumentation [#manual_instrumentation]

Earlier versions of the Ruby agent supported manually instrumenting Rack middlewares via the `NewRelic::Agent::Instrumentation::Rack` module. This instrumentation has been removed and isn't guaranteed to work in Ruby agent version 4.0.0 or higher. This form of manual instrumentation is unncessary with automatic middleware instrumentation. We recommend that you remove references to this module from your code after upgrading to 3.9.0 or higher.
