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

Implement filtering of async requests. #689

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.prometheus.client.Counter;
import io.prometheus.client.Histogram;

import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
Expand Down Expand Up @@ -162,7 +164,7 @@ public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
public void doFilter(ServletRequest servletRequest, final ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (!(servletRequest instanceof HttpServletRequest)) {
filterChain.doFilter(servletRequest, servletResponse);
return;
Expand All @@ -172,17 +174,37 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo

String path = request.getRequestURI();

String components = getComponents(path);
String method = request.getMethod();
Histogram.Timer timer = histogram
final String components = getComponents(path);
final String method = request.getMethod();
final Histogram.Timer timer = histogram
.labels(components, method)
.startTimer();

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) {
done = true;
Comment on lines +191 to +192
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't thread safe, because there's a check-then-act race condition. I don't think thread safety is needed here, because my understanding is that meter() will only be called once. If it's only called once, please remove done. If there are cases where it is called multiple times in different threads, fix the race condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense… I can't find anything definitive on the 3.0 specs that only one of the three methods will ever be called, so I'll rather err on the safe side by fixing the race.

timer.observeDuration();
statusCounter.labels(components, method, getStatusCode(servletResponse)).inc();
}
}
@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 {
timer.observeDuration();
statusCounter.labels(components, method, getStatusCode(servletResponse)).inc();
if (!isAsync) {
timer.observeDuration();
statusCounter.labels(components, method, getStatusCode(servletResponse)).inc();
}
}
}

Expand Down