-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Improve configuration support for Observability integration #4216
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6276caf
mongo-micrometer - Prepare branch
gregturn 1560484
Introduce missing configuration settings for Spring Data MongoDB + Mi…
gregturn e0ac4a2
Polishing.
mp911de 9059f33
Update documentation.
mp911de bf1d9a2
Align conventions with OpenTelemetry spec.
mp911de File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...rc/main/java/org/springframework/data/mongodb/observability/EnableMongoObservability.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import java.lang.annotation.*; | ||
|
||
import org.springframework.context.annotation.Import; | ||
|
||
/** | ||
* Annotation to active Spring Data MongoDB's usage of Micrometer's Observation API. | ||
*/ | ||
@Inherited | ||
@Documented | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Import(MongoMetricsConfiguration.class) | ||
public @interface EnableMongoObservability { | ||
} |
21 changes: 21 additions & 0 deletions
21
...c/main/java/org/springframework/data/mongodb/observability/MongoMetricsConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.tracing.Tracer; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* Class to configure needed beans for MongoDB + Micrometer. | ||
*/ | ||
public class MongoMetricsConfiguration { | ||
|
||
@Bean | ||
MongoObservationCommandListener mongoObservationCommandListener(ObservationRegistry registry) { | ||
return new MongoObservationCommandListener(registry); | ||
} | ||
|
||
@Bean | ||
MongoTracingObservationHandler mongoTracingObservationHandler(Tracer tracer) { | ||
return new MongoTracingObservationHandler(tracer); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
.../java/org/springframework/data/mongodb/observability/MongoMetricsConfigurationHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.tracing.Tracer; | ||
|
||
import com.mongodb.client.SynchronousContextProvider; | ||
|
||
/** | ||
* Helper functions to ease registration of Spring Data MongoDB's observability. | ||
*/ | ||
public class MongoMetricsConfigurationHelper { | ||
|
||
public static SynchronousContextProvider synchronousContextProvider(Tracer tracer, ObservationRegistry registry) { | ||
return () -> new SynchronousTraceRequestContext(tracer).withObservation(Observation.start("name", registry)); | ||
} | ||
|
||
public static void addObservationHandler(ObservationRegistry registry, Tracer tracer) { | ||
registry.observationConfig().observationHandler(new MongoTracingObservationHandler(tracer)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...g/springframework/data/mongodb/observability/MongoMetricsReactiveConfigurationHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.util.context.Context; | ||
|
||
import com.mongodb.reactivestreams.client.ReactiveContextProvider; | ||
|
||
/** | ||
* Helper functions to ease registration of Spring Data MongoDB's observability. | ||
*/ | ||
public class MongoMetricsReactiveConfigurationHelper { | ||
|
||
public static ReactiveContextProvider reactiveContextProvider(ObservationRegistry registry) { | ||
return subscriber -> { | ||
if (subscriber instanceof CoreSubscriber<?> coreSubscriber) { | ||
return new ReactiveTraceRequestContext(coreSubscriber.currentContext()) | ||
.withObservation(Observation.start("name", registry)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
return new ReactiveTraceRequestContext(Context.empty()).withObservation(Observation.start("name", registry)); | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.tracing.Span; | ||
import io.micrometer.tracing.Tracer; | ||
import io.micrometer.tracing.handler.TracingObservationHandler; | ||
|
@@ -49,6 +50,10 @@ public MongoTracingObservationHandler(Tracer tracer) { | |
this.tracer = tracer; | ||
} | ||
|
||
public void register(ObservationRegistry observationRegistry) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shortcut method was purely created to slim down the registration statement users would have to add to their own code. |
||
observationRegistry.observationConfig().observationHandler(this); | ||
} | ||
|
||
@Override | ||
public Tracer getTracer() { | ||
return this.tracer; | ||
|
20 changes: 20 additions & 0 deletions
20
...main/java/org/springframework/data/mongodb/observability/ReactiveTraceRequestContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.observation.Observation; | ||
import reactor.util.context.ContextView; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
class ReactiveTraceRequestContext extends TraceRequestContext { | ||
|
||
ReactiveTraceRequestContext withObservation(Observation value) { | ||
|
||
put(Observation.class, value); | ||
return this; | ||
} | ||
|
||
ReactiveTraceRequestContext(ContextView context) { | ||
super(context.stream().collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue))); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...n/java/org/springframework/data/mongodb/observability/SynchronousTraceRequestContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.tracing.Span; | ||
import io.micrometer.tracing.TraceContext; | ||
import io.micrometer.tracing.Tracer; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
class SynchronousTraceRequestContext extends TraceRequestContext { | ||
|
||
SynchronousTraceRequestContext(Tracer tracer) { | ||
super(context(tracer)); | ||
} | ||
|
||
SynchronousTraceRequestContext withObservation(Observation value) { | ||
|
||
put(Observation.class, value); | ||
return this; | ||
} | ||
|
||
private static Map<Object, Object> context(Tracer tracer) { | ||
|
||
Map<Object, Object> map = new ConcurrentHashMap<>(); | ||
|
||
Span currentSpan = tracer.currentSpan(); | ||
|
||
if (currentSpan == null) { | ||
return map; | ||
} | ||
|
||
map.put(Span.class, currentSpan); | ||
map.put(TraceContext.class, currentSpan.context()); | ||
|
||
return map; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...odb/src/main/java/org/springframework/data/mongodb/observability/TraceRequestContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2013-2022 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.data.mongodb.observability; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import com.mongodb.RequestContext; | ||
|
||
/** | ||
* A {@link Map}-based {@link RequestContext}. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @author Greg Turnquist | ||
* @since 4.0.0 | ||
*/ | ||
class TraceRequestContext implements RequestContext { | ||
|
||
private final Map<Object, Object> map; | ||
|
||
public TraceRequestContext() { | ||
this(new HashMap<>()); | ||
} | ||
|
||
public TraceRequestContext(Map<Object, Object> context) { | ||
this.map = context; | ||
} | ||
|
||
@Override | ||
public <T> T get(Object key) { | ||
return (T) map.get(key); | ||
} | ||
|
||
@Override | ||
public boolean hasKey(Object key) { | ||
return map.containsKey(key); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return map.isEmpty(); | ||
} | ||
|
||
@Override | ||
public void put(Object key, Object value) { | ||
map.put(key, value); | ||
} | ||
|
||
@Override | ||
public void delete(Object key) { | ||
map.remove(key); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return map.size(); | ||
} | ||
|
||
@Override | ||
public Stream<Map.Entry<Object, Object>> stream() { | ||
return map.entrySet().stream(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't have an
Observation.start()
somewhere, then nothing seems to get logged in Zipkin. This is what I had used in the past inside the ZipkinIntegrationTests, but it feels quite arbitarary.If there is a better place to "start" things, I would like to know and could adjust things accordingly.