|
| 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 | + Experimental.setSetEmitExperimentalClientTelemetry( |
| 33 | + (builder, emit) -> builder.builder.setEmitExperimentalHttpClientMetrics(emit)); |
| 34 | + } |
| 35 | + |
| 36 | + SpringWebfluxClientTelemetryBuilder(OpenTelemetry openTelemetry) { |
| 37 | + builder = |
| 38 | + DefaultHttpClientInstrumenterBuilder.create( |
| 39 | + INSTRUMENTATION_NAME, openTelemetry, WebClientHttpAttributesGetter.INSTANCE); |
| 40 | + this.openTelemetry = openTelemetry; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented |
| 45 | + * items for WebClient. |
| 46 | + */ |
| 47 | + @CanIgnoreReturnValue |
| 48 | + public SpringWebfluxClientTelemetryBuilder addAttributesExtractor( |
| 49 | + AttributesExtractor<ClientRequest, ClientResponse> attributesExtractor) { |
| 50 | + builder.addAttributesExtractor(attributesExtractor); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Configures the HTTP WebClient request headers that will be captured as span attributes. |
| 56 | + * |
| 57 | + * @param requestHeaders A list of HTTP header names. |
| 58 | + */ |
| 59 | + @CanIgnoreReturnValue |
| 60 | + public SpringWebfluxClientTelemetryBuilder setCapturedRequestHeaders( |
| 61 | + List<String> requestHeaders) { |
| 62 | + builder.setCapturedRequestHeaders(requestHeaders); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Configures the HTTP WebClient response headers that will be captured as span attributes. |
| 68 | + * |
| 69 | + * @param responseHeaders A list of HTTP header names. |
| 70 | + */ |
| 71 | + @CanIgnoreReturnValue |
| 72 | + public SpringWebfluxClientTelemetryBuilder setCapturedResponseHeaders( |
| 73 | + List<String> responseHeaders) { |
| 74 | + builder.setCapturedResponseHeaders(responseHeaders); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Configures the instrumentation to recognize an alternative set of HTTP request methods. |
| 80 | + * |
| 81 | + * <p>By default, this instrumentation defines "known" methods as the ones listed in <a |
| 82 | + * href="https://www.rfc-editor.org/rfc/rfc9110.html#name-methods">RFC9110</a> and the PATCH |
| 83 | + * method defined in <a href="https://www.rfc-editor.org/rfc/rfc5789.html">RFC5789</a>. |
| 84 | + * |
| 85 | + * <p>Note: calling this method <b>overrides</b> the default known method sets completely; it does |
| 86 | + * not supplement it. |
| 87 | + * |
| 88 | + * @param knownMethods A set of recognized HTTP request methods. |
| 89 | + * @see HttpClientAttributesExtractorBuilder#setKnownMethods(Set) |
| 90 | + */ |
| 91 | + @CanIgnoreReturnValue |
| 92 | + public SpringWebfluxClientTelemetryBuilder setKnownMethods(Set<String> knownMethods) { |
| 93 | + builder.setKnownMethods(knownMethods); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** Sets custom client {@link SpanNameExtractor} via transform function. */ |
| 98 | + @CanIgnoreReturnValue |
| 99 | + public SpringWebfluxClientTelemetryBuilder setSpanNameExtractor( |
| 100 | + Function< |
| 101 | + SpanNameExtractor<? super ClientRequest>, |
| 102 | + ? extends SpanNameExtractor<? super ClientRequest>> |
| 103 | + clientSpanNameExtractor) { |
| 104 | + builder.setSpanNameExtractor(clientSpanNameExtractor); |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns a new {@link SpringWebfluxClientTelemetry} with the settings of this {@link |
| 110 | + * SpringWebfluxClientTelemetryBuilder}. |
| 111 | + */ |
| 112 | + public SpringWebfluxClientTelemetry build() { |
| 113 | + return new SpringWebfluxClientTelemetry(builder.build(), openTelemetry.getPropagators()); |
| 114 | + } |
| 115 | +} |
0 commit comments