Reactive mongo (#2044)

Added support for the new Reactive and Synchronous Context Providers in Mongo
This commit is contained in:
Marcin Grzejszczak
2021-10-26 16:13:57 +02:00
committed by GitHub
parent 90417f1f07
commit eb01d5952b
19 changed files with 1102 additions and 6 deletions

View File

@@ -51,6 +51,7 @@
<module>spring-cloud-sleuth-instrumentation-kotlin-tests</module>
<module>spring-cloud-sleuth-instrumentation-lettuce-tests</module>
<module>spring-cloud-sleuth-instrumentation-messaging-tests</module>
<module>spring-cloud-sleuth-instrumentation-mongodb-tests</module>
<module>spring-cloud-sleuth-instrumentation-mvc-tests</module>
<module>spring-cloud-sleuth-instrumentation-quartz-tests</module>
<module>spring-cloud-sleuth-instrumentation-reactor-tests</module>

View File

@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013-2021 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
~
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-sleuth-instrumentation-mongodb-reactive-tests</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Sleuth Brave MongoDB Reactive Instrumentation Tests</name>
<description>Spring Cloud Sleuth Brave MongoDB Reactive Instrumentation Tests</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-tests-brave</artifactId>
<version>3.1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<properties>
<sonar.skip>true</sonar.skip>
</properties>
<build>
<plugins>
<plugin>
<!--skip deploy -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-tests-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-tests</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.brave.instrument.mongodb;
import brave.sampler.Sampler;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.sleuth.brave.BraveTestSpanHandler;
import org.springframework.cloud.sleuth.test.TestSpanHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
@SpringBootTest
@ContextConfiguration(classes = ReactiveMongoDbIntegrationTests.Config.class)
public class ReactiveMongoDbIntegrationTests
extends org.springframework.cloud.sleuth.instrument.mongodb.ReactiveMongoDbIntegrationTests {
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
TestSpanHandler testSpanHandlerSupplier(brave.test.TestSpanHandler testSpanHandler) {
return new BraveTestSpanHandler(testSpanHandler);
}
@Bean
Sampler alwaysSampler() {
return Sampler.ALWAYS_SAMPLE;
}
@Bean
brave.test.TestSpanHandler braveTestSpanHandler() {
return new brave.test.TestSpanHandler();
}
}
}

View File

@@ -0,0 +1,4 @@
logging.level.org.springframework.cloud: DEBUG
logging.level.org.springframework.cloud.sleuth.instrument.mongodb: TRACE
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration, org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration, org.springframework.cloud.gateway.config.GatewayAutoConfiguration, org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration, org.springframework.cloud.gateway.config.GatewayMetricsAutoConfiguration

View File

@@ -174,6 +174,16 @@
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-proxy</artifactId>
@@ -194,6 +204,11 @@
<artifactId>kafka</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-rsocket</artifactId>

View File

@@ -0,0 +1,171 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.instrument.mongodb;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.TraceContext;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.exporter.FinishedSpan;
import org.springframework.cloud.sleuth.test.TestSpanHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import static org.assertj.core.api.BDDAssertions.then;
@ContextConfiguration(classes = ReactiveMongoDbIntegrationTests.TestConfig.class)
@Testcontainers
public abstract class ReactiveMongoDbIntegrationTests {
private static final Log log = LogFactory.getLog(ReactiveMongoDbIntegrationTests.class);
@Autowired
TestSpanHandler spans;
@Autowired
Tracer tracer;
@Autowired
BasicUserRepository basicUserRepository;
@Container
static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo").withTag("4.4.7"));
@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
mongoDBContainer.start();
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
}
@BeforeEach
void setup() {
this.spans.clear();
}
@Test
public void should_pass_tracing_information_when_using_reactive_mongodb() {
// given
Span nextSpan = this.tracer.nextSpan().name("mongo-reactive-app");
// when
Mono.just(nextSpan).doOnNext(span -> this.tracer.withSpan(nextSpan.start())).flatMap(span -> {
log.info("Hello from flat map");
return this.basicUserRepository.save(new User("foo", "bar", "baz", null))
.flatMap(user -> this.basicUserRepository.findUserByUsername("foo"));
}).contextWrite(context -> context.put(Span.class, nextSpan).put(TraceContext.class, nextSpan.context()))
.doFinally(signalType -> nextSpan.end()).block(Duration.ofMinutes(1));
// then
List<FinishedSpan> reportedSpans = this.spans.reportedSpans();
then(reportedSpans.stream().map(FinishedSpan::getTraceId).collect(Collectors.toSet()))
.as("There must be only 1 trace id").hasSize(1);
List<String> mongoSpanNames = reportedSpans.stream()
.filter(fs -> fs.getName().equals("insert user") || fs.getName().equals("find user"))
.map(FinishedSpan::getName).collect(Collectors.toList());
then(mongoSpanNames).as("There must be first an insert then a find").containsExactly("insert user",
"find user");
}
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
public static class TestConfig {
}
}
interface BasicUserRepository extends ReactiveCrudRepository<User, Long> {
Mono<User> findUserByUsername(String username);
}
class User {
private String id;
private String username;
private String firstname;
private String lastname;
User() {
}
User(String id) {
this.setId(id);
}
User(String username, String firstname, String lastname, String id) {
this.username = username;
this.firstname = firstname;
this.lastname = lastname;
this.id = id;
}
String getId() {
return this.id;
}
void setId(String id) {
this.id = id;
}
String getUsername() {
return this.username;
}
void setUsername(String username) {
this.username = username;
}
String getFirstname() {
return this.firstname;
}
void setFirstname(String firstname) {
this.firstname = firstname;
}
String getLastname() {
return this.lastname;
}
void setLastname(String lastname) {
this.lastname = lastname;
}
}