Code Monkey home page Code Monkey logo

Comments (2)

tomwojcik avatar tomwojcik commented on May 18, 2024

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.

yinkh avatar yinkh commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.