-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2271 from WikiWatershed/arr/api-logging
Log Geoprocessing API Requests
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import sys | ||
import rollbar | ||
|
||
from django.utils.timezone import now | ||
from django.conf import settings | ||
|
||
from apps.core.models import RequestLog | ||
|
||
|
||
rollbar_settings = getattr(settings, 'ROLLBAR', {}) | ||
if rollbar_settings: | ||
rollbar.init(rollbar_settings.get('access_token'), | ||
rollbar_settings.get('environment')) | ||
|
||
|
||
def log_request(view): | ||
""" | ||
Log the request and its response as a RequestLog model | ||
""" | ||
|
||
def decorator(request, *args, **kwargs): | ||
requested_at = now() | ||
|
||
view_result = view(request, *args, **kwargs) | ||
|
||
user = request.user if request.user.is_authenticated() else None | ||
|
||
response_time = now() - requested_at | ||
response_ms = int(response_time.total_seconds() * 1000) | ||
|
||
log = RequestLog.objects.create( | ||
user=user, | ||
job_uuid=view_result.data.get('job', None), | ||
requested_at=requested_at, | ||
response_ms=response_ms, | ||
status_code=view_result.status_code, | ||
path=request.path, | ||
query_params=request.query_params.dict(), | ||
method=request.method, | ||
host=request.get_host(), | ||
remote_addr=_get_remote_addr(request)) | ||
try: | ||
log.save() | ||
except Exception: | ||
if rollbar_settings: | ||
rollbar.report_exc_info(sys.exc_info(), request) | ||
|
||
return view_result | ||
|
||
decorator.__name__ = view.__name__ | ||
decorator.__dict__ = view.__dict__ | ||
decorator.__doc__ = view.__doc__ | ||
|
||
return decorator | ||
|
||
|
||
def _get_remote_addr(request): | ||
# get IP | ||
ipaddr = request.META.get("HTTP_X_FORWARDED_FOR", None) | ||
if ipaddr: | ||
# X_FORWARDED_FOR returns client1, proxy1, proxy2,... | ||
return [x.strip() for x in ipaddr.split(",")][0] | ||
else: | ||
return request.META.get("REMOTE_ADDR", "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
from django.conf import settings | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('core', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='RequestLog', | ||
fields=[ | ||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | ||
('job_uuid', models.UUIDField(help_text='If async request, the uuid of the submitted job', null=True)), | ||
('requested_at', models.DateTimeField(help_text='When the request was made')), | ||
('response_ms', models.PositiveIntegerField(help_text='How long the response took in ms')), | ||
('path', models.CharField(help_text='The requested URI', max_length=400)), | ||
('query_params', models.TextField(help_text='Stringified requested query params dictionary', null=True)), | ||
('status_code', models.PositiveIntegerField(help_text='HTTP status code sent back')), | ||
('method', models.CharField(help_text='HTTP method', max_length=255)), | ||
('host', models.URLField(help_text='The host from which the request was sent')), | ||
('remote_addr', models.GenericIPAddressField(help_text='The IP address from which the request was sent')), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, null=True)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters