Skip to content

Commit 702ea60

Browse files
christophstroblgregturn
authored andcommitted
Introduce Observability with Micrometer and Micrometer Tracing.
See #3942.
1 parent 664ffe5 commit 702ea60

File tree

12 files changed

+1042
-0
lines changed

12 files changed

+1042
-0
lines changed

spring-data-mongodb-distribution/pom.xml

+58
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
<properties>
2222
<project.root>${basedir}/..</project.root>
2323
<dist.key>SDMONGO</dist.key>
24+
25+
<!-- Observability -->
26+
<micrometer-docs-generator.version>1.0.0-M1</micrometer-docs-generator.version>
27+
<micrometer-docs-generator.inputPath>${maven.multiModuleProjectDirectory}/spring-data-mongodb/</micrometer-docs-generator.inputPath>
28+
<micrometer-docs-generator.inclusionPattern>.*</micrometer-docs-generator.inclusionPattern>
29+
<micrometer-docs-generator.outputPath>${maven.multiModuleProjectDirectory}/target/</micrometer-docs-generator.outputPath>
2430
</properties>
2531

2632
<build>
@@ -29,6 +35,58 @@
2935
<groupId>org.apache.maven.plugins</groupId>
3036
<artifactId>maven-assembly-plugin</artifactId>
3137
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<executions>
42+
<execution>
43+
<id>generate-metrics-metadata</id>
44+
<phase>prepare-package</phase>
45+
<goals>
46+
<goal>java</goal>
47+
</goals>
48+
<configuration>
49+
<mainClass>io.micrometer.docs.metrics.DocsFromSources</mainClass>
50+
</configuration>
51+
</execution>
52+
<execution>
53+
<id>generate-tracing-metadata</id>
54+
<phase>prepare-package</phase>
55+
<goals>
56+
<goal>java</goal>
57+
</goals>
58+
<configuration>
59+
<mainClass>io.micrometer.docs.spans.DocsFromSources</mainClass>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
<dependencies>
64+
<dependency>
65+
<groupId>io.micrometer
66+
</groupId>
67+
<artifactId>micrometer-docs-generator-spans</artifactId>
68+
<version>${micrometer-docs-generator.version}
69+
</version>
70+
<type>jar</type>
71+
</dependency>
72+
<dependency>
73+
<groupId>io.micrometer
74+
</groupId>
75+
<artifactId>micrometer-docs-generator-metrics</artifactId>
76+
<version>${micrometer-docs-generator.version}
77+
</version>
78+
<type>jar</type>
79+
</dependency>
80+
</dependencies>
81+
<configuration>
82+
<includePluginDependencies>true</includePluginDependencies>
83+
<arguments>
84+
<argument>${micrometer-docs-generator.inputPath}</argument>
85+
<argument>${micrometer-docs-generator.inclusionPattern}</argument>
86+
<argument>${micrometer-docs-generator.outputPath}</argument>
87+
</arguments>
88+
</configuration>
89+
</plugin>
3290
<plugin>
3391
<groupId>org.asciidoctor</groupId>
3492
<artifactId>asciidoctor-maven-plugin</artifactId>

spring-data-mongodb/pom.xml

+17
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@
193193
<optional>true</optional>
194194
</dependency>
195195

196+
<dependency>
197+
<groupId>io.micrometer</groupId>
198+
<artifactId>micrometer-tracing-api</artifactId>
199+
<optional>true</optional>
200+
</dependency>
201+
196202
<dependency>
197203
<groupId>org.hibernate</groupId>
198204
<artifactId>hibernate-validator</artifactId>
@@ -302,6 +308,17 @@
302308
<scope>test</scope>
303309
</dependency>
304310

311+
<dependency>
312+
<groupId>io.micrometer</groupId>
313+
<artifactId>micrometer-test</artifactId>
314+
<scope>test</scope>
315+
</dependency>
316+
<dependency>
317+
<groupId>io.micrometer</groupId>
318+
<artifactId>micrometer-tracing-test</artifactId>
319+
<scope>test</scope>
320+
</dependency>
321+
305322
<!-- jMolecules -->
306323

