content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.instrumentation.javahttpserver;
  
  import com.google.errorprone.annotations.CanIgnoreReturnValue;
  import com.sun.net.httpserver.HttpExchange;
  import io.opentelemetry.api.OpenTelemetry;
  import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder;
  import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
  import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
  import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor;
  import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder;
  import io.opentelemetry.instrumentation.javahttpserver.internal.Experimental;
  import io.opentelemetry.instrumentation.javahttpserver.internal.JavaHttpServerInstrumenterBuilderUtil;
  import java.util.Collection;
  import java.util.function.Function;
  
  public final class JavaHttpServerTelemetryBuilder {
  
    private static final String INSTRUMENTATION_NAME = "io.opentelemetry.java-http-server";
  
    private final DefaultHttpServerInstrumenterBuilder<HttpExchange, HttpExchange> builder;
  
    static {
      JavaHttpServerInstrumenterBuilderUtil.setServerBuilderExtractor(builder -> builder.builder);
      Experimental.internalSetEmitExperimentalTelemetry(
          (builder, emit) -> builder.builder.setEmitExperimentalHttpServerTelemetry(emit));
    }
  
    JavaHttpServerTelemetryBuilder(OpenTelemetry openTelemetry) {
      builder =
          DefaultHttpServerInstrumenterBuilder.create(
              INSTRUMENTATION_NAME,
              openTelemetry,
              JavaHttpServerAttributesGetter.INSTANCE,
              JavaHttpServerExchangeGetter.INSTANCE);
    }
  
    /** Sets the status extractor for server spans. */
    @CanIgnoreReturnValue
    public JavaHttpServerTelemetryBuilder setStatusExtractor(
        Function<
                SpanStatusExtractor<HttpExchange, HttpExchange>,
                SpanStatusExtractor<HttpExchange, HttpExchange>>
            statusExtractor) {
      builder.setStatusExtractor(statusExtractor);
      return this;
    }
  
    /**
     * Adds an extra {@link AttributesExtractor} to invoke to set attributes to instrumented items.
     * The {@link AttributesExtractor} will be executed after all default extractors.
     */
    @CanIgnoreReturnValue
    public JavaHttpServerTelemetryBuilder addAttributesExtractor(
        AttributesExtractor<HttpExchange, HttpExchange> attributesExtractor) {
      builder.addAttributesExtractor(attributesExtractor);
      return this;
    }
  
    /**
     * Configures the HTTP server request headers that will be captured as span attributes.
     *
     * @param requestHeaders A list of HTTP header names.
     */
    @CanIgnoreReturnValue
    public JavaHttpServerTelemetryBuilder setCapturedRequestHeaders(
        Collection<String> requestHeaders) {
      builder.setCapturedRequestHeaders(requestHeaders);
      return this;
    }
  
    /**
     * Configures the HTTP server response headers that will be captured as span attributes.
     *
     * @param responseHeaders A list of HTTP header names.
     */
    @CanIgnoreReturnValue
    public JavaHttpServerTelemetryBuilder setCapturedResponseHeaders(
        Collection<String> responseHeaders) {
      builder.setCapturedResponseHeaders(responseHeaders);
      return this;
    }
  
    /**
     * Configures the instrumentation to recognize an alternative set of HTTP request methods.
     *
     * <p>By default, this instrumentation defines "known" methods as the ones listed in <a
     * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-methods">RFC9110</a> and the PATCH
     * method defined in <a href="https://www.rfc-editor.org/rfc/rfc5789.html">RFC5789</a>.
     *
     * <p>Note: calling this method <b>overrides</b> the default known method sets completely; it does
     * not supplement it.
     *
     * @param knownMethods A set of recognized HTTP request methods.
     * @see HttpServerAttributesExtractorBuilder#setKnownMethods(Collection)
     */
    @CanIgnoreReturnValue
    public JavaHttpServerTelemetryBuilder setKnownMethods(Collection<String> knownMethods) {
      builder.setKnownMethods(knownMethods);
      return this;
    }
  
    /** Sets custom server {@link SpanNameExtractor} via transform function. */
    @CanIgnoreReturnValue
    public JavaHttpServerTelemetryBuilder setSpanNameExtractor(
        Function<SpanNameExtractor<HttpExchange>, SpanNameExtractor<HttpExchange>>
            serverSpanNameExtractor) {
      builder.setSpanNameExtractor(serverSpanNameExtractor);
      return this;
    }
  
    public JavaHttpServerTelemetry build() {
      return new JavaHttpServerTelemetry(builder.build());
    }
  }
  