Open
Description
Bug Report
I was just looking through the aiohttp integration code and thought I'd provide some feedback that would make your code more robust.
Server
- Don't assign
app.__datadog_trace
. This is not supported behaviour and is probably already broken in v4/master where we use__slots__
. You could just check for your config key instead (if CONFIG_KEY in app
). - CONFIG_KEY could be changed to a
web.AppKey
instance for better type safety. - It is recommended to export the middleware directly to users and check if a user has already added it in the setup function (e.g. https://github.com/aio-libs/aiohttp-debugtoolbar/blob/9753db46f09bd3776f6fd25cee3709e651d23a13/aiohttp_debugtoolbar/main.py#L80). This gives users more control over the order of middlewares (e.g. they may want an exception handler middleware to be before or after the trace middleware, currently this ordering is entirely dependent on whether the setup function is called before/after the exception middleware has been inserted).
Client
- Pin may have similar issues with
__slots__
. - Aiohttp 3.12+ now has middlewares (similar to the server-side), so we'd highly recommend adding a middleware instead of patching, as a more robust (and easily type-checked) approach.