opentelemetry-java-instrume.../instrumentation/spring/spring-webflux/spring-webflux-5.3/library
James Moessis 3f45f755a9
Spring Webflux Library Instrumentation (#7899)
Resolves
https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/7436

* Created new Module `spring-webflux-5.3` which contains only
server-side library instrumentation
* Minimum supported version is 5.3 because there are various problems in
older versions of reactor and webflux that prevent a few of the tests
from passing.
* Moved existing `spring-webflux-5.0` (webclient instrumentation) into a
common `spring-webflux` folder next to the 5.3 (server) instrumentation.
Moved the README to the parent folder so the docs are cohesive between
client/server instrumentation.
* Implemented `WebFilter` which instruments the server-side 
* Depends on the `reactor-3.1` instrumentation to pass the context
around. Registers the react hook when it creates the `WebFilter`
* Tests using the standard HTTP server test suite

---------

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
Co-authored-by: Mateusz Rzeszutek <mrzeszutek@splunk.com>
2023-03-08 08:43:46 +01:00
..
src Spring Webflux Library Instrumentation (#7899) 2023-03-08 08:43:46 +01:00
NOTICE.txt Spring Webflux Library Instrumentation (#7899) 2023-03-08 08:43:46 +01:00
README.md Spring Webflux Library Instrumentation (#7899) 2023-03-08 08:43:46 +01:00
build.gradle.kts Spring Webflux Library Instrumentation (#7899) 2023-03-08 08:43:46 +01:00

README.md

Library Instrumentation for Spring Webflux

Provides OpenTelemetry instrumentation for Spring's WebClient and Webflux server.

For this instrumentation, the minimum supported version of Spring Webflux is 5.3.0.

Add dependencies to your project

For Maven, add to your pom.xml:

<dependencies>
  <dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-spring-webflux-5.3</artifactId>
    <version>OPENTELEMETRY_VERSION</version>
  </dependency>

  <!-- This artifact should already be present in your application -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webflux</artifactId>
    <version>SPRING_VERSION</version>
  </dependency>
</dependencies>

For Gradle, add to your dependencies:

implementation("io.opentelemetry.instrumentation:opentelemetry-spring-webflux-5.3:OPENTELEMETRY_VERSION")

// this artifact should already be present in your application
implementation("org.springframework:spring-webflux:SPRING_VERSION")

Features

SpringWebfluxTelemetry can emit a client span for each request sent using WebClient by implementing the ExchangeFilterFunction interface.

SpringWebfluxTelemetry can also emit a server span for each request received, by implementing a WebFilter and using the OpenTelemetry Reactor instrumentation to ensure context is passed around correctly.

Setup

Here is how to set up client and server instrumentation respectively:

import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemetry;

@Configuration
public class WebClientConfig {
  private final SpringWebfluxTelemetry webfluxTelemetry;

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

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

  // Adds instrumentation to Webflux server
  @Bean
  public WebFilter webFilter() {
    return webfluxTelemetry.createWebFilterAndRegisterReactorHook();
  }
}

Starter Guide

Check out OpenTelemetry Manual Instrumentation to learn more about using the OpenTelemetry API to instrument your code.