How do I get appstats to work with webapp2 and ext

2019-04-12 20:54发布

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!

2条回答
ゆ 、 Hurt°
2楼-- · 2019-04-12 21:34

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.

查看更多
老娘就宠你
3楼-- · 2019-04-12 21:48

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. :)

查看更多
登录 后发表回答