Skip to content

Commit

Permalink
Add ObservationContextSource
Browse files Browse the repository at this point in the history
Closes gh-991
  • Loading branch information
jzheaux committed Jan 7, 2025
1 parent 84827bd commit 848831d
Show file tree
Hide file tree
Showing 6 changed files with 924 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
api "org.springframework:spring-core"
api "org.springframework:spring-beans"
api "org.springframework:spring-tx"
api "io.micrometer:micrometer-core"

implementation "org.slf4j:slf4j-api"

Expand All @@ -31,6 +32,10 @@ dependencies {
optional "commons-pool:commons-pool"
optional "org.apache.commons:commons-pool2"

testImplementation ("io.micrometer:micrometer-test") {
exclude group: 'org.mockito'
exclude group: 'org.assertj'
}
testImplementation platform('org.junit:junit-bom')
testImplementation "org.junit.vintage:junit-vintage-engine"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2002-2025 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.ldap.core.support;

import io.micrometer.observation.ObservationRegistry;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.ldap.core.ContextSource;

/**
* A {@link BeanPostProcessor} that makes any {@link ContextSource} bean observable by
* Micrometer
*
* @author Josh Cummings
* @since 3.3
*/
public final class ContextSourceObservationPostProcessor implements BeanPostProcessor {

private final ObjectProvider<ObservationRegistry> observationRegistry;

public ContextSourceObservationPostProcessor(ObjectProvider<ObservationRegistry> observationRegistry) {
this.observationRegistry = observationRegistry;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof ObservationContextSource) {
return bean;
}
if (!(bean instanceof BaseLdapPathContextSource ldap)) {
return bean;
}
ObservationRegistry observationRegistry = this.observationRegistry
.getIfAvailable(() -> ObservationRegistry.NOOP);
if (observationRegistry.isNoop()) {
return bean;
}
return new ObservationContextSource(ldap, observationRegistry);
}

}
Loading

0 comments on commit 848831d

Please sign in to comment.