How do I get appstats to work with webapp2 and extended routing on GAE?

白昼怎懂夜的黑 提交于 2020-01-13 06:26:12

问题


I'm trying to get Appstats to work on my GAE Python app. I'm using webapp2 with python 2.7.

I've followed the instructions from https://developers.google.com/appengine/docs/python/tools/appstats#Setup which includes creating the appengine_config.py file:

def webapp_add_wsgi_middleware(app):
    from google.appengine.ext.appstats import recording
    app = recording.appstats_wsgi_middleware(app)
    return app

And adding the following lines to my app.yaml:

builtins:
- appstats: on

The python app I wish to use Appstats on looks something like this:

import webapp2
from google.appengine.api import urlfetch
from google.appengine.ext import db
import appengine_config

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello word!')

app = webapp2.WSGIApplication([
    webapp2.Route(r'/method1/', handler=Method1, name='method1'),
    webapp2.Route(r'/method2/', handler=Method2, name='method2'),
    webapp2.Route(r'/', handler=MainHandler, name='home')
], debug=True)

(I tried import appengine_config after reading the comments from Appstats are only working for one WSGIApplication but that doesn't work neither)

The problem I'm facing is that I can see the appstats console at /_ah/stats but it is not recording anything even after many requests have been made to the app.

I'm wondering if it has anything to do with the fact that I'm using extended URL routes? I'd really like to use webapps2 extended routing, so I hope Appstats doesn't have issues with it. If anyone has any insights on what I'm doing wrong, it will really help!

Thanks loads in advance!


回答1:


Maybe this will help:

1) I only configured in my app.yaml:

builtins:
- appstats: on

2) And the appengine_config.py :

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

def webapp_add_wsgi_middleware(app):                    
    from google.appengine.ext.appstats import recording
    app = recording.appstats_wsgi_middleware(app)
    return app

3) I did not have to change my handlers or routing.




回答2:


After much investigation, it turned out that the answer to my problem was a careless mistake. I had somehow created the appengine_config.py file in a sub folder, rather than the root folder, and I didn't notice it because I had been using an IDE.

So should anyone have this problem, make sure:

  1. appengine_config.py is in the root folder
  2. appengine_config.py contains the code mentioned above
  3. app.yaml contains the appstats: on as a builtins: (If you can access /_ah/stats, this part is configured properly)

And it should work. :)



来源:https://stackoverflow.com/questions/13795066/how-do-i-get-appstats-to-work-with-webapp2-and-extended-routing-on-gae

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!