
com.github.kristofa.brave.spring.ServletHandlerInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brave-spring-web-servlet-interceptor Show documentation
Show all versions of brave-spring-web-servlet-interceptor Show documentation
Spring HandlerInterceptorAdapter implementation.
package com.github.kristofa.brave.spring;
import com.github.kristofa.brave.ServerRequestInterceptor;
import com.github.kristofa.brave.ServerResponseInterceptor;
import com.github.kristofa.brave.ServerSpan;
import com.github.kristofa.brave.ServerSpanThreadBinder;
import com.github.kristofa.brave.http.HttpResponse;
import com.github.kristofa.brave.http.HttpServerRequest;
import com.github.kristofa.brave.http.HttpServerRequestAdapter;
import com.github.kristofa.brave.http.HttpServerResponseAdapter;
import com.github.kristofa.brave.http.SpanNameProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.net.URISyntaxException;
public class ServletHandlerInterceptor extends HandlerInterceptorAdapter {
static final String HTTP_SERVER_SPAN_ATTRIBUTE = ServletHandlerInterceptor.class.getName() + ".server-span";
private final ServerRequestInterceptor requestInterceptor;
private final SpanNameProvider spanNameProvider;
private final ServerResponseInterceptor responseInterceptor;
private final ServerSpanThreadBinder serverThreadBinder;
@Autowired
public ServletHandlerInterceptor(ServerRequestInterceptor requestInterceptor, ServerResponseInterceptor responseInterceptor, SpanNameProvider spanNameProvider, final ServerSpanThreadBinder serverThreadBinder) {
this.requestInterceptor = requestInterceptor;
this.spanNameProvider = spanNameProvider;
this.responseInterceptor = responseInterceptor;
this.serverThreadBinder = serverThreadBinder;
}
@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) {
requestInterceptor.handle(new HttpServerRequestAdapter(new HttpServerRequest() {
@Override
public String getHttpHeaderValue(String headerName) {
return request.getHeader(headerName);
}
@Override
public URI getUri() {
try {
return new URI(request.getRequestURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
public String getHttpMethod() {
return request.getMethod();
}
}, spanNameProvider));
return true;
}
@Override
public void afterConcurrentHandlingStarted(final HttpServletRequest request, final HttpServletResponse response, final Object handler) {
request.setAttribute(HTTP_SERVER_SPAN_ATTRIBUTE, serverThreadBinder.getCurrentServerSpan());
serverThreadBinder.setCurrentSpan(null);
}
@Override
public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) {
final ServerSpan span = (ServerSpan) request.getAttribute(HTTP_SERVER_SPAN_ATTRIBUTE);
if (span != null) {
serverThreadBinder.setCurrentSpan(span);
}
responseInterceptor.handle(new HttpServerResponseAdapter(new HttpResponse() {
@Override
public int getHttpStatusCode() {
return response.getStatus();
}
}));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy