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

Make OpenTelemetryHandlerMappingFilter handle exceptions from Servlet… #12221

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
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 @@ -20,6 +20,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -36,14 +38,19 @@
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

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 parseAndCacheMh = parseAndCacheMh();

private final HttpServerRouteGetter<HttpServletRequest> serverSpanName =
(context, request) -> {
if (this.parseRequestPath) {
// sets new value for PATH_ATTRIBUTE of request
parseAndCache(request);
if (!parseAndCache(request)) {
return null;
}
}
if (findMapping(request)) {
// Name the parent span based on the matching pattern
Expand Down Expand Up @@ -191,14 +198,16 @@ private static MethodHandle parseAndCacheMh() {
}
}

private static void parseAndCache(HttpServletRequest request) {
private static boolean parseAndCache(HttpServletRequest request) {
if (parseAndCacheMh == null) {
return;
return false;
}
try {
parseAndCacheMh.invoke(request);
return true;
} catch (Throwable throwable) {
throw new IllegalStateException(throwable);
logger.log(Level.FINE, "Failed calling parseAndCache", throwable);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerExecutionChain;
Expand All @@ -34,12 +36,19 @@
import org.springframework.web.util.ServletRequestPathUtils;

public class OpenTelemetryHandlerMappingFilter implements Filter, Ordered {
private static final Logger logger =
Logger.getLogger(OpenTelemetryHandlerMappingFilter.class.getName());

private final HttpServerRouteGetter<HttpServletRequest> serverSpanName =
(context, request) -> {
if (this.parseRequestPath) {
// sets new value for PATH_ATTRIBUTE of request
ServletRequestPathUtils.parseAndCache(request);
try {
ServletRequestPathUtils.parseAndCache(request);
} catch (RuntimeException exception) {
logger.log(Level.FINE, "Failed calling parseAndCache", exception);
return null;
}
}
if (findMapping(request)) {
// Name the parent span based on the matching pattern
Expand Down
Loading