307324
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.observability;
17+
18+
import io.micrometer.api.instrument.observation.Observation;
19+
20+
import com.mongodb.RequestContext;
21+
import com.mongodb.event.CommandFailedEvent;
22+
import com.mongodb.event.CommandStartedEvent;
23+
import com.mongodb.event.CommandSucceededEvent;
24+
25+
/**
26+
* A {@link Observation.Context} that contains MongoDB events.
27+
*
28+
* @author Marcin Grzejszczak
29+
* @author Greg Turnquist
30+
* @since 4.0.0
31+
*/
32+
public class MongoHandlerContext extends Observation.Context {
33+
34+
private final CommandStartedEvent commandStartedEvent;
35+
private final RequestContext requestContext;
36+
37+
private CommandSucceededEvent commandSucceededEvent;
38+
private CommandFailedEvent commandFailedEvent;
39+
40+
public MongoHandlerContext(CommandStartedEvent commandStartedEvent, RequestContext requestContext) {
41+
42+
this.commandStartedEvent = commandStartedEvent;
43+
this.requestContext = requestContext;
44+
}
45+
46+
public CommandStartedEvent getCommandStartedEvent() {
47+
return this.commandStartedEvent;
48+
}
49+
50+
public RequestContext getRequestContext() {
51+
return requestContext;
52+
}
53+
54+
public CommandSucceededEvent getCommandSucceededEvent() {
55+
return this.commandSucceededEvent;
56+
}
57+
58+
public void setCommandSucceededEvent(CommandSucceededEvent commandSucceededEvent) {
59+
this.commandSucceededEvent = commandSucceededEvent;
60+
}
61+
62+
public CommandFailedEvent getCommandFailedEvent() {
63+
return this.commandFailedEvent;
64+
}
65+
66+
public void setCommandFailedEvent(CommandFailedEvent commandFailedEvent) {
67+
this.commandFailedEvent = commandFailedEvent;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.observability;
17+
18+
import io.micrometer.api.instrument.docs.DocumentedObservation;
19+
import io.micrometer.api.instrument.docs.TagKey;
20+
21+
/**
22+
* A MongoDB-based {@link io.micrometer.api.instrument.observation.Observation}.
23+
*
24+
* @author Marcin Grzejszczak
25+
* @author Greg Turnquist
26+
* @since 1.0.0
27+
*/
28+
enum MongoObservation implements DocumentedObservation {
29+
30+
/**
31+
* Timer created around a MongoDB command execution.
32+
*/
33+
MONGODB_COMMAND_OBSERVATION {
34+
35+
@Override
36+
public String getName() {
37+
return "mongodb.command";
38+
}
39+
40+
@Override
41+
public TagKey[] getLowCardinalityTagKeys() {
42+
return LowCardinalityCommandTags.values();
43+
}
44+
45+
@Override
46+
public TagKey[] getHighCardinalityTagKeys() {
47+
return HighCardinalityCommandTags.values();
48+
}
49+
50+
@Override
51+
public String getPrefix() {
52+
return "mongodb";
53+
}
54+
};
55+
56+
/**
57+
* Enums related to low cardinality tags for MongoDB commands.
58+
*/
59+
enum LowCardinalityCommandTags implements TagKey {
60+
61+
/**
62+
* MongoDB collection name.
63+
*/
64+
MONGODB_COLLECTION {
65+
@Override
66+
public String getKey() {
67+
return "mongodb.collection";
68+
}
69+
},
70+
71+
/**
72+
* MongoDB cluster identifier.
73+
*/
74+
MONGODB_CLUSTER_ID {
75+
@Override
76+
public String getKey() {
77+
return "mongodb.cluster_id";
78+
}
79+
}
80+
}
81+
82+
/**
83+
* Enums related to high cardinality tags for MongoDB commands.
84+
*/
85+
enum HighCardinalityCommandTags implements TagKey {
86+
87+
/**
88+
* MongoDB command value.
89+
*/
90+
MONGODB_COMMAND {
91+
@Override
92+
public String getKey() {
93+
return "mongodb.command";
94+
}
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)