Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spring-cloud-config-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>spring-boot-starter-actuator</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-restclient</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aspectj</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ConfigServerBootstrapper implements BootstrapRegistryInitializer {

private LoaderInterceptor loaderInterceptor;

static ConfigServerBootstrapper create() {
public static ConfigServerBootstrapper create() {
return new ConfigServerBootstrapper();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,14 @@ public List<ConfigServerConfigDataResource> resolveProfileSpecific(
.registerSingleton("configDataConfigClientProperties",
event.getBootstrapContext().get(ConfigClientProperties.class)));

bootstrapContext.registerIfAbsent(ConfigClientRequestTemplateFactory.class,
context -> new ConfigClientRequestTemplateFactory(log, context.get(ConfigClientProperties.class)));
bootstrapContext.registerIfAbsent(ConfigClientRequestTemplateFactory.class, context -> {
ConfigClientProperties props = context.get(ConfigClientProperties.class);
if (ClassUtils
.isPresent("org.springframework.boot.restclient.observation.ObservationRestTemplateCustomizer", null)) {
return ObservationConfigClientRequestTemplateFactory.createWithObservation(context, log, props);
}
return new ConfigClientRequestTemplateFactory(log, props);
});

bootstrapContext.registerIfAbsent(RestTemplate.class, context -> {
ConfigClientRequestTemplateFactory factory = context.get(ConfigClientRequestTemplateFactory.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2026-present 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.cloud.config.client;

import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;

import org.springframework.boot.bootstrap.BootstrapContext;
import org.springframework.boot.restclient.observation.ObservationRestTemplateCustomizer;
import org.springframework.http.client.observation.DefaultClientRequestObservationConvention;
import org.springframework.web.client.RestTemplate;

public class ObservationConfigClientRequestTemplateFactory extends ConfigClientRequestTemplateFactory {

private final ObservationRestTemplateCustomizer observationRestTemplateCustomizer;

public ObservationConfigClientRequestTemplateFactory(Log log, ConfigClientProperties properties,
ObservationRegistry observationRegistry) {
super(log, properties);
this.observationRestTemplateCustomizer = observationRegistry != ObservationRegistry.NOOP
? new ObservationRestTemplateCustomizer(observationRegistry,
new DefaultClientRequestObservationConvention())
: null;
}

@Override
public RestTemplate create() {
RestTemplate template = super.create();
if (observationRestTemplateCustomizer != null) {
observationRestTemplateCustomizer.customize(template);
}
return template;
}

static ConfigClientRequestTemplateFactory createWithObservation(BootstrapContext context, Log log,
ConfigClientProperties props) {
ObservationRegistry registry = context.getOrElse(ObservationRegistry.class, ObservationRegistry.NOOP);
return new ObservationConfigClientRequestTemplateFactory(log, props, registry);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2026-present 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.cloud.config.client;

import io.micrometer.observation.ObservationRegistry;

import org.springframework.boot.bootstrap.BootstrapRegistry;

public class ObservationConfigServerBootstrapper extends ConfigServerBootstrapper {

private ObservationRegistry observationRegistry;

public static ObservationConfigServerBootstrapper create() {
return new ObservationConfigServerBootstrapper();
}

public ObservationConfigServerBootstrapper withObservationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
return this;
}

@Override
public void initialize(BootstrapRegistry registry) {
super.initialize(registry);
if (observationRegistry != null) {
registry.register(ObservationRegistry.class, BootstrapRegistry.InstanceSupplier.of(observationRegistry));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2026-present 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.cloud.config.client;

import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;

import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Luccas Asaphe
*
*/
public class ConfigClientRequestTemplateFactoryTests {

@Test
void shouldInstrumentRestTemplateWhenObservationRegistryProvided() {
Comment thread
LuccasAps marked this conversation as resolved.
// 1. set up the factory
ConfigClientProperties properties = new ConfigClientProperties();
ObservationRegistry registry = ObservationRegistry.create();
ObservationConfigClientRequestTemplateFactory factory = new ObservationConfigClientRequestTemplateFactory(
LogFactory.getLog(getClass()), properties, registry);

// 2. create the RestTemplate
RestTemplate restTemplate = factory.create();

// 3. verify the observation registry
assertThat(restTemplate.getObservationRegistry()).isEqualTo(registry);
}

@Test
void shouldNotInstrumentRestTemplateWhenObservationRegistryNotProvided() {
// 1. set up the factory
ConfigClientProperties properties = new ConfigClientProperties();
ConfigClientRequestTemplateFactory factory = new ConfigClientRequestTemplateFactory(
LogFactory.getLog(getClass()), properties);

// 2. create the RestTemplate
RestTemplate restTemplate = factory.create();

// 3. verify the observation registry
assertThat(restTemplate.getObservationRegistry()).isEqualTo(ObservationRegistry.NOOP);
}

@Test
void shouldNotInstrumentRestTemplateWhenObservationRegistryIsNoop() {
// 1. set up the factory
ConfigClientProperties properties = new ConfigClientProperties();
ObservationRegistry registry = ObservationRegistry.NOOP;
Comment thread
LuccasAps marked this conversation as resolved.
ObservationConfigClientRequestTemplateFactory factory = new ObservationConfigClientRequestTemplateFactory(
LogFactory.getLog(getClass()), properties, registry);

// 2. create the RestTemplate
RestTemplate restTemplate = factory.create();

// 3. verify the observation registry
assertThat(restTemplate.getObservationRegistry()).isEqualTo(ObservationRegistry.NOOP);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Optional;

import io.micrometer.observation.ObservationRegistry;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand Down Expand Up @@ -91,6 +92,34 @@ void customizableRestTemplate() {
}
}

@Test
void customizableObservationRegistry() {
ConfigurableApplicationContext context = null;
try {
ObservationRegistry registry = ObservationRegistry.create();
context = new SpringApplicationBuilder(TestConfig.class)
.addBootstrapRegistryInitializer(
ObservationConfigServerBootstrapper.create().withObservationRegistry(registry))
.addBootstrapRegistryInitializer(reg -> reg.addCloseListener(event -> {
BootstrapContext bootstrapContext = event.getBootstrapContext();
ConfigurableListableBeanFactory beanFactory = event.getApplicationContext().getBeanFactory();

RestTemplate restTemplate = bootstrapContext.get(RestTemplate.class);
beanFactory.registerSingleton("holder", new RestTemplateHolder(restTemplate));
}))
.run("--spring.config.import=optional:configserver:");

RestTemplateHolder holder = context.getBean(RestTemplateHolder.class);
assertThat(holder).isNotNull();
assertThat(holder.restTemplate.getObservationRegistry()).isEqualTo(registry);
}
finally {
if (context != null) {
context.close();
}
}
}

CustomRestTemplate restTemplate(BootstrapContext context) {
ConfigClientProperties properties = context.get(ConfigClientProperties.class);
String custom = context.get(Binder.class).bind("custom.prop", String.class).orElse("default-custom-prop");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2026-present 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.cloud.config.client;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.bootstrap.BootstrapContext;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.context.ConfigurableApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Luccas Asaphe
*
*/
@ClassPathExclusions({ "spring-boot-restclient-*.jar" })
public class ConfigServerConfigDataWithoutMicrometerTests {

@Test
void contextStartsWithoutMicrometer() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to verify the correct beans are created

try (ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class)
.web(WebApplicationType.NONE)
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
BootstrapContext bootstrapContext = event.getBootstrapContext();
ConfigurableListableBeanFactory beanFactory = event.getApplicationContext().getBeanFactory();

ConfigClientRequestTemplateFactory templateFactory = bootstrapContext
.get(ConfigClientRequestTemplateFactory.class);
beanFactory.registerSingleton("factory", templateFactory);
}))
.run("--spring.config.import=optional:configserver:")) {
assertThat(context).isNotNull();

assertThat(context.getBean("factory")).isNotInstanceOf(ObservationConfigClientRequestTemplateFactory.class);
}
}

@SpringBootConfiguration
@EnableAutoConfiguration
static class TestConfig {

}

}