Giter Site home page Giter Site logo

django-watchtower's Introduction

Django Watchtower

Collect hits metrics from Django.

How it works: numbers taken out from Django are stored in Redis and a collector saves them in some database(s)

Metrics: each hit is saved with fields ip, request time, query time, user_agent, geographical information and more

Install

pip install django-watchtower

Add to installed apps:

"django_user_agents",
"watchtower",

Add the middlewares:

MIDDLEWARE_CLASSES = (
   # ... other middlewares
 'django_user_agents.middleware.UserAgentMiddleware',
 'watchtower.middleware.HitsMiddleware',
)

Set the Django databases:

DATABASES = {
 'default': # ...,
 'hits': {
     'ENGINE': 'django.db.backends.sqlite3',
     'NAME': os.path.join(BASE_DIR, 'hits.sqlite3'),
 }
}

DATABASE_ROUTERS = ['watchtower.router.HitsRouter']

Add to settings.py:

# required
SITE_SLUG = "mysite"

# set the databases to use: required
WT_DATABASES = {
 # required: at least one database
 "default": {
     "type": "django",
     "hits_db": "hits" # name of a DATABASE in settings
 },

# defaults:
WT_REDIS = {
 "addr": "localhost:6379",
 "db": 0
}

Install the GeoIp tools (optional): get the geoip database

Unzip and add to settings.py:

GEOIP_PATH = "/my/geo/folder"

Make the migrations:

python3 manage.py migrate watchtower --database=hits

Additional settings

Exclude certain paths from hits recording:

WT_EXCLUDE = ["/path/not/recorded/"]
# default:
# ["/admin/jsi18n/", "/media/"]

Note: this will exclude all paths that start with the provided values

Change the default collector save interval:

WT_FREQUENCY = 30

Run the collector

python3 manage.py collect

Note: it is possible to save the data directly into the database not using Redis and the collector with the setting:

WT_COLLECTOR = False

Do not use this setting in production: it will not work when DEBUG is False

Collected data

{
 "site": "mysite",
 "user": "admin",
 "request_time": 35,
 "status_code": 200,
 "doc_size": 3912,
 "ip": "127.0.0.1",
 "user_agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0",
 "method": "GET",
 "view": "AddPostView",
 "module": "qcf.views",
 "is_superuser": true,
 "is_authenticated": true,
 "reason_phrase": "OK",
 "ua": {
  "os_version": "",
  "is_pc": true,
  "browser_version": "55.0",
  "is_mobile": false,
  "os": "Ubuntu",
  "is_tablet": false,
  "is_bot": false,
  "device": "Other",
  "is_touch": false,
  "browser": "Firefox"
 },
 "geo": {
  "latitude": 0,
  "postal_code": "",
  "country_code": "",
  "region": "",
  "dma_code": "",
  "country_name": "",
  "longitude": 0,
  "city": ""
 },
 "is_staff": true,
 "referer": "",
 "path": "/",
 "queries_time": 2,
 "num_queries": 1
}

django-watchtower's People

Contributors

synw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

priyadhoundiyal

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.