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

How to record the time spent in the sanic request #833

Closed
dalianzhu opened this issue Jul 4, 2017 · 8 comments
Closed

How to record the time spent in the sanic request #833

dalianzhu opened this issue Jul 4, 2017 · 8 comments

Comments

@dalianzhu
Copy link

dalianzhu commented Jul 4, 2017

The time spend on each visit is recorded in tornado, like this:

200 GET /new4/session?1499139673177 ... 2.57ms

Can sanic do it with an efficient way? Thanks for your help.

@dalianzhu dalianzhu changed the title How to record request time in sanic How to record request time cost in sanic Jul 4, 2017
@dalianzhu dalianzhu changed the title How to record request time cost in sanic How to record the time spent in the sanic request Jul 4, 2017
@yunstanford
Copy link
Member

Are you talking about the lat ?

@lixxu
Copy link
Contributor

lixxu commented Jul 5, 2017

Sanic seems not provide this feature, you may need use middleware to do it. like this:

@app.middleware('request')
async def add_start_time(request):
    request['start_time'] = datetime.now()

@app.middleware('response')
async def add_spent_time(request, response):
    request['spent_time'] = datetime.now() - request['start_time']

@dalianzhu
Copy link
Author

@yunstanford sorry, i dont know what lat is.

@dalianzhu
Copy link
Author

@lixxu Yes, it works.Thanks a lot.

@yunstanford
Copy link
Member

lat stands for latency, i think it's roughly describing what you wanna. If you are using gunicorn, you can get that from access log for free. You may also get that from your reverse proxy layer if you have one.

@dalianzhu
Copy link
Author

dalianzhu commented Jul 5, 2017

@yunstanford yes, i can get the access log from my nginx, and @lixxu 's idea can also solve my issue.I just want to use this data to simply measure the performance of a handler.

@victorandree
Copy link

victorandree commented Nov 8, 2017

Can I re-open this issue? I want to log latency as part of my access logs, but doesn't seem like I can right now because:

  1. It doesn't seem like the Gunicorn worker does access logging -- I have tried a number of logging configurations and Gunicorn remains silent. The built-in Gunicorn workers all contain calls to self.log.access which are missing from sanic.worker.GunicornWorker so I'm not sure if we get it for free that way (@yunstanford ?)
  2. There is no straightforward way to override the log call in sanic.server.HttpProtocol.log_response in order to add additional fields (for example, request['spent_time']

It doesn't seem entirely straightforward to add response time taken to just the worker, as its response handling is dealt with elsewhere...

@dalianzhu
Copy link
Author

dalianzhu commented Nov 9, 2017

@victorandree sanic does not seem to have the default configuration. i have to use logging in the response middleware like:

@app.middleware('request')
async def add_start_time(request):
    request['start_time'] = time.time()


@app.middleware('response')
async def add_spent_time(request, response):
    spend_time = round((time.time() - request['start_time']) * 1000)
    logging.info("{} {} {} {} {}ms".format(response.status, request.method,
                                           request.path, request.query_string, spend_time))

The daily visit of this project is about 10 million, it seems no problem. because of i am not use gunicorn, I can not give any other help. you can reopen it if you think it is necessary.

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