| 
 | 1 | +/*  | 
 | 2 | + * Copyright The OpenTelemetry Authors  | 
 | 3 | + * SPDX-License-Identifier: Apache-2.0  | 
 | 4 | + */  | 
 | 5 | + | 
 | 6 | +package io.opentelemetry.instrumentation.spring.webflux.v5_3;  | 
 | 7 | + | 
 | 8 | +import com.google.errorprone.annotations.CanIgnoreReturnValue;  | 
 | 9 | +import io.opentelemetry.api.OpenTelemetry;  | 
 | 10 | +import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;  | 
 | 11 | +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;  | 
 | 12 | +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;  | 
 | 13 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;  | 
 | 14 | +import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.Experimental;  | 
 | 15 | +import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.SpringWebfluxBuilderUtil;  | 
 | 16 | +import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.WebClientHttpAttributesGetter;  | 
 | 17 | +import java.util.List;  | 
 | 18 | +import java.util.Set;  | 
 | 19 | +import java.util.function.Function;  | 
 | 20 | +import org.springframework.web.reactive.function.client.ClientRequest;  | 
 | 21 | +import org.springframework.web.reactive.function.client.ClientResponse;  | 
 | 22 | + | 
 | 23 | +/** A builder of {@link SpringWebfluxClientTelemetry}. */  | 
 | 24 | +public final class SpringWebfluxClientTelemetryBuilder {  | 
 | 25 | +  private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-webflux-5.3";  | 
 | 26 | + | 
 | 27 | +  private final DefaultHttpClientInstrumenterBuilder<ClientRequest, ClientResponse> builder;  | 
 | 28 | +  private final OpenTelemetry openTelemetry;  | 
 | 29 | + | 
 | 30 | +  static {  | 
 | 31 | +    SpringWebfluxBuilderUtil.setClientBuilderExtractor(builder -> builder.builder);  | 
 | 32 | +  }  | 
 | 33 | + | 
 | 34 | +  SpringWebfluxClientTelemetryBuilder(OpenTelemetry openTelemetry) {  | 
 | 35 | +    builder =  | 
 | 36 | +        DefaultHttpClientInstrumenterBuilder.create(  | 
 | 37 | +            INSTRUMENTATION_NAME, openTelemetry, WebClientHttpAttributesGetter.INSTANCE);  | 
 | 38 | +    this.openTelemetry = openTelemetry;  | 
 | 39 | +  }  | 
 | 40 | + | 
 | 41 | +  /**  | 
 | 42 | +   * Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented  | 
 | 43 | +   * items for WebClient.  | 
 | 44 | +   */  | 
 | 45 | +  @CanIgnoreReturnValue  | 
 | 46 | +  public SpringWebfluxClientTelemetryBuilder addAttributesExtractor(  | 
 | 47 | +      AttributesExtractor<ClientRequest, ClientResponse> attributesExtractor) {  | 
 | 48 | +    builder.addAttributeExtractor(attributesExtractor);  | 
 | 49 | +    return this;  | 
 | 50 | +  }  | 
 | 51 | + | 
 | 52 | +  /**  | 
 | 53 | +   * Configures the HTTP WebClient request headers that will be captured as span attributes.  | 
 | 54 | +   *  | 
 | 55 | +   * @param requestHeaders A list of HTTP header names.  | 
 | 56 | +   */  | 
 | 57 | +  @CanIgnoreReturnValue  | 
 | 58 | +  public SpringWebfluxClientTelemetryBuilder setCapturedRequestHeaders(  | 
 | 59 | +      List<String> requestHeaders) {  | 
 | 60 | +    builder.setCapturedRequestHeaders(requestHeaders);  | 
 | 61 | +    return this;  | 
 | 62 | +  }  | 
 | 63 | + | 
 | 64 | +  /**  | 
 | 65 | +   * Configures the HTTP WebClient response headers that will be captured as span attributes.  | 
 | 66 | +   *  | 
 | 67 | +   * @param responseHeaders A list of HTTP header names.  | 
 | 68 | +   */  | 
 | 69 | +  @CanIgnoreReturnValue  | 
 | 70 | +  public SpringWebfluxClientTelemetryBuilder setCapturedResponseHeaders(  | 
 | 71 | +      List<String> responseHeaders) {  | 
 | 72 | +    builder.setCapturedResponseHeaders(responseHeaders);  | 
 | 73 | +    return this;  | 
 | 74 | +  }  | 
 | 75 | + | 
 | 76 | +  /**  | 
 | 77 | +   * Configures the instrumentation to recognize an alternative set of HTTP request methods.  | 
 | 78 | +   *  | 
 | 79 | +   * <p>By default, this instrumentation defines "known" methods as the ones listed in <a  | 
 | 80 | +   * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-methods">RFC9110</a> and the PATCH  | 
 | 81 | +   * method defined in <a href="https://www.rfc-editor.org/rfc/rfc5789.html">RFC5789</a>.  | 
 | 82 | +   *  | 
 | 83 | +   * <p>Note: calling this method <b>overrides</b> the default known method sets completely; it does  | 
 | 84 | +   * not supplement it.  | 
 | 85 | +   *  | 
 | 86 | +   * @param knownMethods A set of recognized HTTP request methods.  | 
 | 87 | +   * @see HttpClientAttributesExtractorBuilder#setKnownMethods(Set)  | 
 | 88 | +   */  | 
 | 89 | +  @CanIgnoreReturnValue  | 
 | 90 | +  public SpringWebfluxClientTelemetryBuilder setKnownMethods(Set<String> knownMethods) {  | 
 | 91 | +    builder.setKnownMethods(knownMethods);  | 
 | 92 | +    return this;  | 
 | 93 | +  }  | 
 | 94 | + | 
 | 95 | +  /** Sets custom client {@link SpanNameExtractor} via transform function. */  | 
 | 96 | +  @CanIgnoreReturnValue  | 
 | 97 | +  public SpringWebfluxClientTelemetryBuilder setSpanNameExtractor(  | 
 | 98 | +      Function<  | 
 | 99 | +              SpanNameExtractor<? super ClientRequest>,  | 
 | 100 | +              ? extends SpanNameExtractor<? super ClientRequest>>  | 
 | 101 | +          clientSpanNameExtractor) {  | 
 | 102 | +    builder.setSpanNameExtractor(clientSpanNameExtractor);  | 
 | 103 | +    return this;  | 
 | 104 | +  }  | 
 | 105 | + | 
 | 106 | +  /**  | 
 | 107 | +   * Can be used via the unstable method {@link  | 
 | 108 | +   * Experimental#setEmitExperimentalTelemetry(SpringWebfluxClientTelemetryBuilder, boolean)}.  | 
 | 109 | +   */  | 
 | 110 | +  void setEmitExperimentalHttpClientTelemetry(boolean emitExperimentalHttpClientTelemetry) {  | 
 | 111 | +    builder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientTelemetry);  | 
 | 112 | +  }  | 
 | 113 | + | 
 | 114 | +  /**  | 
 | 115 | +   * Returns a new {@link SpringWebfluxClientTelemetry} with the settings of this {@link  | 
 | 116 | +   * SpringWebfluxClientTelemetryBuilder}.  | 
 | 117 | +   */  | 
 | 118 | +  public SpringWebfluxClientTelemetry build() {  | 
 | 119 | +    return new SpringWebfluxClientTelemetry(builder.build(), openTelemetry.getPropagators());  | 
 | 120 | +  }  | 
 | 121 | +}  | 
0 commit comments