Skip to content

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

Merged
sl0thentr0py merged 10 commits intomasterfrom
django-request-urlconf
Jan 19, 2022
Merged

feat(django): Pick custom urlconf up from request if any#1308
sl0thentr0py merged 10 commits intomasterfrom
django-request-urlconf

Conversation

@sl0thentr0py
Copy link
Copy Markdown
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
Copy Markdown
Contributor

@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
Copy Markdown
Contributor

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
Copy Markdown
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