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

Backwards incompatible change in 4.1.0 #183

Closed
gvangool opened this issue Feb 6, 2023 · 5 comments
Closed

Backwards incompatible change in 4.1.0 #183

gvangool opened this issue Feb 6, 2023 · 5 comments

Comments

@gvangool
Copy link

gvangool commented Feb 6, 2023

We have a custom RequestMiddleware that is inheriting from django_structlog.middlewares.RequestMiddleware, with the introduction of async support in #180 that became a function instead of a class.

At that point, tests started to fail with a TypeError.

TypeError: function() argument 'code' must be code, not str

If anyone else runs into it, you can make the following change.

diff --git c/./middleware.py i/./middleware.py
index 40d563a5..3b5cc81b 100644
--- c/./middleware.py
+++ i/./middleware.py
@@ -1,8 +1,8 @@
 import structlog
-from django_structlog.middlewares import RequestMiddleware
+from django_structlog.middlewares.request import SyncRequestMiddleware


-class LogRequestMiddleware(RequestMiddleware):
+class LogRequestMiddleware(SyncRequestMiddleware):
     def process_view(self, request, view_func, view_args, view_kwargs):
         resolver_match = request.resolver_match
         view_func_qualname = (

Can we flag the 4.1.0 release as having backwards-incompatible changes?

@jrobichaud
Copy link
Owner

It is something that django-structlog is not currently supporting since I was not aware of people doing this.

Could you explain what you are doing so I could refactor accordingly?

Your change does not allow Async support, I'd like an elegant solution that will allow everything to work.

@gvangool
Copy link
Author

gvangool commented Feb 6, 2023

We're adding extra logging variables. In our case:

  • url_name: the resolved URL name
  • func: the view function (as an import path)
  • project_id: a URL kwarg that is available on 50% of our views

@jrobichaud
Copy link
Owner

@jrobichaud
Copy link
Owner

@gvangool, like you suggested I added a note in 4.1's changelog.

https://django-structlog.readthedocs.io/en/latest/changelog.html#february-4th-2023

In your case, I have the feeling you can do like the example from the documentation:

from django.dispatch import receiver
from django_structlog import signals
import structlog

@receiver(signals.bind_extra_request_metadata)
def bind_awesome_stuff(request, logger, **kwargs):
    # Add your code using the request 
    # url_name: the resolved URL name
    # func: the view function (as an import path)
    # project_id: a URL kwarg that is available on 50% of our views
    structlog.contextvars.bind_contextvars(
        url_name="?",
        func="?",
        project_id="?",
    )

@jrobichaud
Copy link
Owner

I rolled back the changes in 4.1.1. you can switch back to RequestMiddleware if you want.

SyncRequestMiddleware is still available.

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