Skip to content

Commit 848831d

Browse files
committed
Add ObservationContextSource
Closes gh-991
1 parent 84827bd commit 848831d

File tree

6 files changed

+924
-0
lines changed

6 files changed

+924
-0
lines changed

core/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies {
1717
api "org.springframework:spring-core"
1818
api "org.springframework:spring-beans"
1919
api "org.springframework:spring-tx"
20+
api "io.micrometer:micrometer-core"
2021

2122
implementation "org.slf4j:slf4j-api"
2223

@@ -31,6 +32,10 @@ dependencies {
3132
optional "commons-pool:commons-pool"
3233
optional "org.apache.commons:commons-pool2"
3334

35+
testImplementation ("io.micrometer:micrometer-test") {
36+
exclude group: 'org.mockito'
37+
exclude group: 'org.assertj'
38+
}
3439
testImplementation platform('org.junit:junit-bom')
3540
testImplementation "org.junit.vintage:junit-vintage-engine"
3641

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2025 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+
17+
package org.springframework.ldap.core.support;
18+
19+
import io.micrometer.observation.ObservationRegistry;
20+
21+
import org.springframework.beans.BeansException;
22+
import org.springframework.beans.factory.ObjectProvider;
23+
import org.springframework.beans.factory.config.BeanPostProcessor;
24+
import org.springframework.ldap.core.ContextSource;
25+
26+
/**
27+
* A {@link BeanPostProcessor} that makes any {@link ContextSource} bean observable by
28+
* Micrometer
29+
*
30+
* @author Josh Cummings
31+
* @since 3.3
32+
*/
33+
public final class ContextSourceObservationPostProcessor implements BeanPostProcessor {
34+
35+
private final ObjectProvider<ObservationRegistry> observationRegistry;
36+
37+
public ContextSourceObservationPostProcessor(ObjectProvider<ObservationRegistry> observationRegistry) {
38+
this.observationRegistry = observationRegistry;
39+
}
40+
41+
@Override
42+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
43+
if (bean instanceof ObservationContextSource) {
44+
return bean;
45+
}
46+
if (!(bean instanceof BaseLdapPathContextSource ldap)) {
47+
return bean;
48+
}
49+
ObservationRegistry observationRegistry = this.observationRegistry
50+
.getIfAvailable(() -> ObservationRegistry.NOOP);
51+
if (observationRegistry.isNoop()) {
52+
return bean;
53+
}
54+
return new ObservationContextSource(ldap, observationRegistry);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)