Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRF Tracking fails on Azure #12

Closed
drdreff opened this issue Mar 22, 2020 · 3 comments
Closed

DRF Tracking fails on Azure #12

drdreff opened this issue Mar 22, 2020 · 3 comments

Comments

@drdreff
Copy link

drdreff commented Mar 22, 2020

I followed these instructions to create a django project on Azure:
https://docs.microsoft.com/en-us/azure/app-service/containers/tutorial-python-postgresql-app

My requirements.txt:

django==2.2.7
djangorestframework==3.9.4
djangorestframework-jwt==1.11.0
drf-extensions==0.5.0
drf-tracking==1.5.0
pytz==2019.1 # for drf-tracking
sqlparse==0.3.0 # for drf-tracking

snippet from views.py for how I load and all the logging:

...
from rest_framework_tracking.mixins import LoggingMixin, BaseLoggingMixin
from rest_framework_tracking.models import APIRequestLog
...

class TagViewSet(LoggingMixin, viewsets.ModelViewSet):
    logging_methods = ['POST', 'PUT', 'PATCH', 'DELETE']
    queryset = TechnologyTag.objects.all()
    serializer_class = TagSerializer
    permission_classes = [permissions.IsAuthenticated,IsAdminOrReadOnly]
    filter_class = TagFilter

Everything works great locally, but on Azure, nothing is ever written to the log. The reason is hiding in this error:

2020-03-10T14:59:03.135990572Z Logging API call raise exception!
2020-03-10T14:59:03.136026572Z Traceback (most recent call last):
2020-03-10T14:59:03.136032272Z   File "/antenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
2020-03-10T14:59:03.136036372Z     return self.cursor.execute(sql, params)
2020-03-10T14:59:03.136040272Z psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type inet: "24.23.134.119:43398"
2020-03-10T14:59:03.136044372Z LINE 1: ...e/', 'portola.views.DocumentViewSet', 'disclose', '24.23.134...
2020-03-10T14:59:03.136048272Z                                                              ^
2020-03-10T14:59:03.136052072Z
2020-03-10T14:59:03.136055472Z
2020-03-10T14:59:03.136058972Z The above exception was the direct cause of the following exception:
2020-03-10T14:59:03.136062672Z
2020-03-10T14:59:03.136066171Z Traceback (most recent call last):
2020-03-10T14:59:03.136069771Z   File "/antenv/lib/python3.7/site-packages/rest_framework_tracking/base_mixins.py", line 78, in finalize_response
2020-03-10T14:59:03.136073671Z     self.handle_log()
2020-03-10T14:59:03.136089071Z   File "/antenv/lib/python3.7/site-packages/rest_framework_tracking/mixins.py", line 12, in handle_log
2020-03-10T14:59:03.136093371Z     APIRequestLog(**self.log).save()
2020-03-10T14:59:03.136096871Z   File "/antenv/lib/python3.7/site-packages/django/db/models/base.py", line 741, in save
2020-03-10T14:59:03.136100571Z     force_update=force_update, update_fields=update_fields)
2020-03-10T14:59:03.136105271Z   File "/antenv/lib/python3.7/site-packages/django/db/models/base.py", line 779, in save_base
2020-03-10T14:59:03.136108971Z     force_update, using, update_fields,
2020-03-10T14:59:03.136112371Z   File "/antenv/lib/python3.7/site-packages/django/db/models/base.py", line 870, in _save_table
2020-03-10T14:59:03.136115971Z     result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
2020-03-10T14:59:03.136119571Z   File "/antenv/lib/python3.7/site-packages/django/db/models/base.py", line 908, in _do_insert
2020-03-10T14:59:03.136123271Z     using=using, raw=raw)
2020-03-10T14:59:03.136126671Z   File "/antenv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
2020-03-10T14:59:03.136130371Z     return getattr(self.get_queryset(), name)(*args, **kwargs)
2020-03-10T14:59:03.136133871Z   File "/antenv/lib/python3.7/site-packages/django/db/models/query.py", line 1186, in _insert
2020-03-10T14:59:03.136137471Z     return query.get_compiler(using=using).execute_sql(return_id)
2020-03-10T14:59:03.136140971Z   File "/antenv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql
2020-03-10T14:59:03.136144671Z     cursor.execute(sql, params)
2020-03-10T14:59:03.136148071Z   File "/antenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
2020-03-10T14:59:03.136152571Z     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
2020-03-10T14:59:03.136156271Z   File "/antenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
2020-03-10T14:59:03.136159871Z     return executor(sql, params, many, context)
2020-03-10T14:59:03.136163371Z   File "/antenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
2020-03-10T14:59:03.136166971Z     return self.cursor.execute(sql, params)
2020-03-10T14:59:03.136170371Z   File "/antenv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
2020-03-10T14:59:03.136173971Z     raise dj_exc_value.with_traceback(traceback) from exc_value
2020-03-10T14:59:03.136177471Z   File "/antenv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
2020-03-10T14:59:03.136181071Z     return self.cursor.execute(sql, params)
2020-03-10T14:59:03.136184471Z django.db.utils.DataError: invalid input syntax for type inet: "24.23.134.119:43398"
2020-03-10T14:59:03.136188171Z LINE 1: ...e/', 'portola.views.DocumentViewSet', 'disclose', '24.23.134...
2020-03-10T14:59:03.136194471Z                                                              ^
2020-03-10T14:59:03.136198171Z

The core of the problem is this psycopg2 exception:
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type inet: "24.23.134.119:43398"
which seems to be complaining about the port number that is being attached to the IP address via the middleware they're using to serve static pages.

Possible similar errors I have found googling this problem refer to nginx configuration.

Issue moved from aschn/drf-tracking#150 with additional details added.

@lingster
Copy link
Owner

@drdreff : can you try amending: rest_framework_tracking/base_mixins.py
from:

    def _get_ip_address(self, request):
        """Get the remote ip address the request was generated from. """
        ipaddr = request.META.get("HTTP_X_FORWARDED_FOR", None)
        if ipaddr:
            # X_FORWARDED_FOR returns client1, proxy1, proxy2,...
            return ipaddr.split(",")[0].strip()
        return request.META.get("REMOTE_ADDR", "")

to:

    def _get_ip_address(self, request):
        """Get the remote ip address the request was generated from. """
        ipaddr = request.META.get("HTTP_X_FORWARDED_FOR", None)
        if ipaddr:
            # X_FORWARDED_FOR returns client1, proxy1, proxy2,...
            return ipaddr.split(",")[0].strip().split(":")[0]
        return request.META.get("REMOTE_ADDR", "").split(":")[0]

The .split(":")[0] should help to remove the port from the ip address if it exists.
I tried setting up the azure environment but it was taking too long...

@drdreff
Copy link
Author

drdreff commented Mar 29, 2020

I applied this fix to a private branch and it works!

I'll run with my fork until you're done and ready to release a full version.

@lingster
Copy link
Owner

lingster commented May 25, 2020

I've added this change and deployed new version 1.7.0 to pypi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants