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

io.opentelemetry.javaagent.instrumentation.tomcat.common.TomcatAdditionalAttributesExtractor Maven / Gradle / Ivy

/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.javaagent.instrumentation.tomcat.common;

import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.servlet.ServletAccessor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.security.Principal;
import javax.annotation.Nullable;
import org.apache.coyote.Request;
import org.apache.coyote.Response;

public class TomcatAdditionalAttributesExtractor
    implements AttributesExtractor {
  private final ServletAccessor accessor;
  private final TomcatServletEntityProvider servletEntityProvider;

  public TomcatAdditionalAttributesExtractor(
      ServletAccessor accessor,
      TomcatServletEntityProvider servletEntityProvider) {
    this.accessor = accessor;
    this.servletEntityProvider = servletEntityProvider;
  }

  @Override
  public void onStart(AttributesBuilder attributes, Request request) {}

  @Override
  public void onEnd(
      AttributesBuilder attributes,
      Request request,
      @Nullable Response response,
      @Nullable Throwable error) {
    REQUEST servletRequest = servletEntityProvider.getServletRequest(request);
    Principal principal = accessor.getRequestUserPrincipal(servletRequest);
    if (principal != null) {
      set(attributes, SemanticAttributes.ENDUSER_ID, principal.getName());
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy