Skip to content

Commit

Permalink
Review comments on timeout middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Aug 21, 2024
1 parent fec8a71 commit 8fb3241
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
5 changes: 5 additions & 0 deletions ansible_base/lib/middleware/logging/log_request.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import signal
import logging
import traceback
import uuid

from ansible_base.lib.logging import thread_local

import signal


logger = logging.getLogger(__name__)


Expand All @@ -17,6 +21,7 @@ def handle_signal(cls, *args):

def __init__(self, get_response):
self.get_response = get_response
signal.signal(signal.SIGSYS, LogTracebackMiddleware.handle_signal)

def __call__(self, request):
t_id = str(uuid.uuid4())
Expand Down
17 changes: 17 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,20 @@ LOGGING = {
```

After that, the request ID should automatically show up in logs, when the header is passed in.

## Logging Stack on Timeout

django-ansible-base provides machinery to print the stack trace when a request times out.
To do this, you will need to:

1. Set uwsgi params similar to that in `test_app/uwsgi.ini`.
2. Add `ansible_base.lib.middleware.logging.LogTracebackMiddleware` to `MIDDLEWARE` setting

You can try to test this with test_app by running the docker compose server (so it runs with uwsgi),
and then visiting http://localhost:8000/api/v1/timeout_view/ and viewing the logs.
Within those logs, you should be able to see the culprit:

```
test_app-1 | File "/src/test_app/views.py", line 185, in timeout_view
test_app-1 | time.sleep(60*10) # 10 minutes
```
8 changes: 0 additions & 8 deletions test_app/apps.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import signal

from django.apps import AppConfig

from ansible_base.lib.middleware.logging import LogTracebackMiddleware


class TestAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'test_app'

def ready(self):
super().ready()
signal.signal(signal.SIGSYS, LogTracebackMiddleware.handle_signal)
1 change: 1 addition & 0 deletions test_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
re_path(r"^admin/", admin.site.urls, name="admin"),
path('api/v1/', include(resource_api_urls)),
path('api/v1/', views.api_root),
path('api/v1/timeout_view/', views.timeout_view, name='test-timeout-view'),
path('login/', include('rest_framework.urls')),
path("__debug__/", include("debug_toolbar.urls")),
]
7 changes: 5 additions & 2 deletions test_app/uwsgi.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ vacuum = true

# Log to stdout
logto = /dev/stdout
log-master = true
#disable-logging = true

# Increase buffer size
buffer-size = 32768

# Other recommended settings
# Give signal 6 to work with LogTracebackMiddleware
http-timeout = 60
harakiri = 60

harakiri-graceful-timeout = 50
harakiri-graceful-signal = 31
py-call-osafterfork = true
15 changes: 11 additions & 4 deletions test_app/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import logging
from itertools import chain

Expand Down Expand Up @@ -160,24 +161,30 @@ def api_root(request, format=None):
# want '^users/$' [name='user-list']
# do not want '^users/(?P<pk>[^/.]+)/organizations/$' [name='user-organizations-list'],
if '-list' in url.name and url.pattern._regex.count('/') == 1:
list_endpoints[url.name.removesuffix('-list')] = get_relative_url(url.name, request=request, format=format)
list_endpoints[url.name.removesuffix('-list')] = get_relative_url(url.name)

from ansible_base.api_documentation.urls import api_version_urls as docs_urls
from ansible_base.authentication.urls import api_version_urls as authentication_urls

for url in docs_urls + authentication_urls[1:]:
if isinstance(url, URLPattern):
try:
list_endpoints[url.name] = get_relative_url(url.name, request=request, format=format)
list_endpoints[url.name] = get_relative_url(url.name)
except NoReverseMatch:
pass

list_endpoints['service-index'] = get_relative_url('service-index-root', request=request, format=format)
list_endpoints['role-metadata'] = get_relative_url('role-metadata', request=request, format=format)
list_endpoints['service-index'] = get_relative_url('service-index-root')
list_endpoints['role-metadata'] = get_relative_url('role-metadata')
list_endpoints['timeout-view'] = get_relative_url('test-timeout-view')

return Response(list_endpoints)


@api_view(['GET'])
def timeout_view(request, format=None):
time.sleep(60*10) # 10 minutes


class MultipleFieldsViewSet(TestAppViewSet):
serializer_class = serializers.MultipleFieldsModelSerializer

Expand Down

0 comments on commit 8fb3241

Please sign in to comment.