Skip to content

Commit 4149ed5

Browse files
NotBjoggisAtAlldsyer
authored andcommitted
Add autoconfiguration for client side observation interceptor
[resolves #51] Signed-off-by: Jonas Bjørge Andersen <[email protected]>
1 parent a21748a commit 4149ed5

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

spring-grpc-docs/src/main/antora/modules/ROOT/pages/client.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ However, by setting the `mergeWithGlobalInterceptors` parameter on the channel f
131131
You can use this option if you want to add a per-client interceptor between global interceptors.
132132

133133
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.
134+
135+
== Observability
136+
137+
Spring gRPC provides an autoconfigured interceptor that can be used to provide observability to your gRPC clients.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2024-2024 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+
package org.springframework.grpc.autoconfigure.client;
17+
18+
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
19+
import io.micrometer.observation.ObservationRegistry;
20+
import org.springframework.boot.autoconfigure.AutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.grpc.client.GlobalClientInterceptor;
26+
27+
@AutoConfiguration(
28+
afterName = "org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration")
29+
@ConditionalOnClass({ ObservationRegistry.class, ObservationGrpcClientInterceptor.class })
30+
@ConditionalOnBean(ObservationRegistry.class)
31+
@ConditionalOnProperty(name = "spring.grpc.client.observation.enabled", havingValue = "true", matchIfMissing = true)
32+
33+
public class GrpcClientObservationAutoConfiguration {
34+
35+
@Bean
36+
@GlobalClientInterceptor
37+
ObservationGrpcClientInterceptor observationGrpcClientInterceptor(ObservationRegistry observationRegistry) {
38+
return new ObservationGrpcClientInterceptor(observationRegistry);
39+
}
40+
41+
}

spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
"type": "java.lang.Boolean",
2323
"description": "Whether to enable Observations on the server.",
2424
"defaultValue": true
25-
25+
},
26+
{
27+
"name": "spring.grpc.client.observations.enabled",
28+
"type": "java.lang.Boolean",
29+
"description": "Whether to enable Observations on the client.",
30+
"defaultValue": true
2631
}
2732
]
2833
}

spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
org.springframework.grpc.autoconfigure.client.GrpcClientAutoConfiguration
2+
org.springframework.grpc.autoconfigure.client.GrpcClientObservationAutoConfiguration
23
org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration
34
org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration
45
org.springframework.grpc.autoconfigure.server.health.GrpcServerHealthAutoConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2024-2024 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.grpc.autoconfigure.client;
18+
19+
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
20+
import io.micrometer.observation.ObservationRegistry;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.Mockito;
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.test.context.FilteredClassLoader;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.grpc.client.GlobalClientInterceptor;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for the {@link GrpcClientObservationAutoConfiguration}.
32+
*/
33+
class GrpcClientObservationAutoConfigurationTests {
34+
35+
private final ApplicationContextRunner baseContextRunner = new ApplicationContextRunner()
36+
.withConfiguration(AutoConfigurations.of(GrpcClientObservationAutoConfiguration.class));
37+
38+
private ApplicationContextRunner validContextRunner() {
39+
return new ApplicationContextRunner()
40+
.withConfiguration(AutoConfigurations.of(GrpcClientObservationAutoConfiguration.class))
41+
.withBean("observationRegistry", ObservationRegistry.class, Mockito::mock);
42+
}
43+
44+
@Test
45+
void whenObservationRegistryNotOnClasspathAutoConfigSkipped() {
46+
this.validContextRunner()
47+
.withClassLoader(new FilteredClassLoader(ObservationRegistry.class))
48+
.run((context) -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
49+
}
50+
51+
@Test
52+
void whenObservationGrpcClientInterceptorNotOnClasspathAutoConfigSkipped() {
53+
this.validContextRunner()
54+
.withClassLoader(new FilteredClassLoader(ObservationGrpcClientInterceptor.class))
55+
.run((context) -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
56+
}
57+
58+
@Test
59+
void whenObservationRegistryNotProvidedThenAutoConfigSkipped() {
60+
this.baseContextRunner
61+
.run(context -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
62+
}
63+
64+
@Test
65+
void whenObservationPropertyEnabledThenAutoConfigNotSkipped() {
66+
this.validContextRunner()
67+
.withPropertyValues("spring.grpc.client.observation.enabled=true")
68+
.run(context -> assertThat(context).hasSingleBean(GrpcClientObservationAutoConfiguration.class));
69+
}
70+
71+
@Test
72+
void whenObservationPropertyDisabledThenAutoConfigIsSkipped() {
73+
this.validContextRunner()
74+
.withPropertyValues("spring.grpc.client.observation.enabled=false")
75+
.run(context -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
76+
}
77+
78+
@Test
79+
void whenAllConditionsAreMetThenInterceptorConfiguredAsExpected() {
80+
this.validContextRunner().run((context) -> {
81+
assertThat(context).hasSingleBean(ObservationGrpcClientInterceptor.class);
82+
assertThat(context.getBeansWithAnnotation(GlobalClientInterceptor.class)).hasEntrySatisfying(
83+
"observationGrpcClientInterceptor",
84+
bean -> assertThat(bean.getClass().isAssignableFrom(ObservationGrpcClientInterceptor.class))
85+
.isTrue());
86+
});
87+
}
88+
89+
}

0 commit comments

Comments
 (0)