This topic describes how to instrument OkHttp using SOFATracer based on a sample project.
Click here to download the sample project for this topic.
If you have a simple Spring Web project built on SOFABoot, follow these steps:
Import dependencies
Add the following Tracer dependency to your SOFABoot web project:
<!-- SOFATracer dependency -->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
</dependency>After you add the Tracer starter dependency, you can add configuration items to the global configuration file of SOFABoot to customize the behavior of Tracer. For more information, see Tracer configuration items.
Add the OkHttp dependency:
<!-- OkHttp dependency -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.1</version>
</dependency>Project configuration
In the application.properties file of the project, add the spring.application.name and logging.path parameters for SOFATracer.
spring.application.namespecifies the name of the application.logging.pathspecifies the output directory for logs.
# Application Name
spring.application.name=OkHttpClientDemo
# logging path
logging.path=./logs
# port
server.port=8081Add a controller that provides a RESTful service
Add a simple controller to your project code. The following code provides an example:
@RestController
public class SampleRestController{
private final AtomicLong counter =new AtomicLong(0);
/**
* Request http://localhost:8081/okhttp?name=sofa
* @param name name
* @return Map of Result
*/
@RequestMapping("/okhttp")
public Map<String,Object> greeting(@RequestParam(value ="name", defaultValue ="okhttp")String name){
Map<String,Object> map =new HashMap<>();
map.put("count", counter.incrementAndGet());
map.put("name", name);
return map;
}
}Create an OkHttp client to call the RESTful service
The following code provides an example of creating an OkHttp client instance to call the RESTful service.
OkHttpClientInstance httpClient =new OkHttpClientInstance();
String httpGetUrl ="http://localhost:8081/okhttp?name=sofa";
String responseStr = httpClient.executeGet(httpGetUrl);Start the application
Start the SOFABoot application. The following startup logs appear in the console:
2019-04-1213:38:09.896 INFO 51193---[ main] o.s.j.e.a.AnnotationMBeanExporter:Registering beans for JMX exposure on startup
2019-04-1213:38:09.947 INFO 51193---[ main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat started on port(s):8081(http)
2019-04-1213:38:09.952 INFO 51193---[ main] c.a.s.t.e.okhttp.OkHttpDemoApplication:StartedOkHttpDemoApplicationin3.314 seconds (JVM running for4.157)The following log indicates that the OkHttp call is successful:
2019-04-1213:38:10.205 INFO 51193---[ main] c.a.s.t.e.okhttp.OkHttpDemoApplication:Responseis{"count":1,"name":"sofa"}View logs
The application.properties file sets the log output directory to ./logs, which is the root directory of the application. You can change this path as needed. In the root directory of the project, you can find log files with the following structure:
./logs
├── spring.log
└── tracelog
├── okhttp-digest.log
├── okhttp-stat.log
├── spring-mvc-digest.log
├── spring-mvc-stat.log
├──static-info.log
└── tracer-self.logThe example creates an OkHttp object to call the RESTful service. After the call is complete, a log entry similar to the following appears in the okhttp-digest.log file:
{"time":"2019-09-03 11:35:28.429","local.app":"OkHttpDemo","traceId":"0a0fe9271567481728265100112783","spanId":"0","span.kind":"client","result.code":"200","current.thread.name":"main","time.cost.milliseconds":"164ms","request.url":"http://localhost:8081/okhttp?name=sofa","method":"GET","result.code":"200","req.size.bytes":0,"resp.size.bytes":0,"remote.app":"","sys.baggage":"","biz.baggage":""}