mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 14:52:15 +00:00
Added some request stats into eve_proxy, export via the munin command
This commit is contained in:
@@ -4,8 +4,11 @@ import urllib, urllib2
|
|||||||
from hashlib import sha1
|
from hashlib import sha1
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
|
|
||||||
from django.db import models, IntegrityError
|
from django.db import models, IntegrityError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
from eve_proxy.exceptions import *
|
from eve_proxy.exceptions import *
|
||||||
|
|
||||||
# API URL, can be overriden in the configuration
|
# API URL, can be overriden in the configuration
|
||||||
@@ -17,6 +20,11 @@ ROLLBACK_ERRORS = range(516, 902)
|
|||||||
# Errors ignored if encountered, as they're valid responses in some cases
|
# Errors ignored if encountered, as they're valid responses in some cases
|
||||||
IGNORED_ERRORS = range(200, 223)
|
IGNORED_ERRORS = range(200, 223)
|
||||||
|
|
||||||
|
def stat_update_count(key, incr=1):
|
||||||
|
"""Increment a key on the Cache, for stats monitoring"""
|
||||||
|
if getattr(settings, 'EVE_PROXY_STATS', False) and len(getattr(settings, 'CACHES', {})):
|
||||||
|
cache.incr(key, incr)
|
||||||
|
|
||||||
class CachedDocumentManager(models.Manager):
|
class CachedDocumentManager(models.Manager):
|
||||||
"""
|
"""
|
||||||
This manager handles querying or retrieving CachedDocuments.
|
This manager handles querying or retrieving CachedDocuments.
|
||||||
@@ -67,6 +75,7 @@ class CachedDocumentManager(models.Manager):
|
|||||||
|
|
||||||
if created or not doc.cached_until or datetime.utcnow() > doc.cached_until or no_cache:
|
if created or not doc.cached_until or datetime.utcnow() > doc.cached_until or no_cache:
|
||||||
|
|
||||||
|
stat_update_count('eve_proxy_api_requests')
|
||||||
req = urllib2.Request(url)
|
req = urllib2.Request(url)
|
||||||
# Add a header with the admin information in, so CCP can traceback the requests if needed
|
# Add a header with the admin information in, so CCP can traceback the requests if needed
|
||||||
if settings.ADMINS:
|
if settings.ADMINS:
|
||||||
@@ -81,11 +90,13 @@ class CachedDocumentManager(models.Manager):
|
|||||||
if not created:
|
if not created:
|
||||||
pass
|
pass
|
||||||
logger.error('HTTP Error Code: %s' % e.code, exc_info=sys.exc_info(), extra={'data': {'api-url': url}})
|
logger.error('HTTP Error Code: %s' % e.code, exc_info=sys.exc_info(), extra={'data': {'api-url': url}})
|
||||||
|
stat_update_count('eve_proxy_api_exception')
|
||||||
raise DocumentRetrievalError(e.code)
|
raise DocumentRetrievalError(e.code)
|
||||||
except urllib2.URLError, e:
|
except urllib2.URLError, e:
|
||||||
if not created:
|
if not created:
|
||||||
pass
|
pass
|
||||||
logger.error('URL Error: %s' % e, exc_info=sys.exc_info(), extra={'data': {'api-url': url}})
|
logger.error('URL Error: %s' % e, exc_info=sys.exc_info(), extra={'data': {'api-url': url}})
|
||||||
|
stat_update_count('eve_proxy_api_exception')
|
||||||
raise DocumentRetrievalError(e.reason)
|
raise DocumentRetrievalError(e.reason)
|
||||||
else:
|
else:
|
||||||
doc.body = unicode(conn.read(), 'utf-8')
|
doc.body = unicode(conn.read(), 'utf-8')
|
||||||
@@ -106,6 +117,8 @@ class CachedDocumentManager(models.Manager):
|
|||||||
error = enode[0].getAttribute('code')
|
error = enode[0].getAttribute('code')
|
||||||
|
|
||||||
if error:
|
if error:
|
||||||
|
stat_update_count('eve_proxy_api_error')
|
||||||
|
stat_update_count('eve_proxy_api_error_%s' % int(error))
|
||||||
# If we have a rollback error, try and retreive a correct version from the DB
|
# If we have a rollback error, try and retreive a correct version from the DB
|
||||||
if int(error) in ROLLBACK_ERRORS:
|
if int(error) in ROLLBACK_ERRORS:
|
||||||
try:
|
try:
|
||||||
@@ -119,9 +132,11 @@ class CachedDocumentManager(models.Manager):
|
|||||||
doc.save()
|
doc.save()
|
||||||
else:
|
else:
|
||||||
doc.save()
|
doc.save()
|
||||||
|
stat_update_count('eve_proxy_api_success')
|
||||||
|
|
||||||
# If this is user related, write a log instance
|
# If this is user related, write a log instance
|
||||||
if params and (params.get('userid', None) or params.get('keyid', None)):
|
if params and (params.get('userid', None) or params.get('keyid', None)):
|
||||||
|
stat_update_count('eve_proxy_api_authenticated_requests')
|
||||||
try:
|
try:
|
||||||
v = int(params.get('userid', None) or int(params.get('keyid', None)))
|
v = int(params.get('userid', None) or int(params.get('keyid', None)))
|
||||||
except:
|
except:
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ setup_environ(settings)
|
|||||||
import getopt
|
import getopt
|
||||||
|
|
||||||
from django.db.models import Count
|
from django.db.models import Count
|
||||||
|
from django.core.cache import cache
|
||||||
from eve_api.app_defines import *
|
from eve_api.app_defines import *
|
||||||
from eve_api.models import EVEAccount, EVEPlayerCorporation
|
from eve_api.models import EVEAccount, EVEPlayerCorporation
|
||||||
|
|
||||||
@@ -82,7 +83,12 @@ def main(argv=None):
|
|||||||
print "requests.label Cached Requests"
|
print "requests.label Cached Requests"
|
||||||
print "graph_args --base 1000"
|
print "graph_args --base 1000"
|
||||||
return 0
|
return 0
|
||||||
|
if execname == 'auth_api_proxy_requests':
|
||||||
|
print "graph_title Auth - EVE API Requests"
|
||||||
|
print "graph vlabel Requests"
|
||||||
|
print "graph_category auth"
|
||||||
|
print "eve_proxy_api_requests.label API Requests"
|
||||||
|
print "eve_proxy_api_requests.type COUNTER"
|
||||||
|
|
||||||
if execname == 'auth_apikeys':
|
if execname == 'auth_apikeys':
|
||||||
key_count = EVEAccount.objects.filter(api_status=API_STATUS_OK).count()
|
key_count = EVEAccount.objects.filter(api_status=API_STATUS_OK).count()
|
||||||
@@ -97,6 +103,9 @@ def main(argv=None):
|
|||||||
elif execname == 'auth_eveapicache':
|
elif execname == 'auth_eveapicache':
|
||||||
print "requests.value %s" % CachedDocument.objects.count()
|
print "requests.value %s" % CachedDocument.objects.count()
|
||||||
|
|
||||||
|
elif execname == 'auth_api_proxy_requests':
|
||||||
|
print "eve_proxy_api_requests.value %s" % cache.get('eve_proxy_api_requests', 0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user