Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for async requests in MetricsFilter #676

Open
lapo-luchini opened this issue Jul 29, 2021 · 1 comment · May be fixed by #689
Open

Add support for async requests in MetricsFilter #676

lapo-luchini opened this issue Jul 29, 2021 · 1 comment · May be fixed by #689

Comments

@lapo-luchini
Copy link
Contributor

It seems to me that MetricsFilter currently does not support async requests, it reports very low times which I guess are only limited to the time the initial request is answered.

@lapo-luchini
Copy link
Contributor Author

I'm still searching about it, but seems like it could be solved something like this:

        boolean isAsync = false;
        try {
            filterChain.doFilter(servletRequest, servletResponse);
            isAsync = servletRequest.isAsyncStarted();
            if (isAsync) {
                servletRequest.getAsyncContext().addListener(new AsyncListener() {
                    volatile boolean done = false;
                    private void meter() {
                        if (!done) {
                            timer.observeDuration();
                            statusCounter.labels(components, method, getStatusCode(servletResponse)).inc();
                            done = true;
                        }
                    }
                    @Override public void onStartAsync(AsyncEvent asyncEvent) { }
                    @Override public void onComplete(AsyncEvent asyncEvent) { meter(); }
                    @Override public void onError(AsyncEvent asyncEvent) { meter(); }
                    @Override public void onTimeout(AsyncEvent asyncEvent) { meter(); }
                });
            }
        } finally {
            if (!isAsync) {
                timer.observeDuration();
                statusCounter.labels(components, method, getStatusCode(servletResponse)).inc();
            }
        }

@lapo-luchini lapo-luchini linked a pull request Sep 7, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant