Skip to content

SentryWrappingMiddleware does not expose details of wrapped middleware #1145

@MattFisher

Description

@MattFisher

Environment

How do you use Sentry?
Sentry SaaS (sentry.io)

Which SDK and version?
Python sentry-sdk==1.1.0

Steps to Reproduce

Use the python sentry sdk alongside the python NewRelic agent (newrelic-python-agent v6.4.4.161).
Wait for slow transaction traces to appear in NewRelic UI.

Expected Result

The middleware entries in transaction traces should show the names of the middlewares being wrapped.

Actual Result

All middlewares in transaction traces appear listed as sentry_sdk.integrations.django_middleware:_wrap_middleware.<locals>.SentryWrappingMiddleware, as shown in the screenshot below.
Screen Shot 2021-07-07 at 11 29 21 am

I've done some investigation, and the NewRelic agent determines the strings to be displayed
via a callable_name function, that bottoms out in a function called _object_context_py3. This function returns the module name (via object.__module__) and object.__qualname__ attributes of the passed object.

In the sentry_sdk.integrations.django.middleware._wrap_middleware function, the SentryWrappingMiddleware is given the wrapped middleware's __name__ attribute.

	if hasattr(middleware, "__name__"):
        SentryWrappingMiddleware.__name__ = middleware.__name__

In the standard library's functools.wraps(), the wrapping entity is given ('__module__', '__name__', '__qualname__', '__doc__', '__annotations__') of the wrapped object. This implies it should be pretty safe to do the same with SentryWrappingMiddleware, like so:

    if hasattr(middleware, "__name__"):
        SentryWrappingMiddleware.__name__ = middleware.__name__
    if hasattr(middleware, "__module__"):
        SentryWrappingMiddleware.__module__ = middleware.__module__
    if hasattr(middleware, "__qualname__"):
        SentryWrappingMiddleware.__qualname__ = middleware.__qualname__

or potentially

    for attr in ('__name__', '__module__', '__qualname__', '__doc__', '__annotations__'):
        if hasattr(middleware, attr):
            setattr(SentryWrappingMiddleware, attr, getattr(middleware, attr))

After manually making the first code change listed above, the results were what was expected:
Screen Shot 2021-07-07 at 11 28 21 am

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions