Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See: #4216
- Loading branch information
Showing
22 changed files
with
590 additions
and
602 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
.../src/main/java/org/springframework/data/mongodb/observability/ContextProviderFactory.java
This file contains 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,139 @@ | ||
/* | ||
* Copyright 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.Map; | ||
import java.util.Map.Entry; | ||
import java.util.stream.Collectors; | ||
|
||
import org.reactivestreams.Subscriber; | ||
import org.springframework.data.repository.util.ReactiveWrappers; | ||
import org.springframework.data.repository.util.ReactiveWrappers.ReactiveLibrary; | ||
import org.springframework.util.ClassUtils; | ||
|
||
import com.mongodb.ContextProvider; | ||
import com.mongodb.RequestContext; | ||
import com.mongodb.client.SynchronousContextProvider; | ||
import com.mongodb.reactivestreams.client.ReactiveContextProvider; | ||
|
||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; | ||
import reactor.core.CoreSubscriber; | ||
|
||
/** | ||
* Factory to create a {@link ContextProvider} to propagate the request context across tasks. Requires either | ||
* {@link SynchronousContextProvider} or {@link ReactiveContextProvider} to be present. | ||
* | ||
* @author Mark Paluch | ||
* @since 3.0 | ||
*/ | ||
public class ContextProviderFactory { | ||
|
||
private static final boolean SYNCHRONOUS_PRESENT = ClassUtils | ||
.isPresent("com.mongodb.client.SynchronousContextProvider", ContextProviderFactory.class.getClassLoader()); | ||
|
||
private static final boolean REACTIVE_PRESENT = ClassUtils.isPresent( | ||
"com.mongodb.reactivestreams.client.ReactiveContextProvider", ContextProviderFactory.class.getClassLoader()) | ||
&& ReactiveWrappers.isAvailable(ReactiveLibrary.PROJECT_REACTOR); | ||
|
||
/** | ||
* Create a {@link ContextProvider} given {@link ObservationRegistry}. The factory method attempts to create a | ||
* {@link ContextProvider} that is capable to propagate request contexts across imperative or reactive usage, | ||
* depending on their class path presence. | ||
* | ||
* @param observationRegistry must not be {@literal null}. | ||
* @return | ||
*/ | ||
public static ContextProvider create(ObservationRegistry observationRegistry) { | ||
|
||
if (SYNCHRONOUS_PRESENT && REACTIVE_PRESENT) { | ||
return new CompositeContextProvider(observationRegistry); | ||
} | ||
|
||
if (SYNCHRONOUS_PRESENT) { | ||
return new DefaultSynchronousContextProvider(observationRegistry); | ||
} | ||
|
||
if (REACTIVE_PRESENT) { | ||
return DefaultReactiveContextProvider.INSTANCE; | ||
} | ||
|
||
throw new IllegalStateException( | ||
"Cannot create ContextProvider. Neither SynchronousContextProvider nor ReactiveContextProvider is on the class path."); | ||
} | ||
|
||
record DefaultSynchronousContextProvider( | ||
ObservationRegistry observationRegistry) implements SynchronousContextProvider { | ||
|
||
@Override | ||
public RequestContext getContext() { | ||
|
||
MapRequestContext requestContext = new MapRequestContext(); | ||
|
||
Observation currentObservation = observationRegistry.getCurrentObservation(); | ||
if (currentObservation != null) { | ||
requestContext.put(Observation.class, currentObservation); | ||
} | ||
|
||
return requestContext; | ||
} | ||
|
||
} | ||
|
||
enum DefaultReactiveContextProvider implements ReactiveContextProvider { | ||
|
||
INSTANCE; | ||
|
||
@Override | ||
public RequestContext getContext(Subscriber<?> subscriber) { | ||
|
||
if (subscriber instanceof CoreSubscriber<?> cs) { | ||
|
||
Map<Object, Object> map = cs.currentContext().stream() | ||
.collect(Collectors.toConcurrentMap(Entry::getKey, Entry::getValue)); | ||
if (map.containsKey(ObservationThreadLocalAccessor.KEY)) { | ||
map.put(Observation.class, map.get(ObservationThreadLocalAccessor.KEY)); | ||
} | ||
|
||
return new MapRequestContext(map); | ||
} | ||
|
||
return new MapRequestContext(); | ||
} | ||
} | ||
|
||
record CompositeContextProvider(DefaultSynchronousContextProvider synchronousContextProvider) | ||
implements | ||
SynchronousContextProvider, | ||
ReactiveContextProvider { | ||
|
||
CompositeContextProvider(ObservationRegistry observationRegistry) { | ||
this(new DefaultSynchronousContextProvider(observationRegistry)); | ||
} | ||
|
||
@Override | ||
public RequestContext getContext() { | ||
return synchronousContextProvider.getContext(); | ||
} | ||
|
||
@Override | ||
public RequestContext getContext(Subscriber<?> subscriber) { | ||
return DefaultReactiveContextProvider.INSTANCE.getContext(subscriber); | ||
} | ||
} | ||
|
||
} |
This file contains 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: 0 additions & 16 deletions
16
...rc/main/java/org/springframework/data/mongodb/observability/EnableMongoObservability.java
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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
20 changes: 10 additions & 10 deletions
20
...c/main/java/org/springframework/data/mongodb/observability/MongoMetricsConfiguration.java
This file contains 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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package org.springframework.data.mongodb.observability; | ||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.tracing.Tracer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
|
||
/** | ||
* Class to configure needed beans for MongoDB + Micrometer. | ||
* | ||
* @since 3.0 | ||
*/ | ||
@Configuration | ||
public class MongoMetricsConfiguration { | ||
|
||
@Bean | ||
MongoObservationCommandListener mongoObservationCommandListener(ObservationRegistry registry) { | ||
return new MongoObservationCommandListener(registry); | ||
} | ||
@Bean | ||
public MongoObservationCommandListener mongoObservationCommandListener(ObservationRegistry registry) { | ||
return new MongoObservationCommandListener(registry); | ||
} | ||
|
||
@Bean | ||
MongoTracingObservationHandler mongoTracingObservationHandler(Tracer tracer) { | ||
return new MongoTracingObservationHandler(tracer); | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
.../java/org/springframework/data/mongodb/observability/MongoMetricsConfigurationHelper.java
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
...g/springframework/data/mongodb/observability/MongoMetricsReactiveConfigurationHelper.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.