All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.javaagent.instrumentation.servlet.v2_2.Servlet2SpanNameExtractor Maven / Gradle / Ivy

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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy