litestar icon indicating copy to clipboard operation
litestar copied to clipboard

Suggestion: Instrumentation hooks

Open Goldziher opened this issue 1 year ago • 0 comments

The suggestion here is to add several new hooks, primarly meant for instrumentation and any use case that needs access to some internals.

Hooks:

  • after_startup: fires after the ASGI lifecycle startup finishes and calls whatever handler (sync or async) is defined by passing to it the Starlite app instance. The application will block connections until the hook returns / ends.
  • after_shutdown: similar to the previous one, accept it fires when shutdown is finished.
  • after_exception: calls a handler when an exception occurs. The handler is given the exception and it must raise an exception - this allows for example to record exceptions or transform them, but is not meant to replace an exception handler.

Some other options that crossed my mind but im not sure about:

  • before_startup: called as part of the ASGI lifecycle startup, before any function set in the on_startup callable list. Receives the app instance.
  • before_shutdown: same as above, but for shutdown. Receives the app instance.
  • before_routing: called when an ASGI call is received by the app and before resolving routing etc. Receives the ASGI scope object

Please give input and any suggestions you may have.

Goldziher avatar Aug 10 '22 15:08 Goldziher