Skip to content

Instantly share code, notes, and snippets.

@sahid
Created July 5, 2011 10:39
Show Gist options
  • Save sahid/1064635 to your computer and use it in GitHub Desktop.
Save sahid/1064635 to your computer and use it in GitHub Desktop.
A simple way to log all of indexes used by the application
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2011 Sahid Orentino Ferdjaoui
import logging
import hashlib
from google.appengine.api import memcache
from google.appengine.api import apiproxy_stub_map
from google.appengine.ext import db
from google.appengine.datastore import datastore_index
"""A simple way to log all of indexes used by the application.
Just add this middleware in your django settings.
MIDDLEWARE_CLASSES = (
...
'middleware.hook.IndexesMiddleware',
...
)
"""
LOCK_KEY="<l>"
LOCK_TIME=3600
class _hook_indexes_stats(db.Model):
index = db.StringProperty(required=True, multiline=True)
name = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now_add=True, auto_now=True)
class IndexesMiddleware (object):
"""Store indexes used by the application.
Install:
Just add this middleware in your django settings.
MIDDLEWARE_CLASSES = (
...
'middleware.hook.IndexesMiddleware',
...
)
"""
def process_request (self, request):
def log_api_call(service, call, request, response):
if call == 'RunQuery':
required, kind, ancestor, props, eq_filters = \
datastore_index.CompositeIndexForQuery(request)
index = datastore_index.IndexYamlForQuery(kind, ancestor, props)
index_key = hashlib.sha1(index).hexdigest()
if not memcache.get(index_key):
if memcache.add(index_key, LOCK_KEY, LOCK_TIME):
logging.info("indexes: \n%s", index)
_hook_indexes_stats(
key_name=index_key,
name=kind,
index=index).put()
apiproxy_stub_map.apiproxy.GetPreCallHooks().Append(
'log_api_call', log_api_call, 'datastore_v3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment