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

Reference doc update for mongodb #4594

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
** xref:reference/httpcomponents.adoc[HttpComponents Client]
** xref:reference/jetty.adoc[Jetty and Jersey]
** xref:reference/jvm.adoc[JVM]
** xref:reference/mongodb.adoc[MongoDB]
** xref:reference/netty.adoc[Netty]
** xref:reference/okhttpclient.adoc[OkHttpClient]
* xref:guides.adoc[Guides]
Expand Down
30 changes: 30 additions & 0 deletions docs/modules/ROOT/pages/reference/mongodb.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[[overview]]
= MongoDB Metrics Instrumentation

https://www.mongodb.com/[MongoDB] is a modern database that supports transactional, search, analytics, and mobile use cases with a flexible document data model and a unified query interface.

Below you can find an example of how to instrument MongoDb with Micrometer.

TIP: To add more capabilities such as distributed tracing, please consider using https://docs.spring.io/spring-data/mongodb/reference/observability/observability.html[Spring Data MongoDb] which uses https://docs.micrometer.io/micrometer/reference/observation.html[Micrometer Observation] under the hood.

Below you can find an example of metrics for command execution.

[source,java,subs=+attributes]
-----
// Setting up instrumentation
include::{include-core-test-java}/io/micrometer/core/instrument/binder/mongodb/MongoMetricsCommandListenerTest.java[tags=setup, indent=0]

// Usage example
include::{include-core-test-java}/io/micrometer/core/instrument/binder/mongodb/MongoMetricsCommandListenerTest.java[tags=example, indent=0]
-----

Below you can find an example of metrics of the connection pool.

[source,java,subs=+attributes]
-----
// Setting up instrumentation
include::{include-core-test-java}/io/micrometer/core/instrument/binder/mongodb/MongoMetricsConnectionPoolListenerTest.java[tags=setup, indent=0]

// Usage example
include::{include-core-test-java}/io/micrometer/core/instrument/binder/mongodb/MongoMetricsConnectionPoolListenerTest.java[tags=example, indent=0]
-----
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ class MongoMetricsCommandListenerTest extends AbstractMongoDbTest {

@BeforeEach
void setup() {
registry = new SimpleMeterRegistry();
clusterId = new AtomicReference<>();
// tag::setup[]
registry = new SimpleMeterRegistry();
MongoClientSettings settings = MongoClientSettings.builder()
.addCommandListener(new MongoMetricsCommandListener(registry))
.applyToClusterSettings(builder -> builder.hosts(singletonList(new ServerAddress(host, port)))
Expand All @@ -69,15 +70,18 @@ public void clusterOpening(ClusterOpeningEvent event) {
}))
.build();
mongo = MongoClients.create(settings);
// end::setup[]
}

@Test
void shouldCreateSuccessCommandMetric() {
// tag::example[]
mongo.getDatabase("test").getCollection("testCol").insertOne(new Document("testDoc", new Date()));

Tags tags = Tags.of("cluster.id", clusterId.get(), "server.address", String.format("%s:%s", host, port),
"command", "insert", "collection", "testCol", "status", "SUCCESS");
assertThat(registry.get("mongodb.driver.commands").tags(tags).timer().count()).isEqualTo(1);
// end::example[]
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ public void clusterOpening(ClusterOpeningEvent event) {

@Test
void shouldCreatePoolMetricsWithCustomTags() {
MeterRegistry registry = new SimpleMeterRegistry();
AtomicReference<String> clusterId = new AtomicReference<>();
// tag::setup[]
MeterRegistry registry = new SimpleMeterRegistry();
MongoMetricsConnectionPoolListener connectionPoolListener = new MongoMetricsConnectionPoolListener(registry,
e -> Tags.of("cluster.id", e.getServerId().getClusterId().getValue(), "server.address",
e.getServerId().getAddress().toString(), "my.custom.connection.pool.identifier", "custom"));
Expand All @@ -96,7 +97,9 @@ public void clusterOpening(ClusterOpeningEvent event) {
}))
.build();
MongoClient mongo = MongoClients.create(settings);
// end::setup[]

// tag::example[]
mongo.getDatabase("test").createCollection("testCol");

Tags tags = Tags.of("cluster.id", clusterId.get(), "server.address", String.format("%s:%s", host, port),
Expand All @@ -107,6 +110,7 @@ public void clusterOpening(ClusterOpeningEvent event) {
assertThat(registry.get("mongodb.driver.pool.waitqueuesize").gauge().value()).isZero();

mongo.close();
// end::example[]

assertThat(registry.find("mongodb.driver.pool.size").tags(tags).gauge())
.describedAs("metrics should be removed when the connection pool is closed")
Expand Down