Store traces of http requests in Python

筅森魡賤 提交于 2019-12-25 18:37:07

问题


I want to store traces of the python code which gets executed to handle http requests.

We use Python and Django.

What is a trace in this context?

I want to have this for all traced http requests:

  • request.META,
  • request.POST
  • request.GET

For every trace there are N times the stacktraces of the code execution. In the first step it would be enough to store well known python traceback as string. In the first step the value of variables does not need to be stored.

The stacktraces can be created by a supervisor thread or by explicit by a method call in the source code. In the first step explicit is enough, the supervisor thread can be done later.

The traces are optional and only for debugging and analyzing. They are not needed to process the http requests.

How could this be solved?

Is something missing to understand this question? If, yes, please leave a comment.

Update

  • What do I want to achieve? I want to have a much better way to debug errors in production systems. I want to see what happened before an error occurred.
  • All http requests? I think I want to store about one trace per minute. I guess I will delete the data after N weeks.

回答1:


Moved from comment

You could look into https://github.com/getsentry/sentry which is dedicated solution for logging errors. Also it has a way of manually logging data with raven client.




回答2:


There are two approaches to do what you want. One is called "tracing", the other "logging".

Tracing means you have some software (like the trace module) which allows you to create statistics which methods are called how often, what the method parameters are, who calls whom, etc.

With tracing, you need to configure which method/function calls to trace. It's quick to set up but can't look inside of methods/functions. It's a bit dumb.

Also, you'll have to write your own tools to analyze the trace files to make sense of them.

Logging is more manual. You need to configure a logging framework and then call log methods in interesting places. That allows you to see local variables. You can format logical data structures in useful ways, etc.

That means logging is more powerful but takes much more manual work to set up. It will also take some time to learn how to do proper logging (not too much and lot too little). But logging already comes with many powerful tools (log viewers, log file rotation to avoid disk full).




回答3:


You can use a middleware to interfere all requests and then log them. Have a look at Rhumbix/django-request-logging for an example implementation.



来源:https://stackoverflow.com/questions/44562378/store-traces-of-http-requests-in-python

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