Add autoconfiguration for client side observation interceptor
[resolves #51] Signed-off-by: Jonas Bjørge Andersen <dev@jonasandersen.no>
This commit is contained in:
committed by
Dave Syer
parent
a21748aba2
commit
4149ed552e
@@ -131,3 +131,7 @@ However, by setting the `mergeWithGlobalInterceptors` parameter on the channel f
|
||||
You can use this option if you want to add a per-client interceptor between global interceptors.
|
||||
|
||||
IMPORTANT: The per-channel interceptors you pass in must either be bean instances marked with `@Order` or regular objects that implement the `Ordered` interface to be properly merged/ordered with the global interceptors.
|
||||
|
||||
== Observability
|
||||
|
||||
Spring gRPC provides an autoconfigured interceptor that can be used to provide observability to your gRPC clients.
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2024-2024 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.grpc.autoconfigure.client;
|
||||
|
||||
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.grpc.client.GlobalClientInterceptor;
|
||||
|
||||
@AutoConfiguration(
|
||||
afterName = "org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration")
|
||||
@ConditionalOnClass({ ObservationRegistry.class, ObservationGrpcClientInterceptor.class })
|
||||
@ConditionalOnBean(ObservationRegistry.class)
|
||||
@ConditionalOnProperty(name = "spring.grpc.client.observation.enabled", havingValue = "true", matchIfMissing = true)
|
||||
|
||||
public class GrpcClientObservationAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@GlobalClientInterceptor
|
||||
ObservationGrpcClientInterceptor observationGrpcClientInterceptor(ObservationRegistry observationRegistry) {
|
||||
return new ObservationGrpcClientInterceptor(observationRegistry);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,7 +22,12 @@
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether to enable Observations on the server.",
|
||||
"defaultValue": true
|
||||
|
||||
},
|
||||
{
|
||||
"name": "spring.grpc.client.observations.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Whether to enable Observations on the client.",
|
||||
"defaultValue": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
org.springframework.grpc.autoconfigure.client.GrpcClientAutoConfiguration
|
||||
org.springframework.grpc.autoconfigure.client.GrpcClientObservationAutoConfiguration
|
||||
org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration
|
||||
org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration
|
||||
org.springframework.grpc.autoconfigure.server.health.GrpcServerHealthAutoConfiguration
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright 2024-2024 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.grpc.autoconfigure.client;
|
||||
|
||||
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
|
||||
import io.micrometer.observation.ObservationRegistry;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.grpc.client.GlobalClientInterceptor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for the {@link GrpcClientObservationAutoConfiguration}.
|
||||
*/
|
||||
class GrpcClientObservationAutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner baseContextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(GrpcClientObservationAutoConfiguration.class));
|
||||
|
||||
private ApplicationContextRunner validContextRunner() {
|
||||
return new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(GrpcClientObservationAutoConfiguration.class))
|
||||
.withBean("observationRegistry", ObservationRegistry.class, Mockito::mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenObservationRegistryNotOnClasspathAutoConfigSkipped() {
|
||||
this.validContextRunner()
|
||||
.withClassLoader(new FilteredClassLoader(ObservationRegistry.class))
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenObservationGrpcClientInterceptorNotOnClasspathAutoConfigSkipped() {
|
||||
this.validContextRunner()
|
||||
.withClassLoader(new FilteredClassLoader(ObservationGrpcClientInterceptor.class))
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenObservationRegistryNotProvidedThenAutoConfigSkipped() {
|
||||
this.baseContextRunner
|
||||
.run(context -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenObservationPropertyEnabledThenAutoConfigNotSkipped() {
|
||||
this.validContextRunner()
|
||||
.withPropertyValues("spring.grpc.client.observation.enabled=true")
|
||||
.run(context -> assertThat(context).hasSingleBean(GrpcClientObservationAutoConfiguration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenObservationPropertyDisabledThenAutoConfigIsSkipped() {
|
||||
this.validContextRunner()
|
||||
.withPropertyValues("spring.grpc.client.observation.enabled=false")
|
||||
.run(context -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAllConditionsAreMetThenInterceptorConfiguredAsExpected() {
|
||||
this.validContextRunner().run((context) -> {
|
||||
assertThat(context).hasSingleBean(ObservationGrpcClientInterceptor.class);
|
||||
assertThat(context.getBeansWithAnnotation(GlobalClientInterceptor.class)).hasEntrySatisfying(
|
||||
"observationGrpcClientInterceptor",
|
||||
bean -> assertThat(bean.getClass().isAssignableFrom(ObservationGrpcClientInterceptor.class))
|
||||
.isTrue());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user