Skip to content
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 @@ -26,6 +26,7 @@

import javax.net.ssl.SSLContext;

import io.micrometer.observation.ObservationRegistry;
import org.apache.commons.logging.Log;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
Expand All @@ -35,6 +36,7 @@
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.util.Timeout;

import org.springframework.boot.restclient.observation.ObservationRestTemplateCustomizer;
import org.springframework.cloud.configuration.SSLContextFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
Expand All @@ -44,6 +46,7 @@
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.observation.DefaultClientRequestObservationConvention;
import org.springframework.web.client.RestTemplate;

import static org.springframework.cloud.config.client.ConfigClientProperties.AUTHORIZATION;
Expand All @@ -54,9 +57,23 @@ public class ConfigClientRequestTemplateFactory {

private final ConfigClientProperties properties;

private final ObservationRestTemplateCustomizer observationRestTemplateCustomizer;

public ConfigClientRequestTemplateFactory(Log log, ConfigClientProperties properties) {
this.log = log;
this.properties = properties;
this.observationRestTemplateCustomizer = null;

}

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

public Log getLog() {
Expand All @@ -83,6 +100,10 @@ public RestTemplate create() {
template.setInterceptors(Arrays.asList(new GenericRequestHeaderInterceptor(headers)));
}

if (observationRestTemplateCustomizer != null) {
observationRestTemplateCustomizer.customize(template);
}

return template;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import io.micrometer.observation.ObservationRegistry;

import org.springframework.boot.bootstrap.BootstrapContext;
import org.springframework.boot.bootstrap.BootstrapRegistry;
import org.springframework.boot.bootstrap.BootstrapRegistry.InstanceSupplier;
Expand All @@ -35,6 +37,8 @@ public class ConfigServerBootstrapper implements BootstrapRegistryInitializer {

private LoaderInterceptor loaderInterceptor;

private ObservationRegistry observationRegistry;

static ConfigServerBootstrapper create() {
return new ConfigServerBootstrapper();
}
Expand All @@ -46,13 +50,22 @@ public ConfigServerBootstrapper withRestTemplateFactory(
return this;
}

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

public ConfigServerBootstrapper withLoaderInterceptor(LoaderInterceptor loaderInterceptor) {
this.loaderInterceptor = loaderInterceptor;
return this;
}

@Override
public void initialize(BootstrapRegistry registry) {

if (observationRegistry != null) {
registry.register(ObservationRegistry.class, InstanceSupplier.of(observationRegistry));
}
if (restTemplateFactory != null) {
registry.register(RestTemplate.class, restTemplateFactory::apply);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Properties;
import java.util.function.Supplier;

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

import org.springframework.boot.bootstrap.BootstrapRegistry;
Expand Down Expand Up @@ -241,7 +242,8 @@ public List<ConfigServerConfigDataResource> resolveProfileSpecific(
event.getBootstrapContext().get(ConfigClientProperties.class)));

bootstrapContext.registerIfAbsent(ConfigClientRequestTemplateFactory.class,
context -> new ConfigClientRequestTemplateFactory(log, context.get(ConfigClientProperties.class)));
context -> new ConfigClientRequestTemplateFactory(log, context.get(ConfigClientProperties.class),
context.getOrElse(ObservationRegistry.class, ObservationRegistry.NOOP)));

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,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.
Outdated
// 1. set up the factory
ConfigClientProperties properties = new ConfigClientProperties();
ObservationRegistry registry = ObservationRegistry.create();
ConfigClientRequestTemplateFactory factory = new ConfigClientRequestTemplateFactory(
LogFactory.getLog(getClass()), properties, registry);

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

// 3. verify the observation registry
assertThat(restTemplate.getObservationRegistry()).isEqualTo(registry);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be much better to verify if RestTemplate is indeed instrumented by:

  • Using ObservationRegistry
  • Calling RestTemplate
  • Verifying if Observations are created (see micrometer-observation-test)

}

@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.
Outdated
ConfigClientRequestTemplateFactory factory = new ConfigClientRequestTemplateFactory(
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,33 @@ void customizableRestTemplate() {
}
}

@Test
void customizableObservationRegistry() {
ConfigurableApplicationContext context = null;
try {
ObservationRegistry registry = ObservationRegistry.create();
context = new SpringApplicationBuilder(TestConfig.class)
.addBootstrapRegistryInitializer(ConfigServerBootstrapper.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