content: /*
   * Copyright The OpenTelemetry Authors
   * SPDX-License-Identifier: Apache-2.0
   */
  
  package io.opentelemetry.instrumentation.javahttpclient;
  
  import com.google.errorprone.annotations.CanIgnoreReturnValue;
  import io.opentelemetry.api.OpenTelemetry;
  import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
  import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
  import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
  import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
  import io.opentelemetry.instrumentation.javahttpclient.internal.Experimental;
  import io.opentelemetry.instrumentation.javahttpclient.internal.HttpHeadersSetter;
  import io.opentelemetry.instrumentation.javahttpclient.internal.JavaHttpClientInstrumenterBuilderFactory;
  import java.net.http.HttpRequest;
  import java.net.http.HttpResponse;
  import java.util.Collection;
  import java.util.function.Function;
  
  public final class JavaHttpClientTelemetryBuilder {
  
    private final DefaultHttpClientInstrumenterBuilder<HttpRequest, HttpResponse<?>> builder;
    private final OpenTelemetry openTelemetry;
  
    static {
      Experimental.internalSetEmitExperimentalTelemetry(
          (builder, emit) -> builder.builder.setEmitExperimentalHttpClientTelemetry(emit));
    }
  
    JavaHttpClientTelemetryBuilder(OpenTelemetry openTelemetry) {
      builder = JavaHttpClientInstrumenterBuilderFactory.create(openTelemetry);
      this.openTelemetry = openTelemetry;
    }
  
    /**
     * Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented
     * items. The {@link AttributesExtractor} will be executed after all default extractors.
     */
    @CanIgnoreReturnValue
    public JavaHttpClientTelemetryBuilder addAttributesExtractor(
        AttributesExtractor<? super HttpRequest, ? super HttpResponse<?>> attributesExtractor) {
      builder.addAttributesExtractor(attributesExtractor);
      return this;
    }
  
    /**
     * Configures the HTTP request headers that will be captured as span attributes.
     *
     * @param requestHeaders A list of HTTP header names.
     */
    @CanIgnoreReturnValue
    public JavaHttpClientTelemetryBuilder setCapturedRequestHeaders(
        Collection<String> requestHeaders) {
      builder.setCapturedRequestHeaders(requestHeaders);
      return this;
    }
  
    /**
     * Configures the HTTP response headers that will be captured as span attributes.
     *
     * @param responseHeaders A list of HTTP header names.
     */
    @CanIgnoreReturnValue
    public JavaHttpClientTelemetryBuilder 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 HttpClientAttributesExtractorBuilder#setKnownMethods(Collection)
     */
    @CanIgnoreReturnValue
    public JavaHttpClientTelemetryBuilder setKnownMethods(Collection<String> knownMethods) {
      builder.setKnownMethods(knownMethods);
      return this;
    }
  
    /** Sets custom {@link SpanNameExtractor} via transform function. */
    @CanIgnoreReturnValue
    public JavaHttpClientTelemetryBuilder setSpanNameExtractor(
        Function<SpanNameExtractor<HttpRequest>, SpanNameExtractor<HttpRequest>>
            spanNameExtractorTransformer) {
      builder.setSpanNameExtractor(spanNameExtractorTransformer);
      return this;
    }
  
    public JavaHttpClientTelemetry build() {
      return new JavaHttpClientTelemetry(
          builder.build(), new HttpHeadersSetter(openTelemetry.getPropagators()));
    }
  }
  