Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Webflux into client and server #12852

Merged
merged 15 commits into from
Dec 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static WebClientBeanPostProcessor otelWebClientBeanPostProcessor(

@Bean
WebFilter telemetryFilter(OpenTelemetry openTelemetry, ConfigProperties config) {
return WebClientBeanPostProcessor.getWebfluxTelemetry(openTelemetry, config)
return WebClientBeanPostProcessor.getWebfluxServerTelemetry(openTelemetry, config)
.createWebFilterAndRegisterReactorHook();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.InstrumentationConfigUtil;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemetry;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxClientTelemetry;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxServerTelemetry;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.SpringWebfluxBuilderUtil;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import org.springframework.beans.factory.ObjectProvider;
Expand All @@ -31,12 +32,20 @@ final class WebClientBeanPostProcessor implements BeanPostProcessor {
this.configPropertiesProvider = configPropertiesProvider;
}

static SpringWebfluxTelemetry getWebfluxTelemetry(
static SpringWebfluxClientTelemetry getWebfluxClientTelemetry(
OpenTelemetry openTelemetry, ConfigProperties config) {
return InstrumentationConfigUtil.configureClientAndServerBuilder(
return InstrumentationConfigUtil.configureClientBuilder(
config,
SpringWebfluxTelemetry.builder(openTelemetry),
SpringWebfluxBuilderUtil.getClientBuilderExtractor(),
SpringWebfluxClientTelemetry.builder(openTelemetry),
SpringWebfluxBuilderUtil.getClientBuilderExtractor())
.build();
}

static SpringWebfluxServerTelemetry getWebfluxServerTelemetry(
OpenTelemetry openTelemetry, ConfigProperties config) {
return InstrumentationConfigUtil.configureServerBuilder(
config,
SpringWebfluxServerTelemetry.builder(openTelemetry),
SpringWebfluxBuilderUtil.getServerBuilderExtractor())
.build();
}
Expand All @@ -54,9 +63,9 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
}

private WebClient.Builder wrapBuilder(WebClient.Builder webClientBuilder) {
SpringWebfluxTelemetry instrumentation =
getWebfluxTelemetry(
SpringWebfluxClientTelemetry instrumentation =
getWebfluxClientTelemetry(
openTelemetryProvider.getObject(), configPropertiesProvider.getObject());
return webClientBuilder.filters(instrumentation::addClientTracingFilter);
return webClientBuilder.filters(instrumentation::addFilter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@
public final class InstrumentationConfigUtil {
private InstrumentationConfigUtil() {}

@CanIgnoreReturnValue
public static <T, CLIENTREQUEST, CLIENTRESPONSE, SERVERREQUEST, SERVERRESPONSE>
T configureClientAndServerBuilder(
ConfigProperties config,
T builder,
Function<T, DefaultHttpClientInstrumenterBuilder<CLIENTREQUEST, CLIENTRESPONSE>>
getClientBuilder,
Function<T, DefaultHttpServerInstrumenterBuilder<SERVERREQUEST, SERVERRESPONSE>>
getServerBuilder) {
CommonConfig commonConfig = getConfig(config);
getClientBuilder.apply(builder).configure(commonConfig);
getServerBuilder.apply(builder).configure(commonConfig);
return builder;
}

@CanIgnoreReturnValue
public static <T, REQUEST, RESPONSE> T configureClientBuilder(
ConfigProperties config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ For this instrumentation, the minimum supported version of Spring Webflux is 5.3
For Maven, add to your `pom.xml`:

```xml

<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
Expand Down Expand Up @@ -59,23 +60,24 @@ import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemet

@Configuration
public class WebClientConfig {
private final SpringWebfluxTelemetry webfluxTelemetry;
private final SpringWebfluxClientTelemetry webfluxClientTelemetry;
private final SpringWebfluxServerTelemetry webfluxServerTelemetry;

public WebClientConfig(OpenTelemetry openTelemetry) {
this.webfluxTelemetry = SpringWebfluxTelemetry.builder(openTelemetry).build();
this.webfluxClientTelemetry = SpringWebfluxClientTelemetry.builder(openTelemetry).build();
}

// Adds instrumentation to WebClients
@Bean
public WebClient.Builder webClient() {
WebClient webClient = WebClient.create();
return webClient.mutate().filters(webfluxTelemetry::addClientTracingFilter);
return webClient.mutate().filters(webfluxClientTelemetry::addFilter);
}

// Adds instrumentation to Webflux server
@Bean
public WebFilter webFilter() {
return webfluxTelemetry.createWebFilterAndRegisterReactorHook();
return webfluxServerTelemetry.createWebFilterAndRegisterReactorHook();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.webflux.v5_3;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.WebClientTracingFilter;
import java.util.List;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;

/** Entrypoint for instrumenting Spring Webflux HTTP clients. */
public final class SpringWebfluxClientTelemetry {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as with armeria, do we want client and server instrumentation in separate packages as with ktor


/**
* Returns a new {@link SpringWebfluxClientTelemetry} configured with the given {@link
* OpenTelemetry}.
*/
public static SpringWebfluxClientTelemetry create(OpenTelemetry openTelemetry) {
return builder(openTelemetry).build();
}

/**
* Returns a new {@link SpringWebfluxClientTelemetryBuilder} configured with the given {@link
* OpenTelemetry}.
*/
public static SpringWebfluxClientTelemetryBuilder builder(OpenTelemetry openTelemetry) {
return new SpringWebfluxClientTelemetryBuilder(openTelemetry);
}

private final Instrumenter<ClientRequest, ClientResponse> clientInstrumenter;
private final ContextPropagators propagators;

SpringWebfluxClientTelemetry(
Instrumenter<ClientRequest, ClientResponse> clientInstrumenter,
ContextPropagators propagators) {
this.clientInstrumenter = clientInstrumenter;
this.propagators = propagators;
}

public void addFilter(List<ExchangeFilterFunction> exchangeFilterFunctions) {
for (ExchangeFilterFunction filterFunction : exchangeFilterFunctions) {
if (filterFunction instanceof WebClientTracingFilter) {
return;
}
}
exchangeFilterFunctions.add(new WebClientTracingFilter(clientInstrumenter, propagators));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.webflux.v5_3;

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.spring.webflux.v5_3.internal.Experimental;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.SpringWebfluxBuilderUtil;
import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.WebClientHttpAttributesGetter;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;

/** A builder of {@link SpringWebfluxClientTelemetry}. */
public final class SpringWebfluxClientTelemetryBuilder {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-webflux-5.3";

private final DefaultHttpClientInstrumenterBuilder<ClientRequest, ClientResponse> builder;
private final OpenTelemetry openTelemetry;

static {
SpringWebfluxBuilderUtil.setClientBuilderExtractor(builder -> builder.builder);
Experimental.setSetEmitExperimentalClientTelemetry(
(builder, emit) -> builder.builder.setEmitExperimentalHttpClientMetrics(emit));
}

SpringWebfluxClientTelemetryBuilder(OpenTelemetry openTelemetry) {
builder =
DefaultHttpClientInstrumenterBuilder.create(
INSTRUMENTATION_NAME, openTelemetry, WebClientHttpAttributesGetter.INSTANCE);
this.openTelemetry = openTelemetry;
}

/**
* Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented
* items for WebClient.
*/
@CanIgnoreReturnValue
public SpringWebfluxClientTelemetryBuilder addAttributesExtractor(
AttributesExtractor<ClientRequest, ClientResponse> attributesExtractor) {
builder.addAttributesExtractor(attributesExtractor);
return this;
}

/**
* Configures the HTTP WebClient request headers that will be captured as span attributes.
*
* @param requestHeaders A list of HTTP header names.
*/
@CanIgnoreReturnValue
public SpringWebfluxClientTelemetryBuilder setCapturedRequestHeaders(
List<String> requestHeaders) {
builder.setCapturedRequestHeaders(requestHeaders);
return this;
}

/**
* Configures the HTTP WebClient response headers that will be captured as span attributes.
*
* @param responseHeaders A list of HTTP header names.
*/
@CanIgnoreReturnValue
public SpringWebfluxClientTelemetryBuilder setCapturedResponseHeaders(
List<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(Set)
*/
@CanIgnoreReturnValue
public SpringWebfluxClientTelemetryBuilder setKnownMethods(Set<String> knownMethods) {
builder.setKnownMethods(knownMethods);
return this;
}

/** Sets custom client {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public SpringWebfluxClientTelemetryBuilder setSpanNameExtractor(
Function<
SpanNameExtractor<? super ClientRequest>,
? extends SpanNameExtractor<? super ClientRequest>>
clientSpanNameExtractor) {
builder.setSpanNameExtractor(clientSpanNameExtractor);
return this;
}

/**
* Returns a new {@link SpringWebfluxClientTelemetry} with the settings of this {@link
* SpringWebfluxClientTelemetryBuilder}.
*/
public SpringWebfluxClientTelemetry build() {
return new SpringWebfluxClientTelemetry(builder.build(), openTelemetry.getPropagators());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.webflux.v5_3;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.reactor.v3_1.ContextPropagationOperator;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;

/** Entrypoint for instrumenting Spring Webflux HTTP services. */
public final class SpringWebfluxServerTelemetry {

/**
* Returns a new {@link SpringWebfluxServerTelemetry} configured with the given {@link
* OpenTelemetry}.
*/
public static SpringWebfluxServerTelemetry create(OpenTelemetry openTelemetry) {
return builder(openTelemetry).build();
}

/**
* Returns a new {@link SpringWebfluxServerTelemetryBuilder} configured with the given {@link
* OpenTelemetry}.
*/
public static SpringWebfluxServerTelemetryBuilder builder(OpenTelemetry openTelemetry) {
return new SpringWebfluxServerTelemetryBuilder(openTelemetry);
}

// We use ServerWebExchange (which holds both the request and response)
// because we need it to get the HTTP route while instrumenting.
private final Instrumenter<ServerWebExchange, ServerWebExchange> serverInstrumenter;

SpringWebfluxServerTelemetry(
Instrumenter<ServerWebExchange, ServerWebExchange> serverInstrumenter) {
this.serverInstrumenter = serverInstrumenter;
}

public WebFilter createWebFilter() {
return new TelemetryProducingWebFilter(serverInstrumenter);
}

public WebFilter createWebFilterAndRegisterReactorHook() {
registerReactorHook();
return this.createWebFilter();
}

private static void registerReactorHook() {
ContextPropagationOperator.builder().build().registerOnEachOperator();
}
}
Loading
Loading