Skip to content

Commit 6d95099

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

File tree

17 files changed

+1423
-1
lines changed

17 files changed

+1423
-1
lines changed

ci/openjdk17-mongodb-5.0/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ RUN apt-get update; \
2121
apt-get install -y mongodb-org=${MONGODB} mongodb-org-server=${MONGODB} mongodb-org-shell=${MONGODB} mongodb-org-mongos=${MONGODB} mongodb-org-tools=${MONGODB} ; \
2222
apt-get clean; \
2323
rm -rf /var/lib/apt/lists/*;
24+

pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
</modules>
2525

2626
<properties>
27-
<source.level>16</source.level>
2827
<project.type>multi</project.type>
2928
<dist.id>spring-data-mongodb</dist.id>
3029
<springdata.commons>3.0.0-SNAPSHOT</springdata.commons>

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,104 @@
1+
/*
2+
* Copyright 2015-2021 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.core.aggregation;
17+
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
import org.bson.Document;
23+
import org.springframework.util.Assert;
24+
25+
/**
26+
* An enum of supported {@link AggregationExpression}s in aggregation pipeline stages.
27+
*
28+
* @author Thomas Darimont
29+
* @author Oliver Gierke
30+
* @author Christoph Strobl
31+
* @author Mark Paluch
32+
* @since 1.7
33+
* @deprecated since 1.10. Please use {@link ArithmeticOperators} and {@link ComparisonOperators} instead.
34+
*/
35+
@Deprecated
36+
public enum AggregationFunctionExpressions {
37+
38+
SIZE, CMP, EQ, GT, GTE, LT, LTE, NE, SUBTRACT, ADD, MULTIPLY;
39+
40+
/**
41+
* Returns an {@link AggregationExpression} build from the current {@link Enum} name and the given parameters.
42+
*
43+
* @param parameters must not be {@literal null}
44+
* @return new instance of {@link AggregationExpression}.
45+
*/
46+
public AggregationExpression of(Object... parameters) {
47+
48+
Assert.notNull(parameters, "Parameters must not be null!");
49+
return new FunctionExpression(name().toLowerCase(), parameters);
50+
}
51+
52+
/**
53+
* An {@link AggregationExpression} representing a function call.
54+
*
55+
* @author Thomas Darimont
56+
* @author Oliver Gierke
57+
* @since 1.7
58+
*/
59+
static class FunctionExpression implements AggregationExpression {
60+
61+
private final String name;
62+
private final List<Object> values;
63+
64+
/**
65+
* Creates a new {@link FunctionExpression} for the given name and values.
66+
*
67+
* @param name must not be {@literal null} or empty.
68+
* @param values must not be {@literal null}.
69+
*/
70+
public FunctionExpression(String name, Object[] values) {
71+
72+
Assert.hasText(name, "Name must not be null!");
73+
Assert.notNull(values, "Values must not be null!");
74+
75+
this.name = name;
76+
this.values = Arrays.asList(values);
77+
}
78+
79+
@Override
80+
public Document toDocument(AggregationOperationContext context) {
81+
82+
List<Object> args = new ArrayList<Object>(values.size());
83+
84+
for (Object value : values) {
85+
args.add(unpack(value, context));
86+
}
87+
88+
return new Document("$" + name, args);
89+
}
90+
91+
private static Object unpack(Object value, AggregationOperationContext context) {
92+
93+
if (value instanceof AggregationExpression) {
94+
return ((AggregationExpression) value).toDocument(context);
95+
}
96+
97+
if (value instanceof Field) {
98+
return context.getReference((Field) value).toString();
99+
}
100+
101+
return value;
102+
}
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2012-2021 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.core.mapping.event;
17+
18+
import org.springframework.beans.factory.ObjectFactory;
19+
import org.springframework.context.ApplicationListener;
20+
import org.springframework.core.Ordered;
21+
import org.springframework.data.auditing.AuditingHandler;
22+
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
23+
import org.springframework.data.mapping.context.MappingContext;
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* Event listener to populate auditing related fields on an entity about to be saved.
28+
*
29+
* @author Oliver Gierke
30+
* @author Thomas Darimont
31+
* @deprecated since 2.2, use {@link AuditingEntityCallback}.
32+
*/
33+
@Deprecated
34+
public class AuditingEventListener implements ApplicationListener<BeforeConvertEvent<Object>>, Ordered {
35+
36+
private final ObjectFactory<IsNewAwareAuditingHandler> auditingHandlerFactory;
37+
38+
/**
39+
* Creates a new {@link AuditingEventListener} using the given {@link MappingContext} and {@link AuditingHandler}
40+
* provided by the given {@link ObjectFactory}.
41+
*
42+
* @param auditingHandlerFactory must not be {@literal null}.
43+
*/
44+
public AuditingEventListener(ObjectFactory<IsNewAwareAuditingHandler> auditingHandlerFactory) {
45+
46+
Assert.notNull(auditingHandlerFactory, "IsNewAwareAuditingHandler must not be null!");
47+
this.auditingHandlerFactory = auditingHandlerFactory;
48+
}
49+
50+
@Override
51+
public void onApplicationEvent(BeforeConvertEvent<Object> event) {
52+
event.mapSource(it -> auditingHandlerFactory.getObject().markAudited(it));
53+
}
54+
55+
@Override
56+
public int getOrder() {
57+
return 100;
58+
}
59+
}
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+
}

0 commit comments

Comments
 (0)