Make OpenTelemetryHandlerMappingFilter handle exceptions from Servlet… (#12221)

This commit is contained in:
Lauri Tulmin 2024-09-12 09:28:59 +03:00 committed by GitHub
parent cab8ce66c0
commit 39668b503f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 5 deletions

View File

@ -20,6 +20,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.servlet.Filter; import javax.servlet.Filter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
@ -36,6 +38,9 @@ import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
public class OpenTelemetryHandlerMappingFilter implements Filter, Ordered { public class OpenTelemetryHandlerMappingFilter implements Filter, Ordered {
private static final Logger logger =
Logger.getLogger(OpenTelemetryHandlerMappingFilter.class.getName());
private static final MethodHandle usesPathPatternsMh = getUsesPathPatternsMh(); private static final MethodHandle usesPathPatternsMh = getUsesPathPatternsMh();
private static final MethodHandle parseAndCacheMh = parseAndCacheMh(); private static final MethodHandle parseAndCacheMh = parseAndCacheMh();
@ -43,7 +48,9 @@ public class OpenTelemetryHandlerMappingFilter implements Filter, Ordered {
(context, request) -> { (context, request) -> {
if (this.parseRequestPath) { if (this.parseRequestPath) {
// sets new value for PATH_ATTRIBUTE of request // sets new value for PATH_ATTRIBUTE of request
parseAndCache(request); if (!parseAndCache(request)) {
return null;
}
} }
if (findMapping(request)) { if (findMapping(request)) {
// Name the parent span based on the matching pattern // Name the parent span based on the matching pattern
@ -191,14 +198,16 @@ public class OpenTelemetryHandlerMappingFilter implements Filter, Ordered {
} }
} }
private static void parseAndCache(HttpServletRequest request) { private static boolean parseAndCache(HttpServletRequest request) {
if (parseAndCacheMh == null) { if (parseAndCacheMh == null) {
return; return false;
} }
try { try {
parseAndCacheMh.invoke(request); parseAndCacheMh.invoke(request);
return true;
} catch (Throwable throwable) { } catch (Throwable throwable) {
throw new IllegalStateException(throwable); logger.log(Level.FINE, "Failed calling parseAndCache", throwable);
return false;
} }
} }
} }

View File

@ -26,6 +26,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.HandlerExecutionChain;
@ -34,12 +36,19 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import org.springframework.web.util.ServletRequestPathUtils; import org.springframework.web.util.ServletRequestPathUtils;
public class OpenTelemetryHandlerMappingFilter implements Filter, Ordered { public class OpenTelemetryHandlerMappingFilter implements Filter, Ordered {
private static final Logger logger =
Logger.getLogger(OpenTelemetryHandlerMappingFilter.class.getName());
private final HttpServerRouteGetter<HttpServletRequest> serverSpanName = private final HttpServerRouteGetter<HttpServletRequest> serverSpanName =
(context, request) -> { (context, request) -> {
if (this.parseRequestPath) { if (this.parseRequestPath) {
// sets new value for PATH_ATTRIBUTE of request // sets new value for PATH_ATTRIBUTE of request
try {
ServletRequestPathUtils.parseAndCache(request); ServletRequestPathUtils.parseAndCache(request);
} catch (RuntimeException exception) {
logger.log(Level.FINE, "Failed calling parseAndCache", exception);
return null;
}
} }
if (findMapping(request)) { if (findMapping(request)) {
// Name the parent span based on the matching pattern // Name the parent span based on the matching pattern