# WSGI Middleware

Python

WSGI (Web Server Gateway Interface) is a specification that defines the protocol between web servers and Python web applications.

The Blackfire Python SDK contains a middleware easing the integration with WSGI web applications.

The integrations we provide for [Django](django), [Flask](flask), [Odoo](odoo) are extending the `BlackfireWSGIMiddleware`. They could [serve as examples](https://github.com/blackfireio/python-sdk/tree/master/hooks)helping the creation of a new middleware.

Integrations with other frameworks could benefit the Blackfire community. Please consider [contributing your code](https://support.blackfire.upsun.com).

## Installation

The Blackfire Python SDK is available [with the Blackfire Python Package](../../up-and-running/installation).

## Configuration

Create a class extending the `BlackfireWSGIMiddleware`.

The `FRAMEWORK` variable must be set; it should describe the instrumented application.

```python
from blackfire.hooks.wsgi import BlackfireWSGIMiddleware

class BlackfireFooBarMiddleware(BlackfireWSGIMiddleware):

    FRAMEWORK = 'Foo-Bar'

    ...
```

## Monitoring

[Requires Production Plan](https://www.blackfire.io/pricing)

A `get_view_name` method must be defined to have the requests of your WSGI-based application be instrumented by [Blackfire Monitoring](../../monitoring-cookbooks/index).

This function is required only for the monitoring of your application. Its definition can be omitted if you are not considering it.

The `get_view_name` method retrieves the view name executing or handling the HTTP request. Blackfire Monitoring relies on this information to [group requests in the monitoring dashboard](../../monitoring-cookbooks/naming-transactions).

If it returns `None`, the viewname will be grouped with other [Unnamed Transactions](../../monitoring-cookbooks/naming-transactions#monitoring_unnamed_transactions)

The function receives 1 parameter:

- `environ` is a [dictionary](https://peps.python.org/pep-0333/#environ-variables)that contains the HTTP request headers and other metadata.

The function should return a `string` or `None`.

```python
from blackfire.hooks.wsgi import BlackfireWSGIMiddleware

class BlackfireFooBarMiddleware(BlackfireWSGIMiddleware):

    FRAMEWORK = 'Foo-Bar'

    def get_view_name(self, environ):
        # implementation ...
```
