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

AttributeError: 'Response' object has no attribute 'template_name' #54

Open
someidiot opened this issue Oct 26, 2017 · 5 comments
Open

Comments

@someidiot
Copy link

Using django-prometheus 1.0.9 with django 1.11.2.

I have some django-rest-framework views that are being cached with the cache_page decorator like:

@method_decorator(cache_page(settings.CACHE_TIMEOUT_API))
def dispatch(self, *args, **kwargs):
    return super(BaseAPICachedView, self).dispatch(*args, **kwargs)

When there is a cache hit, the django-prometheus middleware fails in middleware.py:135 with AttributeError: 'Response' object has no attribute 'template_name'

def process_template_response(self, request, response):
    responses_by_templatename.labels(str(
        response.template_name)).inc()
    return response

Sure enough, the cached views don't seem to have template_name. I fixed it by checking for the attribute first, not sure if that's the best way but it works for me:

def process_template_response(self, request, response):
    if hasattr(response, 'template_name'):
        responses_by_templatename.labels(str(
            response.template_name)).inc()
    return response
@korfuri
Copy link
Owner

korfuri commented Oct 30, 2017

Interesting. I'm not familiar with cache_page so before I fix I should ask - do you think ignoring the request in responses_by_templatename is the best way to go here? Should we account them to something like template="<unknown_template>" instead?

@someidiot
Copy link
Author

Yeah, that's probably better so at least the views are included in the metrics.

@elnappo
Copy link

elnappo commented Feb 6, 2018

Same error here, sadly I can't use this awesome project in my application due to this bug and #42.

Django 2.0.2
django-prometheus 1.0.11
Python 3.6.4

@adilnaimi
Copy link
Contributor

same error

Django 1.11.10
django-prometheus 1.0.11
Python 3.6.4

@adilnaimi
Copy link
Contributor

adilnaimi commented Feb 28, 2018

I submitted a PR #63 based on
#28 which is old, I only added the test to my env:

Django 1.11.10
django-prometheus 1.0.11
Python 3.6.4

elnappo added a commit to elnappo/django-prometheus that referenced this issue Mar 13, 2018
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

4 participants