---
title: Django Middleware Instrumentation Filtering
source: https://docs.newrelic.com/docs/apm/agents/python-agent/supported-features/django-middleware-filtering
---

The Python agent can now filter which Django middleware should be monitored.  By default, all middleware will be instrumented.

> #### ⚠️ IMPORTANT
>
> Middleware Instrumentation Filtering for Django is available for Python agent version 10.17.0 and higher.  By default, all middleware will be instrumented.  To change this configuration, check out our [documentation](https://docs.newrelic.com/docs/apm/agents/python-agent/configuration/python-agent-configuration/#middleware-filters).

## Disable all Django middleware [#django-middleware-disable]

Disabling instrumentation of middleware in Django can be done one of two ways:

1.  Set the `NEW_RELIC_INSTRUMENTATION_DJANGO_MIDDLEWARE_ENABLED` environment variable to `False`
2.  Set `instrumentation.middleware.django.enabled = false` in the configuration file.

## Django middleware configuration settings [#django-middleware-configuration-settings]

**instrumentation.middleware.django.include**

| Type:    | Space-Separated List of Strings |
| -------- | ------------------------------- |
| Default: | (none)                          |

If attributes are enabled, all attribute keys in this list will be sent.

**instrumentation.middleware.django.exclude**

| Type:    | Space-separated list of strings |
| -------- | ------------------------------- |
| Default: | (none)                          |

All attribute keys in this list will **not** be sent.

## Middleware exclude/include rules [#django-middleware-rules]

The agent follows these rules when determining which middleware to include or exclude for monitoring:

**Enabled takes precedence over include and exclude.**

The `instrumentation.middleware.FRAMEWORK.enabled` flags take precedence over include and exclude middlewares.

Example configuration:

````ini
instrumentation.middleware.django.enabled = false
instrumentation.middleware.django.include = one two
instrumentation.middleware.django.include = three four
```

Example output:

```
Middleware used in app: one, two, three, four
Middleware included for monitoring:
Middleware excluded for monitoring: one, two, three, four
```

````

**Middleware is included if the destination is enabled.**

If a destination is enabled, all middleware is sent to that destination by default.

All middleware monitoring defaults to `true`.

Example configuration:

````ini
instrumentation.middleware.django.enabled = true
instrumentation.middleware.django.exclude = baz
```

Example output:

```
Middleware used in app: foo, bar, baz
Middleware included for monitoring: foo, bar
Middleware excluded for monitoring: baz
```

````

**Exclude always supersedes include.**

If the same middleware is listed in the include and exclude lists, then middleware with the specified name will be excluded.

Example configuration:

````ini
instrumentation.middleware.django.enabled = true
instrumentation.middleware.django.include = foo bar
instrumentation.middleware.django.exclude = nerd bar
```

Example output:

```
Middleware used in app: foo, bar, nerd
Middleware included for monitoring: foo
Middleware excluded for monitoring: nerd, bar
```

````

**Middleware names are case sensitive.**

Middleware names are case-sensitive.

Example configuration:

````ini
instrumentation.middleware.django.enabled = true
instrumentation.middleware.django.exclude = username UsErNaMe
```

Example output:

```
Middleware used in app: username, Username, USERNAME, UsErNaMe, userNAME
Middleware included for monitoring:  Username, USERNAME, userNAME
Middleware excluded for monitoring:  username, UsErNaMe
```

````

**Use `\*` for wildcards.**

You can use an asterisk `*` at the end of a name as a wildcard. This will match a set of middleware with the same prefix.

Example configuration:

````ini
instrumentation.middleware.django.enabled = true
instrumentation.middleware.django.include = custom*
instrumentation.middleware.django.exclude = request.parameters.*
```

Example output:

```
Middleware used in app: custom, custom.key1, custom.key2, request.parameters., request.parameters.foo, request.parameters.bar
Middleware included for monitoring: custom, custom.key1, custom.key2
Middleware excluded for monitoring: request.parameters., request.parameters.foo, request.parameters.bar
```

````

**Most specific middleware name takes priority.**

If multiple include or exclude names affect the same middleware, the most specific name will have priority.

Example configuration:

````ini
instrumentation.middleware.django.enabled = true
instrumentation.middleware.django.include = request.parameters.foo
instrumentation.middleware.django.exclude = request.parameters.*
```

Example output:

```
Middleware used in app: request.parameters., request.parameters.foo, request.parameters.bar
Middleware included for monitoring: request.parameters.foo
Middleware included for monitoring: request.parameters., request.parameters.bar
```

````
