Dubbo instrumentation
This document uses a demo project to demonstrate how to use SOFATracer to instrument Dubbo.
Prerequisites
This demo uses the following framework and component versions:
SOFABoot 3.1.1/SpringBoot 2.1.0.RELEASE
SOFATracer 2.4.0/3.0.4
JDK 8
This demo includes three sub-modules:
tracer-sample-with-dubbo-consumer: The service consumer.
tracer-sample-with-dubbo-provider: The service provider.
tracer-sample-with-dubbo-facade: The interface.
How it works
SOFATracer instruments Dubbo using its Service Provider Interface (SPI) mechanism. SOFATracer provides a custom DubboSofaTracerFilter based on Dubbo's Filter extension to instrument service calls. Because DubboSofaTracerFilter is not an official Dubbo extension, you must add a reference to it as shown in the Filter extension documentation. For example:
<!-- Intercept the consumer call process -->
<dubbo:reference filter="dubboSofaTracerFilter"/>
<!-- Default interceptor for the consumer call process. This intercepts all references. -->
<dubbo:consumer filter="dubboSofaTracerFilter"/>
<!-- Intercept the provider call process -->
<dubbo:service filter="dubboSofaTracerFilter"/>
<!-- Default interceptor for the provider call process. This intercepts all services. -->
<dubbo:provider filter="dubboSofaTracerFilter"/>Create a SOFABoot project as the parent project
After you create a Spring Boot project, add the SOFABoot dependency. First, unzip the Spring Boot project zip package. Then, modify the pom.xml file of the Maven project. Replace the following code:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<relativePath/>
</parent>Replace the existing content with the following:
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-enterprise-dependencies</artifactId>
<version>${sofa.boot.version}</version>
</parent>The ${sofa.boot.version} parameter specifies the SOFABoot version. For more information, see SOFABoot Version Guide.
Create tracer-sample-with-dubbo-facade
Define the service interface:
public interface HelloService{
String SayHello(String name);
}Create tracer-sample-with-dubbo-provider
Add the SOFATracer dependency to the pom.xml file of the project module:
<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>tracer-enterprise-sofa-boot-starter</artifactId> </dependency>NoteThe SOFATracer version is managed by the SOFABoot version. If the SOFABoot version you use is not compatible, you must manually specify a SOFATracer version later than 2.4.0.
Add the following parameters to the
application.propertiesfile of the project:# Spring Boot application spring.application.name=dubbo-provider # Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service dubbo.scan.base-packages=com.alipay.sofa.tracer.examples.dubbo.impl ## The filter must be configured dubbo.provider.filter=dubboSofaTracerFilter # Dubbo Protocol dubbo.protocol.name=dubbo ## Dubbo Registry dubbo.registry.address=zookeeper://localhost:2181 logging.path=./logsPublish the Dubbo service using an annotation:
@Service public class HelloServiceImpl implements HelloService{ @Override public String SayHello(String name){ return "Hello , "+name; } }
Create tracer-sample-with-dubbo-consumer
Add the SOFATracer dependency to the pom.xml file of the project module:
<dependency> <groupId>com.alipay.sofa</groupId> <artifactId>tracer-enterprise-sofa-boot-starter</artifactId> </dependency>Add the following parameters to the
application.propertiesfile of the project:spring.application.name=dubbo-consumer dubbo.registry.address=zookeeper://localhost:2181 # The filter must be configured dubbo.consumer.filter=dubboSofaTracerFilter logging.path=./logsReference the service:
@Reference(async =false) public HelloService helloService; @Bean public ApplicationRunner runner(){ return args ->{ logger.info(helloService.SayHello("sofa")); }; }
Testing
Start the tracer-sample-with-dubbo-provider and tracer-sample-with-dubbo-consumer projects in that order. Then, check the following logs:
dubbo-client-digest.log
{"time":"2019-09-02 23:36:08.250","local.app":"dubbo-consumer","traceId":"1e27a79c156743856804410019644","spanId":"0","span.kind":"client","result.code":"00","current.thread.name":"http-nio-8080-exec-2","time.cost.milliseconds":"205ms","protocol":"dubbo","service":"com.glmapper.bridge.boot.service.HelloService","method":"SayHello","invoke.type":"sync","remote.host":"192.168.2.103","remote.port":"20880","local.host":"192.168.2.103","client.serialize.time":35,"client.deserialize.time":5,"req.size.bytes":336,"resp.size.bytes":48,"error":"","sys.baggage":"","biz.baggage":""}dubbo-server-digest.log
{"time":"2019-09-02 23:36:08.219","local.app":"dubbo-provider","traceId":"1e27a79c156743856804410019644","spanId":"0","span.kind":"server","result.code":"00","current.thread.name":"DubboServerHandler-192.168.2.103:20880-thread-2","time.cost.milliseconds":"9ms","protocol":"dubbo","service":"com.glmapper.bridge.boot.service.HelloService","method":"SayHello","local.host":"192.168.2.103","local.port":"62443","server.serialize.time":0,"server.deserialize.time":27,"req.size.bytes":336,"resp.size.bytes":0,"error":"","sys.baggage":"","biz.baggage":""}dubbo-client-stat.log
{"time":"2019-09-02 23:36:13.040","stat.key":{"method":"SayHello","local.app":"dubbo-consumer","service":"com.glmapper.bridge.boot.service.HelloService"},"count":1,"total.cost.milliseconds":205,"success":"true","load.test":"F"}dubbo-server-stat.log
{"time":"2019-09-02 23:36:13.208","stat.key":{"method":"SayHello","local.app":"dubbo-provider","service":"com.glmapper.bridge.boot.service.HelloService"},"count":1,"total.cost.milliseconds":9,"success":"true","load.test":"F"}
Compatibility with Dubbo 2.6.x
SOFATracer versions 2.4.1, 3.0.6, and later are also compatible with the Dubbo 2.6.x series.
By default, the tracer logs are generated in the user's root directory.
The log-sofa-boot-starter dependency determines the SOFATracer log directory based on the logging.path in the application.properties file. Therefore, you must import log-sofa-boot-starter for Spring Boot projects.