Tracing from an effect back to its cause in a large Python codebase
I have a reasonably large project in Django, which is a reasonably large framework, and I'm using a reasonably large number of apps, middlewares, context processors, etc. The scale means that when a part of the codebase runs for requests where I don't want it to, identifying why it did is hard. Straight code inspection is much too time-consuming, as is single-stepping through the entire request in a debugger.
In this particular case, my problem is that I'm getting "Vary: Cookie" set on every response, including some that I want heavily cached and where I shouldn't need any cookies. I suspect, but don't know how to prove, that some middleware or context processor is accessing request.session
even when it doesn't use the result--though it might be an indirect access, such as through request.user
. And of course it might be something else entirely.
In Python, how would you trace from an effect ("the Vary header was added to the response") back to its cause in a large codebase?
Here's a thought: monkey patch the django HttpResponse class so that its __setitem__
method raises an exception if the header being set is Vary
. You can take care of this from an otherwise do-nothing middleware when it's created. It should give you a nice traceback from the line that's setting the header.
class MonkeyPatchMiddleware(object):
def __init__(self):
from django.http import HttpResponse
original_set_item = HttpResponse.__setitem__
def __setitem__(self, header, value):
if header == "Vary":
raise ValueError
original_set_item(self, header, value)
HttpResponse.__setitem__ = __setitem__
Install the middleware as the first thing in your middleware stack in your django settings file.
链接地址: http://www.djcxy.com/p/11398.html