Skip to content

Commit 1026133

Browse files
author
Dave Syer
committed
Add autoconfiguration for Micrometer tracing
This will activate in a Spring Boot 3 application if there is a dependency on one of the micrometer bridge jars, e.g. io.micrometer:micrometer-tracing-bridge-{otel,brave}.
1 parent 9f45492 commit 1026133

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,11 @@ allprojects { project ->
186186
}
187187
}
188188

189+
ext['micrometer.version'] = '1.10.3'
190+
189191
ext {
190-
micrometerVersion = '1.10.3'
191192
// not explicitly needed in subprojects, as the BOM for Spring Boot sets this version
193+
micrometerVersion = dependencyManagement.importedProperties['micrometer.version']
192194
springFrameworkVersion = dependencyManagement.importedProperties['spring-framework.version']
193195
springSecurityVersion = dependencyManagement.importedProperties['spring-security.version']
194196
springCloudCommonsVersion = dependencyManagement.importedProperties['spring-cloud-commons.version']

grpc-client-spring-boot-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
api project(':grpc-common-spring-boot')
1717
api 'org.springframework.boot:spring-boot-starter'
1818
api 'jakarta.validation:jakarta.validation-api'
19+
optionalSupportImplementation "io.micrometer:micrometer-observation"
1920
optionalSupportImplementation 'org.springframework.boot:spring-boot-starter-actuator'
2021
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
2122
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2016-2022 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.client.autoconfigure;
19+
20+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.core.annotation.Order;
25+
26+
import io.grpc.ClientInterceptor;
27+
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
28+
import io.micrometer.observation.ObservationRegistry;
29+
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
30+
import net.devh.boot.grpc.common.autoconfigure.GrpcCommonTraceAutoConfiguration;
31+
import net.devh.boot.grpc.common.util.InterceptorOrder;
32+
33+
/**
34+
* The configuration used to configure micrometer tracing for grpc.
35+
*
36+
* @author Dave Syer ([email protected])
37+
*/
38+
@Configuration(proxyBeanMethods = false)
39+
@ConditionalOnProperty(value = "management.tracing.grpc.enabled", matchIfMissing = true)
40+
@AutoConfigureAfter(value = GrpcCommonTraceAutoConfiguration.class,
41+
name = "org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration")
42+
@ConditionalOnBean(ObservationRegistry.class)
43+
public class GrpcClientMicrometerTraceAutoConfiguration {
44+
45+
/**
46+
* Configures a global client interceptor that applies micrometer tracing logic to the requests.
47+
*
48+
* @param observations The observation registry bean.
49+
* @return The tracing client interceptor bean.
50+
*/
51+
@GrpcGlobalClientInterceptor
52+
@Order(InterceptorOrder.ORDER_TRACING_METRICS + 1)
53+
ClientInterceptor globalObservationClientInterceptorConfigurer(final ObservationRegistry observations) {
54+
return new ObservationGrpcClientInterceptor(observations);
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration
22
net.devh.boot.grpc.client.autoconfigure.GrpcClientMetricAutoConfiguration
33
net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration
4+
net.devh.boot.grpc.client.autoconfigure.GrpcClientMicrometerTraceAutoConfiguration
45
net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration
56
net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration

grpc-server-spring-boot-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515

1616
api project(':grpc-common-spring-boot')
1717
api 'org.springframework.boot:spring-boot-starter'
18+
optionalSupportImplementation "io.micrometer:micrometer-observation"
1819
optionalSupportImplementation 'org.springframework.boot:spring-boot-starter-actuator'
1920
optionalSupportImplementation 'org.springframework.security:spring-security-core'
2021
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2016-2022 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.server.autoconfigure;
19+
20+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.core.annotation.Order;
25+
26+
import io.grpc.ServerInterceptor;
27+
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcServerInterceptor;
28+
import io.micrometer.observation.ObservationRegistry;
29+
import net.devh.boot.grpc.common.autoconfigure.GrpcCommonTraceAutoConfiguration;
30+
import net.devh.boot.grpc.common.util.InterceptorOrder;
31+
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
32+
33+
/**
34+
* The configuration used to configure micrometer tracing for grpc.
35+
*
36+
* @author Dave Syer ([email protected])
37+
*/
38+
@Configuration(proxyBeanMethods = false)
39+
@ConditionalOnProperty(value = "management.tracing.grpc.enabled", matchIfMissing = true)
40+
@AutoConfigureAfter(value = GrpcCommonTraceAutoConfiguration.class,
41+
name = "org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration")
42+
@ConditionalOnBean(ObservationRegistry.class)
43+
public class GrpcServerMicrometerTraceAutoConfiguration {
44+
45+
/**
46+
* Configures a global server interceptor that applies micrometer tracing logic to the requests.
47+
*
48+
* @param observations The observation registry.
49+
* @return The tracing server interceptor bean.
50+
*/
51+
@GrpcGlobalServerInterceptor
52+
@Order(InterceptorOrder.ORDER_TRACING_METRICS + 1)
53+
public ServerInterceptor globalObservationGrpcServerInterceptorConfigurer(final ObservationRegistry observations) {
54+
return new ObservationGrpcServerInterceptor(observations);
55+
}
56+
57+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ net.devh.boot.grpc.server.autoconfigure.GrpcReflectionServiceAutoConfiguration
88
net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration
99
net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration
1010
net.devh.boot.grpc.server.autoconfigure.GrpcServerMetricAutoConfiguration
11+
net.devh.boot.grpc.server.autoconfigure.GrpcServerMicrometerTraceAutoConfiguration
1112
net.devh.boot.grpc.server.autoconfigure.GrpcServerSecurityAutoConfiguration

0 commit comments

Comments
 (0)