Skip to content

SentrySpringRequestListener is not added as a listener on the ServletContext #1187

@dparrella

Description

@dparrella

Platform:

  • Android -> If yes, which Device API (and compileSdkVersion/targetSdkVersion/Build tools) version?
  • Java -> If yes, which Java (and sourceCompatibility/targetCompatibility) version?
  • Kotlin -> If yes, which Kotlin (and jvmTarget) version?
  • NDK -> If yes, which NDK/CMake version?
  • React-Native -> If yes, which version?
  • Timber -> If yes, which version?
  • Log4j2 -> If yes, which version?
  • Logback -> If yes, which version?
  • Spring -> If yes, which version? 5.3.2

IDE:

  • Android Studio -> If yes, which version?
  • IntelliJ -> If yes, which version?
  • Other -> If yes, which one?

Build system:

  • Gradle -> If yes, which version?
  • Buck -> If yes, which version?
  • Bazel -> If yes, which version?
  • Maven -> If yes, which version?
  • Other -> If yes, which one?

Android Gradle Plugin:

  • Yes -> If yes, which version?
  • No

Sentry Android Gradle Plugin:

  • Yes -> If yes, which version?
  • No

Proguard/R8:

  • Enabled
  • Disabled

Platform installed with:

  • JCenter
  • Bintray
  • Maven Central
  • Manually

The version of the SDK:
3.2.0


I have the following issue:

In my non-Spring Boot application, I am loading SentryWebConfiguration but the SentrySpringRequestListener bean it creates is not hooked into the ServletContext. If I set a break point in the requestInitialized() method it is never reached.

To work around the issue, I created a WebApplicationInitializer that adds a delegating listener to the ServletContext which takes the ServletContext as a constructor. It can then get the application context and retrieve the SentrySpringRequestListener bean to then call its requestInitialized() method. With this in place, the SentrySpringRequestListener is reached on every request. It's not pretty but I haven't been able to come up with anything better.

public class SentryWebApplicationInitializer implements Ordered, WebApplicationInitializer {

    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        servletContext.addListener(new DelegatingSentrySpringRequestListener(servletContext));
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
public class DelegatingSentrySpringRequestListener implements ServletRequestListener {

    private final ServletContext servletContext;

    private SentrySpringRequestListener delegate;
    private Object delegateMonitor = new Object();

    public DelegatingSentrySpringRequestListener(final ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @Override
    public void requestDestroyed(final ServletRequestEvent requestEvent) {
        if (getDelegate() == null) {
            return;
        }
        getDelegate().requestDestroyed(requestEvent);
    }

    @Override
    public void requestInitialized(final ServletRequestEvent requestEvent) {
        if (getDelegate() == null) {
            return;
        }
        getDelegate().requestInitialized(requestEvent);
    }

    private SentrySpringRequestListener getDelegate() {
        synchronized (this.delegateMonitor) {
            if (this.delegate == null) {
                final WebApplicationContext context = WebApplicationContextUtils.findWebApplicationContext(this.servletContext);
                if (context == null) {
                    log.warn("No WebApplicationContext found in the ServletContext");
                    return null;
                }

                this.delegate = context.getBean(SentrySpringRequestListener.class);
            }

            return this.delegate;
        }
    }
}

Steps to reproduce:

  • See sample code and description

Actual result:

  • No requests hit the SentrySpringRequestListener

Expected result:

  • SentrySpringRequestListener should be automatically added as a listener to the ServletContext and receive all requests

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions