io.opentelemetry.javaagent.instrumentation.servlet.v2_2.Servlet2SpanNameExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-javaagent-servlet-2.2 Show documentation
Show all versions of opentelemetry-javaagent-servlet-2.2 Show documentation
Instrumentation of Java libraries using OpenTelemetry.
The newest version!
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.servlet.v2_2;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.instrumentation.servlet.ServletAccessor;
import io.opentelemetry.javaagent.instrumentation.servlet.ServletRequestContext;
import java.util.Set;
public class Servlet2SpanNameExtractor
implements SpanNameExtractor> {
private final ServletAccessor accessor;
private final Set knownMethods = AgentCommonConfig.get().getKnownHttpRequestMethods();
public Servlet2SpanNameExtractor(ServletAccessor accessor) {
this.accessor = accessor;
}
@Override
public String extract(ServletRequestContext requestContext) {
REQUEST request = requestContext.request();
String method = accessor.getRequestMethod(request);
String servletPath = accessor.getRequestServletPath(request);
if (method == null) {
return "HTTP";
}
if (!knownMethods.contains(method)) {
method = "HTTP";
}
if (servletPath == null || servletPath.isEmpty()) {
return method;
}
String contextPath = accessor.getRequestContextPath(request);
if (contextPath == null || contextPath.isEmpty() || contextPath.equals("/")) {
return method + " " + servletPath;
}
return method + " " + contextPath + servletPath;
}
}