Skip to main content

Moesif Middleware for Python WSGI based flatforms (Flask, Bottle & Others)

Project description

# Moesif Middleware for Python WSGI based Frameworks

[WSGI (Web Server Gateway Interface)](https://wsgi.readthedocs.io/en/latest/)
is a standard (PEP 3333) that describes
how a web server communicates with web applications. Many Python Frameworks
are build on top of WSGI, such as [Flask](http://flask.pocoo.org/),
[Bottle](https://bottlepy.org/docs/dev/), [Pyramid](https://trypyramid.com/) etc.
Moesif WSGI Middleware help APIs that are build on top of these Frameworks to
easily integrate with [Moesif](https://www.moesif.com).

## How to install

```shell
pip install moesifwsgi
```

## How to use

### Flask

Wrap your wsgi_app with the Moesif middleware.

```python
from moesifwsgi import MoesifMiddleware

moesif_settings = {
'APPLICATION_ID': 'Your application id'
}

app.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)

```

You can find your Application Id from [_Moesif Dashboard_](https://www.moesif.com/) -> _Top Right Menu_ -> _App Setup_

### Bottle
Wrap your bottle app with the Moesif middleware.

```python

from moesifwsgi import MoesifMiddleware

app = bottle.Bottle()

moesif_settings = {
'APPLICATION_ID': 'Your application id',
}

bottle.run(app=MoesifMiddleware(app, moesif_settings))

```

### Pyramid


```python
from pyramid.config import Configurator
from moesifwsgi import MoesifMiddleware

if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/')
config.scan()
app = config.make_wsgi_app()

# configure your moesif settings
moesif_settings = {
'APPLICATION_ID': 'Your application id',
'DEBUG': True
# ... other options see below.
}
# Put middleware
app = MoesifMiddleware(app, moesif_settings)

server = make_server('0.0.0.0', 8080, app)
server.serve_forever()

```
### Other WSGI frameworks

If you are using a framework that is built on top of WSGI, it should work just by adding the Moesif middleware.
Please read the documentation for your specific framework on how to add middleware.

## Configuration options

#### __`APPLICATION_ID`__
(__required__), _string_, is obtained via your Moesif Account, this is required.

#### __`SKIP`__
(optional) _(app, environ) => boolean_, a function that takes a wsgi app and an environ, and returns true if you want to skip this particular event.

#### __`IDENTIFY_USER`__
(optional, but highly recommended) _(request, response) => string_, a function that takes an app and an environ, and returns a string that is the user id used by your system. While Moesif tries to identify users automatically,
but different frameworks and your implementation might be very different, it would be helpful and much more accurate to provide this function.

#### __`GET_SESSION_TOKEN`__
(optional) _(request, response) => string_, a function that takes an app and an environ, and returns a string that is the session token for this event. Again, Moesif tries to get the session token automatically, but if you setup is very different from standard, this function will be very help for tying events together, and help you replay the events.

#### __`MASK_EVENT_MODEL`__
(optional) _(EventModel) => EventModel_, a function that takes an EventModel and returns an EventModel with desired data removed. Use this if you prefer to write your own mask function than use the string based filter options: REQUEST_BODY_MASKS, REQUEST_HEADER_MASKS, RESPONSE_BODY_MASKS, & RESPONSE_HEADER_MASKS. The return value must be a valid EventModel required by Moesif data ingestion API. For details regarding EventModel please see the [Moesif Python API Documentation](https://www.moesif.com/docs/api?python).

#### __'DEBUG'__

(optional) _boolean_, a flag to see debugging messages.

### Example:

```python
def identifyUser(app, environ):
# if your setup do not use the standard request.user.username
# return the user id here
return "user_id_1"

def should_skip(app, environ):
if "healthprobe" in environ.get('PATH_INFO', ''):
return True
else:
return False

def get_token(app, environ):
# if your setup do not use the standard Django method for
# setting session tokens. do it here.
return "token"

def mask_event(eventmodel):
# do something to remove sensitive fields
# be sure not to remove any required fields.
return eventmodel

moesif_settings = {
'APPLICATION_ID': 'Your application id',
'DEBUG': False,
'IDENTIFY_USER': identifyUser,
'GET_SESSION_TOKEN': get_token,
'SKIP': should_skip,
'MASK_EVENT_MODEL': mask_event,
}

app.wsgi_app = MoesifMiddleware(app.wsgi_app, moesif_settings)

```


## Other integrations

To view more more documentation on integration options, please visit __[the Integration Options Documentation](https://www.moesif.com/docs/getting-started/integration-options/).__


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moesifwsgi-1.0.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

moesifwsgi-1.0.0-py2.py3-none-any.whl (10.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file moesifwsgi-1.0.0.tar.gz.

File metadata

  • Download URL: moesifwsgi-1.0.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for moesifwsgi-1.0.0.tar.gz
Algorithm Hash digest
SHA256 943f00dc9ab61fcf5afb1b3406f2a8946b09a03528010c4728b2a4146a06e837
MD5 4ec9bca391f3f0eaccbd954b0cd60ef7
BLAKE2b-256 32468a8121aa3280815d1ccb6f409fd239e99ef036e9eb2e2c7d7110c01e4af9

See more details on using hashes here.

Provenance

File details

Details for the file moesifwsgi-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for moesifwsgi-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ac95c0de2ae10ea9a0920b4372d201c64fe475037200e51e599a21fba34773c9
MD5 f1ae4ea78a68c0caa630b21db3d4df9d
BLAKE2b-256 158a0f8f60fe27baa6291474c3496582da00da3c03f268ef115520659c0ff74e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page