Comments (2)
The order of middlewares in your app is important. Try to add it as the first one if possible, so it will also be executed as the last one on the response and that's when the context is emptied. The exception here says the context is not available because it's not running in the request-response cycle. Probably it happens when your create a "response" log. And indeed, it's not possible to access context there as it's emptied and I will keep it like that to make sure there's no memory leak.
Sadly, it's not possible to access context data on the "response" log. Or at least not to my knowledge.
What I'd suggest:
- in your logger check if context exists (
if context.exists()
). If not, don't log context data - add "logger" on another application layer, so the server doesn't have to time it, but rather the gateway/proxy would be responsible for that
I don't understand why you're doing all those things here
async def enrich_response(self, response: Response) -> None:
context: dict = _request_scope_context_storage.get()
start_time = context.get(self.key)
context[self.key] = time.time() - start_time
_request_scope_context_storage.set(context)
In this method you're supposed to alter the response. If you want to change something in the context, this line is sufficient
context[self.key] = time.time() - start_time
_request_scope_context_storage.set
is private for a reason. The idea is to use the context
object directly. Did you have a problem with from starlette_context import context
this context? Have you seen the docs?
Plugins are used to extract some data from the request, keep it in the context
and add some data to the response.
What you rather want is to alter the set_context
method in your middleware. You can add a timestamp in this method and in logs just time.time() - context['timestamp']
and all logs would have diff that's incrementing. The set_context
method is in the middleware. Just remember to check if context is available.
I'd advise to use HeaderKeys
class instead of raw strings to access headers.
from starlette-context.
Thanks for your reply, the reason of calculate request time inside enrich_response
method is if an error occor then two logger(access_log and error_log) is need to rewrite. I will try the method you said.
from starlette-context.
Related Issues (20)
- Possible to have multiple RawContextMiddlewares? HOT 3
- codecov report not being uploaded HOT 1
- Question: Can I add request-id into uvicorn.access log? HOT 2
- Can't write new plugin - plugin is not iterable HOT 4
- Testing code that relies on context vars without a full test client / app HOT 8
- Consider removing `RawContextMiddleware` and keeping only `ContextMiddleware` after resolving memory usage issue HOT 8
- How to use context in Jinja (or any other template engine)? HOT 1
- No module named '_contextvars' HOT 1
- How to get request.body from starlette-context plugin? HOT 2
- async pytests fail when using logging middleware from the examples HOT 3
- Intended behavior on BackgroundTasks HOT 2
- Improved logging HOT 4
- Can't use http middleware HOT 1
- Improving the __repr__ method of _Context HOT 2
- Properly use pyproject.toml HOT 2
- 0.3.3 does not contain the testing helper request_cycle_context HOT 2
- ContextDoesNotExistError on FastAPI Custom Exception Handler HOT 8
- Docs are outdated HOT 1
- Exceptions should inherit from Exception, not BaseException HOT 2
- bump starlette 0.23.1 -> 0.25 and other deps
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from starlette-context.