Skip to content

Moesif/moesifdjangoexample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moesif Django Example

Django is a web application framework that many developers use to serve APIs.

Moesif is an API analytics and monitoring platform. moesifdjango is a middleware that makes integration with Moesif easy for Django based applications.

This is an example of django application with Moesif integrated. This example is based on the quick start tutorials of Django and Django rest framework.

Key files

moesifdjango's github readme already documented the steps for setup Moesif Django. But here is the key file where the Moesif integration is added:

  • mysite/settings.py added Moesif middleware related settings.

How to run this example with WSGI

  1. Setup virtual env if needed python -m venv ENV. Start the virtual env by source ENV/bin/activate

  2. Install packages:

    • Django 3 LTS: pip install -r requirements.txt
    • Django 4.2 LTS: pip install -r requirements-django4.txt
  3. Edit the mysite/setting.py to add your Moesif Application ID.

    MOESIF_MIDDLEWARE = {
        'APPLICATION_ID': 'Your Moesif Collector Application Id',
    }

    After you log into Moesif Portal, you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:

    a. Select the account icon to bring up the settings menu.

    b. Select Installation or API Keys.

    c. Copy your Moesif Application ID from the Collector Application ID field.

    Also make sure you specify your time zone in the TIME_ZONE setting in mysite/settings.py file.

  4. Run the service:

    python manage.py runserver
  5. See urls.py for some urls that you can hit the server with (e.g. http://localhost:8000/users), and the data should be captured in the corresponding Moesif account of the application id.

How to run this example with ASGI

  1. Setup virtual env if needed python -m venv ENV. Start the virtual env by source ENV/bin/activate

  2. Install packages:

    • Django 3 LTS: pip install -r requirements.txt
    • Django 4: pip install -r requirements-django4.txt

    Also install dependencies to related to asgi

    pip install --no-cache uvicorn gunicorn
  3. Be sure to edit the mysite/setting.py to add your Moesif Application ID.

    MOESIF_MIDDLEWARE = {
        'APPLICATION_ID': 'Your Moesif Collector Application Id',
    }

    After you log into Moesif Portal, you can get your Moesif Application ID during the onboarding steps. You can always access the Application ID any time by following these steps from Moesif Portal after logging in:

    a. Select the account icon to bring up the settings menu.

    b. Select Installation or API Keys.

    c. Copy your Moesif Application ID from the Collector Application ID field.

    Also make sure you specify your time zone in the TIME_ZONE setting in mysite/settings.py file.

  4. Run the service:

    gunicorn mysite.asgi:application -k uvicorn.workers.UvicornWorker
  5. See urls.py for some urls that you can hit the server with (e.g. http://localhost:8000/users), and the data should be captured in the corresponding Moesif account of the application id.

Note : If you get OperationalError with Exception Value: no such table: auth_user, that means the schema has not been generated in database yet. please run the following command line to resolve

python manage.py migrate

Tested Python and Django versions

Tested Python versions: 3.10.4, 3.12.3
Tested Django versions: 3.2.13 LTS, 4.2.0 LTS

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published