Add null servlet context check (DataDog/dd-trace-java#1414)
MODIFY
instrumentation/servlet/request-3.0/src/main/java/io/opentelemetry/auto/instrumentation/servlet/v3_0/Servlet3Decorator.java
instrumentation/servlet/request-3.0/src/main/java/io/opentelemetry/auto/instrumentation/servlet/v3_0/Servlet3Decorator.java
@@ -88,6 +88,10 @@ public class Servlet3Decorator
*/
private void onContext(
final Span span, final HttpServletRequest request, final ServletContext context) {
+ if (context == null) {
+ // some frameworks (jetty) may return a null context.
+ return;
+ }
final Object attribute = context.getAttribute("io.opentelemetry.auto.dispatcher-filter");
if (attribute instanceof Filter) {
final Object priorAttr = request.getAttribute(SPAN_ATTRIBUTE);

MODIFY
instrumentation/servlet/request-3.0/src/test/groovy/AbstractServlet3Test.groovy
instrumentation/servlet/request-3.0/src/test/groovy/AbstractServlet3Test.groovy
@@ -86,7 +86,9 @@ abstract class AbstractServlet3Test<SERVER, CONTEXT> extends HttpServerTest<SERV
"$Tags.HTTP_URL" { it == "${endpoint.resolve(address)}" || it == "${endpoint.resolveWithoutFragment(address)}" }
"$Tags.HTTP_METHOD" method
"$Tags.HTTP_STATUS" endpoint.status
+ if (context) {
"servlet.context" "/$context"
+ }
"servlet.path" { it == endpoint.path || it == "/dispatch$endpoint.path" }
"span.origin.type" String
if (endpoint.errored) {

ADD
instrumentation/servlet/request-3.0/src/test/groovy/JettyServletHandlerTest.groovy
+/*
+ * Copyright The OpenTelemetry Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import org.eclipse.jetty.server.Server
+import org.eclipse.jetty.server.handler.ErrorHandler
+import org.eclipse.jetty.servlet.ServletHandler
+
+import javax.servlet.Servlet
+import javax.servlet.http.HttpServletRequest
+
+class JettyServletHandlerTest extends AbstractServlet3Test<Server, ServletHandler> {
+
+ @Override
+ Server startServer(int port) {
+ Server server = new Server(port)
+ ServletHandler handler = new ServletHandler()
+ server.setHandler(handler)
+ setupServlets(handler)
+ server.addBean(new ErrorHandler() {
+ protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message) throws IOException {
+ Throwable th = (Throwable) request.getAttribute("javax.servlet.error.exception")
+ writer.write(th ? th.message : message)
+ }
+ })
+ server.start()
+ return server
+ }
+
+ @Override
+ void addServlet(ServletHandler servletHandler, String path, Class<Servlet> servlet) {
+ servletHandler.addServletWithMapping(servlet, path)
+ }
+
+ @Override
+ void stopServer(Server server) {
+ server.stop()
+ server.destroy()
+ }
+
+ @Override
+ String getContext() {
+ ""
+ }
+
+ @Override
+ Class<Servlet> servlet() {
+ TestServlet3.Sync
+ }
+
+ @Override
+ boolean testNotFound() {
+ false
+ }
+}

Add null servlet context check (DataDog/dd-trace-java#1414)