Skip to content

feat(django): Pick custom urlconf up from request if any #1308

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

Merged
merged 10 commits into from
Jan 19, 2022

Conversation

sl0thentr0py
Copy link
Member

@sl0thentr0py sl0thentr0py commented Jan 17, 2022

Premise

Django middlewares sometimes can override request.urlconf for the current request which it has a contract for using subsequently as the source of truth for url related operations.

As a result, we also need to respect the overwritten urlconf in our transaction name resolving.

Solution

I finally went with an _after_get_response solution. The resolution is repeated code but runs only if urlconf is overwritten. I wanted to touch as little of existing logic as possible.

For reference, I went back and forth several times between doing it in a couple of other places.

  • _before_get_response - this doesn't work because it runs before all the django middlewares
  • middleware wrapping - this feels clunky since the new code block would run on every middleware invocation

Problem

One of our customers have a django-tenants multi-tenant setup. They have a dynamically resolved urlconf and set ROOT_URLCONF to an empty string which breaks this get_resolver in Django.

This results in a silent exception when we try to update the transaction name and hence they see Generic WSGI request for all their requests.

References

@sl0thentr0py sl0thentr0py changed the title Pick custom urlconf up from request if any feat(django): Pick custom urlconf up from request if any Jan 18, 2022
@sl0thentr0py sl0thentr0py marked this pull request as ready for review January 18, 2022 16:34
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice. I have just one question.


content, status, _headers = client.get("/custom/ok")
assert status.lower() == "200 ok"
assert b"".join(content) == b"custom ok"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need the bytes literal here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a byte stream or something from werkzeug, not a string. I just followed the other tests.

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

Successfully merging this pull request may close these issues.

3 participants