Skip to content

Commit

Permalink
feat: add Micrometer Reactor metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
aoudiamoncef committed Feb 11, 2024
1 parent 2209ea8 commit d3a2d58
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<env name="reactor.schedulers.defaultBoundedElasticOnVirtualThreads" value="true" />
<env name="java.rmi.server.hostname" value="localhost" />
<env name="enable-reactor-debug-agent" value="true" />
<env name="enable-reactor-metrics" value="true" />
</envs>
<module name="bulk-update-with-spring-data-mongodb-reactive" />
<option name="SPRING_BOOT_MAIN_CLASS" value="com.maoudia.tutorial.Application" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ services:
networks:
- mongodb-network
environment:
- ME_CONFIG_MONGODB_URL=mongodb://maoudia-mongodb:15015/?replicaSet=rs0
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=password
- ME_CONFIG_MONGODB_URL=mongodb://maoudia-mongodb:15015
ports:
- 1515:8081
volumes:
Expand Down
4 changes: 2 additions & 2 deletions bulk-update-with-spring-data-mongodb-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
<description>Bulk Update with Spring data MongoDB Reactive</description>
<properties>
<java.version>21</java.version>
<netty-resolver-dns-native-macos.version>4.1.76.Final</netty-resolver-dns-native-macos.version>
<byte-buddy.version>1.14.11</byte-buddy.version>
<reactor-tools.version>3.6.2</reactor-tools.version>
<netty-resolver-dns-native-macos.version>4.1.76.Final</netty-resolver-dns-native-macos.version>

<enable-reactor-debug-agent>false</enable-reactor-debug-agent>
<enable-reactor-metrics>false</enable-reactor-metrics>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -51,7 +52,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-tools</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.maoudia.tutorial;

import com.mongodb.TransactionOptions;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.ReplaceOptions;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.ObservationRegistry;
import org.bson.Document;
import org.reactivestreams.Publisher;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.reactive.TransactionalOperator;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.observability.micrometer.Micrometer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -36,21 +37,26 @@ private static ReplaceOneModel<Document> toReplaceOneModel(Document document) {
private final ReactiveMongoTemplate template;
private final WebClient client;
private final TransactionalOperator transactionalOperator;
private final MeterRegistry meterRegistry;

public CollectionService(AppProperties properties,
ReactiveMongoTemplate template,
WebClient client,
TransactionalOperator transactionalOperator) {
TransactionalOperator transactionalOperator, ObservationRegistry observationRegistry, MeterRegistry meterRegistry) {
this.properties = properties;
this.template = template;
this.client = client;
this.transactionalOperator = transactionalOperator;
this.meterRegistry = meterRegistry;
}

public Flux<BulkWriteResult> enrichAll(String collectionName,
String enrichingKey,
URI enrichingUri) {
return template.findAll(Document.class, collectionName)
.name("app.documents.flux")
.tag("source", "mongodb")
.tap(Micrometer.metrics(meterRegistry))
.onBackpressureBuffer(properties.bufferMaxSize())
.flatMap(document -> enrich(document, enrichingKey, enrichingUri))
.map(CollectionService::toReplaceOneModel)
Expand All @@ -73,16 +79,24 @@ private Mono<Document> getEnrichingDocument(URI enrichingUri) {
return client.get()
.uri(enrichingUri)
.retrieve()
.bodyToMono(Document.class);
.bodyToMono(Document.class)
.name("app.enriching.call")
.tag("source", "http")
.doOnNext(unused -> meterRegistry.getMeters())
.tap(Micrometer.metrics(meterRegistry));
}

private Publisher<BulkWriteResult> bulkWrite(Flux<ReplaceOneModel<Document>> updateOneModelFlux,
String collectionName) {
return updateOneModelFlux
.name("app.documents.bulk")
.tap(Micrometer.metrics(meterRegistry))
.collectList()
.flatMapMany(updateOneModels -> template.getCollection(collectionName)
.flatMapMany(collection -> collection.bulkWrite(updateOneModels, BULK_WRITE_OPTIONS)))
.as(transactionalOperator::transactional);
.as(transactionalOperator::transactional)
.name("app.transactions")
.tap(Micrometer.metrics(meterRegistry));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ spring:
application:
name: bulk-update-with-spring-data-mongodb-reactive
main:
web-application-type: none
web-application-type: NONE
data:
mongodb:
database: test
uri: mongodb://localhost:15015/?replicaSet=rs0&retryWrites=false
uri: mongodb://localhost:15015
---
spring.config.activate.on-profile: dev
logging:
level:
org.mongodb.driver: debug
org.mongodb.driver: DEBUG
---
spring.config.activate.on-profile: test
app:
Expand Down

0 comments on commit d3a2d58

Please sign in to comment.